######################## # A Sample R Session ######################## ################# #1.Scatter plot ################# ?mtcars #plot mtcars$mpg vs mtcars$wt plot(mtcars$wt, mtcars$mpg) #plot mtcars$mpg vs index plot(mtcars$mpg) #add lines to the plot plot(mtcars$wt, mtcars$mpg) abline(h=20) #add horizontal line abline(v=4) #add vertical line reg<-lm(mtcars$mpg~mtcars$wt) #fit regression model of mtcars$mpg vs mtcars$wt abline(reg) #add regression line #add main, sub titles and control the range of x and y axis of the plot plot(mtcars$wt, mtcars$mpg,main='scatter plot',sub='mpg vs weight',xlab='weight',ylab='mpg',xlim=c(0,7),ylim=c(5,40)) #control the point type, color, point and label magnitude ?par par(pch=2,col="Red",cex=1.5,cex.lab=2) plot(mtcars$wt, mtcars$mpg) #create pairwise scatter plot pairs(mtcars) #add points to the plot plot(-4:4, -4:4, type = "n") # setting up coord. system points(rnorm(200), rnorm(200), col = "red") #rnorm(200) generate 200 obs from standard normal distribution points(rnorm(100)/2, rnorm(100)/2, col = "blue", cex = 1.5) #add lines to the plot plot(-4:4, -4:4, type = "n") # setting up coord. system points(rnorm(200), rnorm(200), col = "red") lines(c(-2,0,2),c(-2,1,2)) #add legend to the plot: plot(-4:4, -4:4, type = "n") # setting up coord. system points(rnorm(200), rnorm(200), col = "red") points(rnorm(100)/2, rnorm(100)/2, col = "blue", cex = 1.5) legend(2,3,c('Dist1','Dist2'),pch=c(1,1),cex=1,col=c("red","blue")) #present multiple plots simutaniously: par(mfrow=c(2,3)) #setup the parameter so we can produce two by three plots plot(mtcars$wt,mtcars$mpg) plot(mtcars$wt,mtcars$disp) plot(mtcars$wt,mtcars$hp) plot(mtcars$wt,mtcars$drat) plot(mtcars$wt,mtcars$qsec) plot(mtcars$mpg,mtcars$qsec) #add a smooth curve computed by loess (locally weighted scatterplot smoothing) to a scatter plot: plot(mtcars$wt, mtcars$mpg) scatter.smooth(mtcars$wt, mtcars$mpg) ################# #2.QQ plot ################# par(mfrow=c(1,2)) x <- rt(100, df=3) #generate 100 obs from t distribution with df=3 qqnorm(x) #plot theoretical quantiles with sample quantiles qqline(x) #add the qq line y=x qqplot(rt(1000,df=3), x, main="t(3) Q-Q Plot",ylab="Sample Quantiles") #produce qq plot based on two samples abline(0,1) #add the line y=x ################# #3.Histogram ################# set.seed(11091951) #set the random seed x<-rgamma(100,10) #generate 100 obs from gamma distribution with shape parameter=10 par(mfrow=c(2,2)) hist(x) hist(x,breaks=20,xlim=c(0,25),ylim=c(0,15)) hist(x,breaks=10,prob=T,main=c('probability'),xlab='x value',ylab='prob',col='blue',border = "purple") hist(x,breaks=10,prob=T,xlim=c(0,25),ylim=c(0,0.15)) xd<-seq(from=0,to=30,length=1000) yd<-dgamma(xd,shape=10) #generate density values from gamma distribution lines(xd,yd) abline(h=0) #fit density curve by using Kernel smoothing method set.seed(11091951) x<-rgamma(100,10) hist(x,prob=T,breaks=20,xlim=c(0,25),ylim=c(0,0.15)) d<-density(x) #produce kernel density estimation for each x lines(d) ################# #4.Curve ################# curve(sin, -2*pi, 2*pi,xname='t') curve(exp,0,5) curve(sqrt,0,10) ################# #5.Bar Chart ################# par(mfrow=c(2,2)) counts <- table(mtcars$gear) barplot(counts, main="Car Distribution",xlab="Number of Gears") counts <- table(mtcars$gear) barplot(counts, main="Car Distribution", horiz=TRUE,names.arg=c("3 Gears", "4 Gears", "5 Gears")) counts <- table(mtcars$vs, mtcars$gear) barplot(counts, main="Car Distribution by Gears and VS",xlab="Number of Gears", col=c("darkblue","red"),legend = rownames(counts)) counts <- table(mtcars$vs, mtcars$gear) barplot(counts, main="Car Distribution by Gears and VS",xlab="Number of Gears", col=c("darkblue","red"),legend = rownames(counts), beside=TRUE) #function to produce histogram with error bar: error.bar <- function(x, y, upper, lower=upper, length=0.1,...){ if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper)) stop("vectors must be same length") arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...) } y <- rnorm(500, mean=1) #generate 500 obs from normal distribution with mean=1 and sd=1 y <- matrix(y,100,5) y.means <- apply(y,2,mean) y.sd <- apply(y,2,sd) barx <- barplot(y.means, names.arg=1:5,ylim=c(0,1.5), col="blue", axis.lty=1, xlab="Replicates", ylab="Value (arbitrary units)") error.bar(barx,y.means, 1.96*y.sd/10) ################# #6.Pie Chart ################# par(mfrow=c(2,2)) # Simple Pie Chart slices <- c(10, 12,4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pie(slices, labels = lbls, main="Pie Chart of Countries") # Pie Chart with Percentages slices <- c(10, 12, 4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pct <- round(slices/sum(slices)*100) lbls <- paste(lbls, pct) # add percents to labels lbls <- paste(lbls,"%",sep="") # ad % to labels pie(slices,labels = lbls, col=rainbow(length(lbls)), main="Pie Chart of Countries") # 3D Exploded Pie Chart library(plotrix) slices <- c(10, 12, 4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pie3D(slices,labels=lbls,explode=0.1,main="Pie Chart of Countries ") # Pie Chart from data frame with Appended Sample Sizes mytable <- table(iris$Species) lbls <- paste(names(mytable), "\n", mytable, sep="") pie(mytable, labels = lbls,main="Pie Chart of Species\n (with sample sizes)") ###################### #7.Stem and Leaf Plot ###################### stem(mtcars$mpg,scale=1) ###################### #8.Box plot ###################### boxplot(mpg~cyl,data=mtcars, main="Car Milage Data",xlab="Number of Cylinders", ylab="Miles Per Gallon")