setwd("C:\\Users\\Owner\\Desktop\\SchoolStuff\\Spring2013\\Presentations\\Graphs") #set working directory data()#display all available data sets names(mtcars)#variable names in mtcar help(mtcars) #description ov variables attach(mtcars) #windows() #open new graph window; avoid overwritting the old graph; #scatterplot plot(wt, mpg) #scatterplot abline(lm(mpg~wt))#OLS line fit title("Regression of MPG on Weight") #title #scatterplot with multiple y values plot(wt, hp, pch="hp", col="red", ylab="", xlab="weight") points(wt, disp, pch="d", col="green") title("Regression of Weight on Horsepower and Displacement") #title #barplots # Simple Bar Plot counts <- table(mtcars$gear) barplot(counts, main="Car Distribution", xlab="Number of Gears") # Stacked Bar Plot with Colors and Legend 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)) # Grouped Bar Plot counts <- table(mtcars$vs, mtcars$gear) barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue","red"), legend = c("vs = 0", "vs = 1" ), beside=TRUE) # Fitting Labels #las=2 # make label text perpendicular to axis #cex.names changes the font size for labels par(mar=c(5,8,4,2)) # increase y-axis margin. counts <- table(mtcars$gear) barplot(counts, main="Car Distribution", horiz=TRUE, names.arg=c("3 Gears", "4 Gears", "5 Gears"), cex.names=0.8, las=2) #histogram hist(mpg, breaks=12, col="red", xlab="Miles/gallon", main="Histogram of mpg") #colored histogram with different number of bins xvals<-seq(from=min(mpg), to=max(mpg) , length=40) lines(pnorm(xvals, mean(mpg), sd(mpg)) #add a normal density curve to a histogram x<-mpg h<-hist(x, breaks=12, col="turquoise2", xlab="Miles Per Gallon", main="Histogram with Normal Curve") xfit<-seq(min(x),max(x),length=40) #a sequence of numbers from min x to max x of length 40 yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) #density plot , returns prob yfit <- yfit*diff(h$mids[1:2])*length(x) #diff(h$mids[1:2]) is the difference between 2 midpoints , depends on numebre of bins spec by breaks lines(xfit, yfit, col="blue", lwd=2) #stem-and-leaf plot stem(carb) #side by side boxplots # Boxplot of MPG by Car Cylinders bx=boxplot(mpg~cyl,data=mtcars, main="Car Milage Data", xlab="Number of Cylinders", ylab="Miles Per Gallon", col="skyblue") # Violin Plots library(vioplot) x1 <- mtcars$mpg[mtcars$cyl==4] x2 <- mtcars$mpg[mtcars$cyl==6] x3 <- mtcars$mpg[mtcars$cyl==8] vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"), col="gold") title("Violin Plots of Miles Per Gallon") #QQ plot qqnorm(mpg) qqline(mpg) #Interaction plot data(ToothGrowth) attach(ToothGrowth) png("Scatterplot.png") #redirect graph as a PNG image interaction.plot(dose, supp, len, fixed = TRUE, type = c("o"), col=c("red","blue"), font.main=4, col.main="darkgreen",legend=FALSE , main="Interaction Plot between Dose and Supplement Type", ylab="Mean of Tooth Length", xlab="Dose", font.lab=3) legend("bottomright", legend = c ("VC", "OJ"),lty=c(3,1), col=c("red","blue")) dev.off() #close graphical device #Density plot #plot univariate density curve; d <-density(mpg) plot(d, main="Kernel Density") polygon(d, col="wheat4", border="red") #fill the density plot #3-D plots # 3D Scatterplot with Coloring and Vertical Lines and Regression Plane library(scatterplot3d) s3d <-scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE, type="h", main="3D Scatterplot") fit <- lm(mpg ~ wt+disp) s3d$plane3d(fit) #add different types of lines to a graph; ref Quick - R x <- c(1:5); y <- x # create some data par(pch=22, col="red") # plotting symbol and color par(mfrow=c(2,4)) # all plots on one page opts = c("p","l","o","b","c","s","S","h") for(i in 1:length(opts)){ heading = paste("type=",opts[i]) plot(x, y, type="n", main=heading) lines(x, y, type=opts[i]) } #InClassActivity #plot the within/between corelation and rho(ICC); sdb<-seq(1, 30, 1) sdw<-c(5,10,15,20,25) icc<-matrix(ncol=30, nrow=5, NA) for(i in 1:length(sdw)){ for(j in 1:length(sdb)) { icc[i,j]<-sdb[j]^2/(sdb[j]^2+sdw[i]^2) }} one<-expression(paste(sigma[b]^2, sep=" ", "(Betwen Subject Variation)")) #subscript b and squared; plot(sdb^2, icc[1,], pch=19, col="blue", type="l", xlab=c(one), ylab ="ICC", cex=3, lwd=3, font=12, main = "Intraclass Correlation Coefficint (ICC) \n under Compound Symmetry (CSS)") for (i in 1:length(sdw)) lines(sdb^2, icc[i,], lwd=3, col = c(i*13)) lTitle= expression(paste(sigma^2, "(Within Subject Variation)")) legend("bottomright", c("25", "100", "225", "400", "625"), title=lTitle, col=c(13,26 ,3*13, 4*13, 5*13), pch=19, bg='gray90', bty="n", cex=.7)