################ # Section 11 ################ # Demonstrations of some R graphics demo(graphics) demo(image) #Creating and managing windows windows() windows() # opening windows -- graphics devices windows() # On a Mac, instead of windows(), you can try: x11() # should work on Linux too # or: quartz() dev.set() # will return the active device dev.set() dev.set() dev.set(3) # setting the active device dev.set(2) dev.off() # closes a graphics window zvars<-rnorm(100) plot(zvars) # Simple plot of a one-dimensional data vector is not very informative data(state) state.dfr <- data.frame(I(state.abb),state.region,state.x77) attach(state.dfr) # A basic histogram: hist(Population) hist(Population, main="Population in 10,000s by State, 1977") # plot of a nonparametric density estimate: plot(density(Population)) density(zvars) # summary statistics, no plot shown plot(density(zvars)) plot(density(rnorm(1000))) plot(density(rnorm(10000))) # A simple boxplot boxplot(zvars) # A basic stem-and-leaf plot stem(zvars) # Plots for categorical data: VADeaths # a two-way table for two categorical variables barplot(VADeaths,legend=T) # default is stacked bars to show the two dimensions barplot(t(VADeaths),legend=T) # problem with legend barplot(t(VADeaths),legend=T,args.legend = list(x = "topleft")) # much better # Revisiting the "barplot" command # beside=TRUE produces side-by-side bars for each group # beside=FALSE (the default) produces the stacked bars barplot(VADeaths, beside = TRUE, col = c("lightblue", "mistyrose", "lightcyan", "lavender", "cornsilk"), legend = rownames(VADeaths), ylim = c(0, 100)) barplot(t(VADeaths), beside = TRUE, col = c("lightblue", "mistyrose", "lightcyan", "lavender"), legend = colnames(VADeaths), ylim = c(0, 100)) pie(VADeaths) # basic pie chart # Showing marginal distribributions: pie(apply(VADeaths,1,sum)) pie(apply(VADeaths,2,sum)) barplot(apply(VADeaths,1,sum)) barplot(apply(VADeaths,2,sum)) # Two-Dimensional Scatterplots: data(state) Illiteracy<-state.x77[,3] Murder<-state.x77[,5] plot(Illiteracy,Murder) # basic scatterplot ?par # help file for graphics parameters par() # returns default values for graphics parameters # Adding things onto a basic scatterplot: plot(Illiteracy,Murder,las=1,lwd=2,cex=1.2,pch=19, xlab="Percent Illiterate",ylab="") #las=1 prints axis labels horizontally title("Murders per 100,000 vs. Percent Illiterate") text(2,4,"The 50 States",cex=1.2, adj=0, col=2) # placing text in an interactive way: text(locator(1),"1977",cex=1.2, adj=0, col=2) arrows(x0=2.4, y0=8, x1=2.7, y1=12) text(2.4, 7.5, "Which state?") # Plotting a simple function: xgrid <- seq(0,10,length=25) yhat <- sqrt(xgrid) plot(xgrid,yhat,type='l') # The 'lines' function puts a curve on top of an existing scatterplot: plot(xgrid, runif(25,0,10)) lines(xgrid,yhat) plot(xgrid, runif(25,0,10), xlim=c(0,20), ylim=c(0,20)) # controlling the plotting window limits # The 'points' function plots points on top of an existing scatterplot: plot(xgrid,yhat,type='l', xlim=c(0,10), ylim=c(0,10)) points(xgrid, runif(25,0,10)) # Plotting a regression function on top of data: plot(Illiteracy,Murder) Illiteracy2 <- Illiteracy^2 lm(Murder ~ Illiteracy + Illiteracy2) Illgrid <- pretty(Illiteracy,50) Murderhat <- 1.6627+5.5642*Illgrid-0.4586*Illgrid^2 lines(Illgrid,Murderhat,lty=2,lwd=2,col=2) text(0.5,14,"With Fitted Quadratic", adj=0, col=2) colors() # shows possible colors for plots # Plotting a standard normal density: curve(dnorm,-4,4) curve(dnorm,-4,4,ylab="Standard Normal Density") curve(dnorm,-4,4,main="Standard Normal Density",ylab=expression(phi(x))) xvec <- yvec <- c(0,1) plot(xvec,yvec, type="n",xaxt="n",xlab="",yaxt="n",ylab="") # Creates empty plot text(.6,.4,"The BEATLES",cex=1.5, adj=0, lwd=5, col="gray") # Multiple plots on a page par(mfrow=c(1,2)) plot(rnorm(15)) #repeat above command par(mfrow=c(2,3)) par(mfcol=c(2,3)) # A simple example par(mfrow=c(1,2)) plot(Illiteracy,Murder) plot(Frost,Illiteracy) # Demonstrating plotting characters and line types: windows() par(mfrow=c(1,2),pty="s") plot(1:25,1:25,type="n") for (i in 1:25) points(i,i,pch=i,cex=0.7) title("25 pch symbols") plot(1:6,1:6,type="n") for (i in 1:6) lines(c(1,6),c(i,i),lty=i,lwd=2) title("6 lty line types") #An improved display for the plotting characters par(mfrow=c(1,1)) plot(c(0.5,5.5),c(0.5,5.5),type="n",xlab="",ylab="",main="Plotting symbols 1-25, row by row") rowseq=rep(1:5,each=5) colseq=rep(1:5,5) pchval=(colseq-1)*5+rowseq points(rowseq,colseq,pch=pchval,cex=3.0) text(rowseq,colseq-.3,paste(pchval)) # 3-D plots help(datasets) data(volcano) volcano contour(volcano) # a contour plot # Some 3-D perspective plots: persp(volcano, theta=30, phi=15) persp(volcano, theta=60, phi=15) persp(volcano, theta=90, phi=15) persp(volcano, theta=150, phi=15) persp(volcano, theta=0, phi=45) persp(volcano, theta=0, phi=0) persp(volcano, theta=0, phi=-30) filled.contour(volcano) #colors needs an argument filled.contour(volcano,col=terrain.colors(10)) filled.contour(volcano,col=terrain.colors(25)) # A heat plot: image(volcano) # Two plots on same window: image(volcano) par(new=T) # does not remove previous plot before doing next plot contour(volcano) # A better way: image(volcano) contour(volcano, add=T) # Interactive graphics plot(Illiteracy,Murder) mypoints <- identify(Illiteracy,Murder,state.abb) mypoints state.abb[mypoints] mypoints <- identify(Illiteracy,Murder,state.abb,pos=TRUE) mypoints state.abb[mypoints$ind] #Scatterplot matrix: pairs(state.x77[,3:6]) # Plotting scatterplots for separate categories: coplot( Murder ~ Illiteracy | state.region) #trellis graphs # loading the "lattice" package: library(lattice) xyplot( Murder ~ Illiteracy | state.region) ### Saving graphics output as postscript or pdf files: # File -> Change directory, then: postscript("myplotname.eps",onefile=FALSE, horizontal=FALSE) # plotting commands: plot(Illiteracy,Murder) # Closing graphics device: dev.off() # To save as a pdf: pdf("myplotname.pdf",onefile=FALSE) # plotting commands: plot(Illiteracy,Murder) # Closing graphics device: dev.off()