Enrollment No. : 165023693099        
Division : B
Batch: B2
Semester: 4th
Course: Master of Computer Applications(MCA)
Subject : Data Analytics with R
Subject Code: 3640005
Faculty Name: Dr.Hetal Thaker
College: Atmiya Institute Of Technology &        Science(AITS)



Bar plot Example

1.Simple Bar Plot
-------------------------------------------------------------------
Definition: A bar chart represents data in rectangular bars with length of the bar proportional to the value of the variable. R uses the function barplot() to create bar charts. R can draw both vertical and horizontal bars in the bar chart. In bar chart each of the bars can be given different colors.

Syntax: barplot(H, xlab, ylab, main, names.arg, col).
 
 
Following is the description of the parameters used −
·         H is a vector or matrix containing numeric values used in bar chart.
·         xlab is the label for x axis.
·         ylab is the label for y axis.
·         main is the title of the bar chart.
·         names.arg is a vector of names appearing under each bar.
·         col is used to give colors to the bars in the graph.
 
Example:

> mtcars
                     mpg cyl  disp  hp drat    wt  qsec vs
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0

counts <- table(mtcars$gear)
barplot(counts,main = "Car Distribution",xlab = "Number of Gear")
2.Simple Horizontal Bar Plot with added Labels.
counts <- table(mtcars$gear)
barplot(counts,main = "Car Distribution Demo",horiz = TRUE,
        names.arg = c("3 Gears","4 Gears","5 Gears"))
Stacked Bar Plot
1.Stacked Bar Plot with colors and Legend.
counts <- table(mtcars$vs,mtcars$gear)
barplot(counts,main = "Car Distribution By Gears ans VS",
xlab="Number of Gears",
horiz = TRUE,
col = c("darkblue","red"),
 legend=rownames(counts),
names.arg = c("3 Gears","4 Gears","5 Gears"))
R-Bar chart
1.create a simple bar chart.
H <- c(7,12,28,3,41)
png(file="barchat5.png")
2.current r Working Directory.
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")
#png(file="barchat_Months.png")
barplot(H,names.arg = M,xlab = "Month",
        ylab = "Revenue",
        col=c("Red","Blue","Green","Pink","Yellow"),
        main = "Revenue Chart",
        border = "Red")
#dev.off()
3.we can create bar chart with groups of bars and stcks(Matrix).
colors <- c("Green","Orange","Brown")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")
values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),
nrow=3,ncol=5,
byrow=TRUE)
#png(file="barchart_stacked.png")
barplot(values,main = "Total Revenue",names.arg = months,xlab = "Month",ylab = "Revenue",col = colors)
legend("topleft",regions,cex=1.3,fill=colors)
#dev.off()
colors <- c("Green","Red","Blue")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")
values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),
nrow=3,ncol=5,
byrow=TRUE)
#png(file="barchart_stacked.png")
barplot(values,main = "Total Revenue",names.arg = months,xlab = "Month",ylab = "Revenue",col = colors)
legend("topleft",regions,cex=1.3,fill=colors)
#dev.off()
R - Line Graphs
A line chart is a graph that connects a series of points by drawing line segments between them. These points are ordered in one of their coordinate (usually the x-coordinate) value. Line charts are usually used in identifying the trends in data.
The plot() function in R is used to create the line graph.

Syntax

The basic syntax to create a line chart in R is −
plot(v,type,col,xlab,ylab)
Following is the description of the parameters used −
·         v is a vector containing the numeric values.
·         type takes the value "p" to draw only the points, "l" to draw only the lines and "o" to draw both points and lines.
·         xlab is the label for x axis.
·         ylab is the label for y axis.
·         main is the Title of the chart.
·         col is used to give colors to both the points and lines.

Example

1.A simple line chart is created using the input vector and the type parameter as "O". The below script will create and save a line chart in the current R working directory.
v <- c(7,12,28,3,41)
 
png(file = "line_chart.jpg")
plot(v,type = "o")
dev.off()

 

 

Line Chart Title, Color and Labels

# Create the data for the chart.
v <- c(7,12,28,3,41)
png(file = "line_chart.jpg")
plot(v,type = "o")
dev.off()
# Create the data for the chart.
v <- c(7,12,28,3,41)
png(file = "line_chart_label_colored.jpg")
plot(v,type = "o", col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")
dev.off()

Multiple Lines in a Line Chart

àMore than one line can be drawn on the same chart by using the lines()function.
# Create the data for the chart.
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)

# Give the chart file a name.
png(file = "line_chart_2_lines.jpg")
plot(v,type = "o",col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")
lines(t, type = "o", col = "blue")
dev.off()

Creating the Boxplot

Boxplots are a measure of how well distributed is the data in a data set. It divides the data set into three quartiles. This graph represents the minimum, maximum, median, first quartile and third quartile in the data set. It is also useful in comparing the distribution of data across data sets by drawing boxplots for each of them.
Boxplots are created in R by using the boxplot() function.

Syntax

The basic syntax to create a boxplot in R is −
boxplot(x, data, notch, varwidth, names, main)
Following is the description of the parameters used −
·         x is a vector or a formula.
·         data is the data frame.
·         notch is a logical value. Set as TRUE to draw a notch.
·         varwidth is a logical value. Set as true to draw width of the box proportionate to the sample size.
·         names are the group labels which will be printed under each boxplot.
·         main is used to give a title to the graph.

Example

àThe below script will create a boxplot graph for the relation between mpg (miles per gallon) and cyl (number of cylinders).
png(file = "boxplot.png")
boxplot(mpg ~ cyl, data = mtcars,
xlab = "Number of Cylinders",
ylab = "Miles Per Gallon", main = "Mileage Data")
dev.off()

Boxplot with Notch


png(file = "boxplot_with_notch.png")
boxplot(mpg ~ cyl, data = mtcars,
        xlab = "Number of Cylinders",
        ylab = "Miles Per Gallon",
        main = "Mileage Data",
        notch = TRUE,
        varwidth = TRUE,
        col = c("green","yellow","purple"),
        names = c("High","Medium","Low")
)
dev.off()




R - Pie Charts
R Programming language has numerous libraries to create charts and graphs. A pie-chart is a representation of values as slices of a circle with different colors. The slices are labeled and the numbers corresponding to each slice is also represented in the chart.
In R the pie chart is created using the pie() function which takes positive numbers as a vector input. The additional parameters are used to control labels, color, title etc.

Syntax

The basic syntax for creating a pie-chart using the R is −
pie(x, labels, radius, main, col, clockwise)
Following is the description of the parameters used −
·         x is a vector containing the numeric values used in the pie chart.
·         labels is used to give description to the slices.
·         radius indicates the radius of the circle of the pie chart.(value between −1 and +1).
·         main indicates the title of the chart.
·         col indicates the color palette.
·         clockwise is a logical value indicating if the slices are drawn clockwise or anti clockwise.


Example

x <- c(21,62,10,53)
labels <- c("BCA","MCA","PGDCA","BSCIT")
pie(x,labels,main = "My Pie chart Demo",col =rainbow(length(x)))

3D Pie Chart

A pie chart with 3 dimensions can be drawn using additional packages. The package plotrix has a function called pie3D() that is used for this.
# Get the library.
library(plotrix)

x <- c(21,62,10,53)
lbl <- c("SE","ANDROID","BS","R_Lang")
pie3D(x,labels = lbl,explode = 0.1,main="My Subjects")

No comments:

Post a Comment

Unleashing B2B Success: Mastering Event and Conversion Tracking with GA4

Tracking events and conversions is vital for B2B businesses using GA4 (Google Analytics 4) to optimize their marketing efforts. Here is a re...