## 3. Plotting demo(graphics) ## 3.1 plot () and allied functions plot(y ~ x) # Use a formula to specify the graph plot(x, y) # plot((0:20)*pi/10, sin((0:20)*pi/10)) plot((1:30)*0.92, sin((1:30)*0.92)) attach(elasticband) # R now knows where to find distance & stretch plot(distance ~ stretch) plot(ACT ~ Year, data=austpop, type="l") plot(ACT ~ Year, data=austpop, type="b") attach(austpop) plot(spline(Year, ACT), type="l") # Fit smooth curve through points detach(austpop) # In S-PLUS, specify detach("austpop") ## 3.1.1 Plot methods for other classes of object plot(hills) # Has the same effect as pairs(hills) ## 3.2 Fine control - Parameter settings par(cex=1.25) # character expansion oldpar <- par(cex=1.25, mex=1.25) # mex=1.25 expands the margin by 25% attach(elasticband) oldpar <- par(cex=1.5) plot(distance ~ stretch) par(oldpar) # Restores the earlier settings detach(elasticband) oldpar <- par(cex=1.25) on.exit(par(oldpar)) ## 3.2.1 Multiple plots on the one page par(mfrow=c(2,2), pch=16) attach(Animals) # This dataset is in the MASS package, which must be attached plot(body, brain) plot(sqrt(body), sqrt(brain)) plot((body)^0.1, (brain)^0.1) plot(log(body),log(brain)) detach(Animals) par(mfrow=c(1,1), pch=1) # Restore to 1 figure per page ## 3.2.2 The shape of the graph sheet ## 3.3 Adding points, lines and text primates attach(primates) # Needed if primates is not already attached. plot(Bodywt, Brainwt) text(x=Bodywt, y=Brainwt, labels=row.names(primates), pos=4) # pos=4 positions text to the right of the point plot(x=Bodywt, y=Brainwt, pch=16, xlab="Body weight (kg)", ylab="Brain weight (g)", xlim=c(0,280), ylim=c(0,1350)) # Specify xlim so that there is room for the labels text(x=Bodywt, y=Brainwt, labels=row.names(primates), pos=4) detach(primates) text(x=Bodywt, y=Brainwt, labels=row.names(primates), pos=2) ## 3.3.1 Size, colour and choice of plotting symbol plot(1, 1, xlim=c(1, 7.5), ylim=c(1.75,5), type="n", axes=F, xlab="", ylab="") # Do not plot points box() points(1:7, rep(4.5, 7), cex=1:7, col=1:7, pch=0:6) text(1:7,rep(3.5, 7), labels=paste(0:6), cex=1:7, col=1:7) points(1:7,rep(2.5,7), pch=(0:6)+7) # Plot symbols 7 to 13 text((1:7), rep(2.5,7), paste((0:6)+7), pos=4) # Label with symbol number points(1:7,rep(2,7), pch=(0:6)+14) # Plot symbols 14 to 20 text((1:7), rep(2,7), paste((0:6)+14), pos=4) # Labels with symbol number view.colours <- function(){ plot(1, 1, xlim=c(0,14), ylim=c(0,3), type="n", axes=F, xlab="",ylab="") text(1:6, rep(2.5,6), paste(1:6), col=palette()[1:6], cex=2.5) text(10, 2.5, "Default palette", adj=0) rainchars <- c("R","O","Y","G","B","I","V") text(1:7, rep(1.5,7), rainchars, col=rainbow(7), cex=2.5) text(10, 1.5, "rainbow(7)", adj=0) cmtxt <- substring("cm.colors", 1:9,1:9) # Split "cm.colors" into its 9 characters text(1:9, rep(0.5,9), cmtxt, col=cm.colors(9), cex=3) text(10, 0.5, "cm.colors(9)", adj=0) } view.colours() ## 3.3.2 Adding Text in the Margin ## 3.4 Identification and Location on the Figure Region ## 3.4.1 identify() attach(florida) plot(BUSH, BUCHANAN, xlab="Bush", ylab="Buchanan") identify(BUSH, BUCHANAN, County) detach(florida) ## 3.4.2 locator() attach(florida) # if not already attached plot(BUSH, BUCHANAN, xlab="Bush", ylab="Buchanan") locator() detach(florida) ## 3.5 Plots that show the distribution of data values ## 3.5.1 Histograms and density plots attach(possum) here <- sex == "f" hist(totlngth[here], breaks = 72.5 + (0:5) * 5, ylim = c(0, 22), xlab="Total length", main ="A: Breaks at 72.5, 77.5, ...") detach(possum) attach(possum) plot(density(totlngth[here]),type="l") detach(possum) attach(possum) here <- sex == "f" dens <- density(totlngth[here]) xlim <- range(dens$x) ylim <- range(dens$y) hist(totlngth[here], breaks = 72.5 + (0:5) * 5, probability = TRUE, xlim = xlim, ylim = ylim, xlab="Total length", main="") lines(dens) detach(possum) ## 3.5.3 Boxplots attach(possum) boxplot(totlngth[here], horizontal=TRUE) rug(totlngth[here], side=1) detach(possum) ## 3.5.4 Normal probability plots x11(width=8, height=6) # This is a better shape for this plot attach(possum) here <- sex == "f" par(mfrow=c(2,4)) # A 2 by 4 layout of plots y <- totlngth[here] qqnorm(y,xlab="", ylab="Length", main="Possums") for(i in 1:7)qqnorm(rnorm(43),xlab="", ylab="Simulated lengths", main="Simulated") detach(possum) # Before continuing, type dev.off() ## 3.6 Other Useful Plotting Functions ## 3.6.1 Scatterplot smoothing attach(ais) here<- sex=="f" plot(pcBfat[here]~ht[here], xlab = "Height", ylab = "% Body fat") panel.smooth(ht[here],pcBfat[here]) detach(ais) ## 3.6.2 Adding lines to plots attach(ais) here<- sex=="f" plot(pcBfat[here] ~ ht[here], xlab = "Height", ylab = "% Body fat") abline(lm(pcBfat[here] ~ ht[here])) detach(ais) ## 3.6.3 Rugplots attach(ais) here <- sex == "f" oldpar <- par(mar=c(4.1,1.1,1.1, 1.1), mgp=c(2.25, 0.75,0)) boxplot(ht[here], boxwex = 0.35, ylab = "Height", horizontal=TRUE) rug(ht[here], side = 1) par(oldpar) detach(ais) ## 3.6.4 Scatterplot matrices ## 3.6.5 Dotcharts dotchart(islands) # vector of named numeric values, in the datasets base package dotchart(islands, cex=0.5) ## 3.7 Plotting Mathematical Symbols ##x <- 1:15 plot(x, y, xlab="Radius", ylab=expression(Area == pi*r^2)) demo(graphics) ## 3.8 Guidelines for Graphs 3.9 Exercises 3.10 References 4. Lattice graphics ## 4.1 Examples that Present Panels of Scatterplots - Using xyplot() xyplot(csoa ~ it | sex * agegp, data=tinting) # Simple use of xyplot() xyplot(csoa~it|sex*agegp, data=tinting, groups=target, auto.key=list(columns=2)) xyplot(csoa~it|sex*agegp, data=tinting, panel=panel.superpose, groups=target, type=c("p","smooth") xyplot(csoa~it|sex*agegp, data=tinting, groups=tint, auto.key=list(columns=3)) ## 4.2 Some further examples of lattice plots ## 4.2.1 Plotting columns in parallel library(DAAGxtras) xyplot(Beer+Spirit+Wine ~ Year | Country, outer=TRUE, data=grog) xyplot(Beer+Spirit+Wine ~ Year, groups=Country, outer=TRUE, data=grog) xyplot(Beer+Spirit+Wine ~ Year | Country, outer=FALSE, data=grog, auto.key=list(columns=3), par.settings=simpleTheme(pch=16, cex=2) ) ## 4.2.2 Fixed, sliced and free scales library(DAAG) ## scale="fixed" xyplot(BC+Alberta ~ Date, data=jobs, outer=TRUE) ## scale="sliced" - different slices of same scale xyplot(BC+Alberta ~ Date, data=jobs, outer=TRUE, scales=list(y=list(relation="sliced")) ) ## scale="free" - independent scales xyplot(BC+Alberta ~ Date, data=jobs, outer=TRUE, scales=list(y=list(relation="free")) ) ## 4.3 An incomplete list of lattice Functions splom( ~ data.frame) # Scatterplot matrix bwplot(factor ~ numeric , . .) # Box and whisker plot qqnorm(numeric , . .) # normal probability plots dotplot(factor ~ numeric , . .) # 1-dim. Display stripplot(factor ~ numeric , . .) # 1-dim. Display barchart(character ~ numeric , . .) histogram( ~ numeric , . .) densityplot( ~ numeric , . .) # Smoothed version of histogram qqmath(numeric ~ numeric , . .) # QQ plot splom( ~ dataframe, . .) # Scatterplot matrix parallel( ~ dataframe, . .) # Parallel coordinate plots cloud(numeric ~ numeric * numeric, . .) # 3-D plot contourplot(numeric ~ numeric * numeric, . .) # Contour plot levelplot(numeric ~ numeric * numeric, . .) # Variation on a contour plot ## 4.4 Exercises