bar chart - wordpress.com · 9/2/2019 · to create pie chart for following tabular text city...
TRANSCRIPT
Bar Chart
Simple Bar Graph Creating Bar Chart of Numeric Vector, say, as
> aa=c(46, 76, 37, 86, 46, 75)
> barplot(aa)
Or, barplot(c(46, 76, 37, 86, 46, 75))
To give title and sub-title to the graph created above
> barplot(aa, main = "Nightingale", sub = "2017")
To give titles to horizontal and vertical axes
> barplot(aa, main = "Nightingale", sub = "2017", xlab = "Districts", ylab = "Production")
02
04
06
08
0
If bar plot of 'aa' created above corresponds to production of cereal in six districts say, "Kavre", "Kaski",
"Palpa", "Nuwakot", "Dhading", "Morang", then by using bar graph of vector, as created above, it is not
possible to provide names of different districts to different rectangles of bar graph. But if above values
are stored as matrix of one dimension, then by using 'dimnames' attribute it will be possible.
> aa = c(46, 76, 37, 86, 46, 75)
> bb = matrix(aa, nrow = 1, ncol=6, dimnames = list("District", c("Kavre", "Kaski", "Palpa", "Nuwakot",
"Dhading", "Morang")))
> barplot(bb)
Another way of displaying bar graph in above mentioned format is as follows
> barplot(aa, names.arg=c("Kavre", "Kaski", "Palpa", "Nuwakot", "Dhading", "Morang")))
Stacked Bar Graph For following matrix
To create stacked bar graph of marks in three subjects, as
> mm1 = matrix(c(34, 57, 54,76, 57, 87), nrow=2, ncol=3, dimnames = list(c("Rajan", "Hari"), c("Math",
"Science", "Computer")))
> barplot(mm1)
To create bar chart of above matrix with rectangles of each subject adjacent as
> barplot(mm1, beside = TRUE)
To specify title and subtitle
> barplot(mm1, beside = TRUE, main = "Marks", sub = "Nightingale")
To provide titles for axes
Use 'xlab' and 'ylab'
Another example
To create stacked bar chart of form as shown below
Create a matrix of following form
Mar Apr May Jun Jul East 2 9 3 11 9 West 4 8 7 3 12 North 5 2 8 10 11
First create a vector for three regions by
> reg= c("East", "West", "North")
Create a vector for month names by
> months=c("Mar","Apr","May","Jun","Jul")
Next, create matrix for revenue values in the 3 regions as
> rev = matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),nrow = 3,ncol = 5,byrow = TRUE)
Create a vector for three different colors for three different regions by
> colors=c("Green","Orange","Brown")
Give the chart a name by
> png(file="ccc.png")
Create the bar plot by
> barplot(rev, main="Total Revenue", names.arg=months, xlab="Months", ylab="Revenue", col=colors)
Next, to provide legends to different regions
> legend(x="topleft", legend=reg, fill=colors)
To create bar graph from dataframe Create a '.csv' of form as shown below and save it in name 'prod.csv'.
Load the data frame by
> prod1 = read.csv("prod.csv", header=TRUE)
Now to create bar graph of 'Production' column
> barplot(prod1[,2])
Next to create the bar graph with "year" as X axis label
> barplot(prod1[,2], names.arg=prod1[,1], col="Grey")
To display border of color (other than black) around bars
Use "border = " attribute in barplot() function.
Pie Chart To create pie chart with a vector of numbers
> dd = c(46, 75, 47, 65, 25, 57)
> pie(dd) // Or, pie(x = dd)
To create pie chart For following tabular text
City Beijing New Delhi Moscow Kathmandu Mumbai
Length of road in km
1450 2500 1255 789 1435
> city = c("Beijing","New Delhi","Moscow", "Kathmandu", "Mumbai")
> road = c(145, 2500, 1255, 789, 1435)
> pie(city, road) // or, pie(x= road, labels = city)
To bring rainbow colors in the pie chart
> pie(x= road, labels = city, col = rainbow(length(road)))
To display values in pie chart and create legend for cities, as
> pie(x= road, labels = road, col = rainbow(length(road)))
> legend(x="topright", legend=city, fill=rainbow(length(road)), bg = "grey")
To display percentages in pie chart and create legend for cities, as
> percent = round(road/sum(road)*100,1)
> pie(x= road, labels = percent, col = rainbow(length(road)))
> legend(x="topright", legend=city, fill=rainbow(length(road)), bg = "grey")
To create 3D pie chart
Needs new package
??? From // www.tutorialspoint.com/r
And www.r-tutor.com
Histograms The basic syntax is
hist(v,main,xlab,xlim,ylim,breaks,col,border)
• v is a vector containing numeric values used in histogram.
• main indicates title of the chart.
• col is used to set color of the bars.
• border is used to set border color of each bar.
• xlab is used to give description of x-axis.
• xlim is used to specify the range of values on the x-axis.
• ylim is used to specify the range of values on the y-axis.
• breaks is used to mention the width of each bar.
Histogram of Numeric Vector To create histogram for following values in vector
> v = c(34, 25, 32, 35, 41, 42, 36, 33, 44, 22, 11, 5, 6, 15, 23)
To create histogram of above values as
> hist(v)
To fill color and display border of different color, as
> hist(v, xlab="Weight", col="Light Blue", border="Red")
To fix range of horizontal and vertical axes in histogram
> hist(v, xlab="Weight", col="Light Blue", border="Red", xlim = c(0,60), ylim=c(1,6))
To create histogram for data in dataframe
The data frame file "proteinconc.xls" is available in working directory.
A csv version of this file is also created there. The data are as follows-
First we need to create a data frame for csv file by using
> proteinconc = read.csv("proteinconc1.csv", header = TRUE)
To create a histogram of 'nucleus' column
> hist(proteinconc[, 1])
To set column heading of respective column as title for histogram, we can use "main=" attribute as
follows-
> hist(proteinconc[, 2], main = colnames(proteinconc[2])
To set number of rectangles in histogram to a fixed value
Use "breaks= " attribute in hist() function and set it to required number.
To set number and width of rectangles in histogram
Use "breaks= " attribute and use vector c(0.5, 0.7, 0.8, 0.9,1)
To plot frequency density in vertical axis of histogram instead of frequency
Set "freq=" to FALSE in hist() function.
Line Graph
To create line graph of following values in vector
> v = c(34, 25, 32, 35, 41, 42, 36, 33, 44, 22, 11, 5, 6, 15, 23)
Now to create line graph of these values use plot() function and then include attribute 'type =' and set
its value to "o", as
> plot(v, type="o")
To display multiple lines in the graph, use lines() function, as discussed in 'Scatter Plot' section.
Scatter Plots (Or, Dot plots, or XY-plot)
To create scatter plot of a sequence of numbers
> aa=c(23, 43, 34, 54, 35)
> plot(aa)
To create scatter plot of two sequences (of same length) one at horizontal and other at vertical axes.
> plot(1:10, 1:10)
> plot(c(1991,1992,1993,1994,1995), c(25, 54, 45, 66, 87))
Notes:
To display graph title
Inside plot() command use "main=" attribute
To display X axis title and Y axis titles
Inside plot() command use "xlab=" and "ylab=" attributes
To change color of indicators
Inside plot() command use "col=" attribute and set it to "red", "blue", etc.
To display lines to join dots in the plot
After creating a scatter plot, use "lines()" function. For exm.
> plot(c(1991,1992,1993,1994,1995), c(25, 54, 45, 66, 87))
> lines(c(1991,1992,1993,1994,1995), c(25, 54, 45, 66, 87))
To display horizontal line in the scatter plot at desired location
> abline(h=5)
To display vertical line in the scatter plot at desired location
> abline(v=1993)
To display line of given intercept and given slope
> abline(coef=c(35, 2)) // not doing.
To add extra points in an existing scatter plot
> points(1:5, c(4, 4, 4, 4, 4), pch=1:5, col=1:5) //not doing
To create scatter plot of data in a dataframe
> proteinconc = read.csv("proteinconc1.csv", header=TRUE)
> plot(proteinconc[,1])
> lines(proteinconc[ ,1])
To create new scatter plot on the top of existing one
> par(new = TRUE)
> plot(proteinconc[,2])
> lines(proteinconc[ ,2], col = "red")
To create legend for two scatter plots
> legend(x="topright", legend=c("Nucleus","Nuclear Membrane"), lwd=2, col = 1:2, bg="grey")
To create 2D scatter plot
The data file 'mtcars.csv' in included and displayed below
> dat = read.csv(file="mtcars.csv", header= T)
> dat
Now to create scatter plot for 'Cylinder' versus 'Mileage'
> plot(~Cylinder+Mileage, data=dat)
To create two separate scatter plots for Cylinder and Mileage in paired form as
> pairs(~Cylinder+Mileage, data=dat)
To create paired plots for Mileage, Cylinder and Weight, as
> plot(~Cylinder+Mileage+Weight, data = dat)
Or,
> pairs(~Cylinder+Mileage+Weight, data = dat)
Boxplot To create boxplot of all numeric fields in "proteinconc.csv" file
> proteinconc = read.csv("proteinconc1.csv", header=TRUE)
> boxplot(proteinconc)
To create box plot of only values in second column of above file
> boxplot([, 2])
The dataset 'mtcars' stores information on number of cylinders of different models of cars and their
mileage.
Now to create box plot for 'mileage' data
> mpg = Read.csv("mtcars.csv", header= TRUE)
> boxplot(mpg $ Mileage)
To create 4 different box plots for different "cyl" values, as
> boxplot(mpg $ Mileage ~ mpg $ Cylinder)
Similarly, box plots for different models can be displayed as
To provide different colors to above boxplots, as
> boxplot(mpg$Mileage ~ mpg$Model, col=c("Green","Yellow","Red","Brown","Blue"))
The basic syntax of boxplot() function is as follows
boxplot(x, data, notch, varwidth, names, main)
• 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.
Stem and Leaf Plot
To create stem-and-leaf plot for 'eruptions' variable in 'faithful' data table, as