1# Copyright (C) 1997-2009 The R Core Team 2 3require(datasets) 4require(grDevices); require(graphics) 5 6## Here is some code which illustrates some of the differences between 7## R and S graphics capabilities. Note that colors are generally specified 8## by a character string name (taken from the X11 rgb.txt file) and that line 9## textures are given similarly. The parameter "bg" sets the background 10## parameter for the plot and there is also an "fg" parameter which sets 11## the foreground color. 12 13 14x <- stats::rnorm(50) 15opar <- par(bg = "white") 16plot(x, ann = FALSE, type = "n") 17abline(h = 0, col = gray(.90)) 18lines(x, col = "green4", lty = "dotted") 19points(x, bg = "limegreen", pch = 21) 20title(main = "Simple Use of Color In a Plot", 21 xlab = "Just a Whisper of a Label", 22 col.main = "blue", col.lab = gray(.8), 23 cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3) 24 25 26## A little color wheel. This code just plots equally spaced hues in 27## a pie chart. If you have a cheap SVGA monitor (like me) you will 28## probably find that numerically equispaced does not mean visually 29## equispaced. On my display at home, these colors tend to cluster at 30## the RGB primaries. On the other hand on the SGI Indy at work the 31## effect is near perfect. 32 33par(bg = "gray") 34pie(rep(1,24), col = rainbow(24), radius = 0.9) 35title(main = "A Sample Color Wheel", cex.main = 1.4, font.main = 3) 36title(xlab = "(Use this as a test of monitor linearity)", 37 cex.lab = 0.8, font.lab = 3) 38 39 40## We have already confessed to having these. This is just showing off X11 41## color names (and the example (from the postscript manual) is pretty "cute". 42 43pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12) 44names(pie.sales) <- c("Blueberry", "Cherry", 45 "Apple", "Boston Cream", "Other", "Vanilla Cream") 46pie(pie.sales, 47 col = c("purple","violetred1","green3","cornsilk","cyan","white")) 48title(main = "January Pie Sales", cex.main = 1.8, font.main = 1) 49title(xlab = "(Don't try this at home kids)", cex.lab = 0.8, font.lab = 3) 50 51 52## Boxplots: I couldn't resist the capability for filling the "box". 53## The use of color seems like a useful addition, it focuses attention 54## on the central bulk of the data. 55 56par(bg="cornsilk") 57n <- 10 58g <- gl(n, 100, n*100) 59x <- rnorm(n*100) + sqrt(as.numeric(g)) 60boxplot(split(x,g), col="lavender", notch=TRUE) 61title(main="Notched Boxplots", xlab="Group", font.main=4, font.lab=1) 62 63 64## An example showing how to fill between curves. 65 66par(bg="white") 67n <- 100 68x <- c(0,cumsum(rnorm(n))) 69y <- c(0,cumsum(rnorm(n))) 70xx <- c(0:n, n:0) 71yy <- c(x, rev(y)) 72plot(xx, yy, type="n", xlab="Time", ylab="Distance") 73polygon(xx, yy, col="gray") 74title("Distance Between Brownian Motions") 75 76 77## Colored plot margins, axis labels and titles. You do need to be 78## careful with these kinds of effects. It's easy to go completely 79## over the top and you can end up with your lunch all over the keyboard. 80## On the other hand, my market research clients love it. 81 82x <- c(0.00, 0.40, 0.86, 0.85, 0.69, 0.48, 0.54, 1.09, 1.11, 1.73, 2.05, 2.02) 83par(bg="lightgray") 84plot(x, type="n", axes=FALSE, ann=FALSE) 85usr <- par("usr") 86rect(usr[1], usr[3], usr[2], usr[4], col="cornsilk", border="black") 87lines(x, col="blue") 88points(x, pch=21, bg="lightcyan", cex=1.25) 89axis(2, col.axis="blue", las=1) 90axis(1, at=1:12, lab=month.abb, col.axis="blue") 91box() 92title(main= "The Level of Interest in R", font.main=4, col.main="red") 93title(xlab= "1996", col.lab="red") 94 95 96## A filled histogram, showing how to change the font used for the 97## main title without changing the other annotation. 98 99par(bg="cornsilk") 100x <- rnorm(1000) 101hist(x, xlim=range(-4, 4, x), col="lavender", main="") 102title(main="1000 Normal Random Variates", font.main=3) 103 104 105## A scatterplot matrix 106## The good old Iris data (yet again) 107 108pairs(iris[1:4], main="Edgar Anderson's Iris Data", font.main=4, pch=19) 109pairs(iris[1:4], main="Edgar Anderson's Iris Data", pch=21, 110 bg = c("red", "green3", "blue")[unclass(iris$Species)]) 111 112 113## Contour plotting 114## This produces a topographic map of one of Auckland's many volcanic "peaks". 115 116x <- 10*1:nrow(volcano) 117y <- 10*1:ncol(volcano) 118lev <- pretty(range(volcano), 10) 119par(bg = "lightcyan") 120pin <- par("pin") 121xdelta <- diff(range(x)) 122ydelta <- diff(range(y)) 123xscale <- pin[1]/xdelta 124yscale <- pin[2]/ydelta 125scale <- min(xscale, yscale) 126xadd <- 0.5*(pin[1]/scale - xdelta) 127yadd <- 0.5*(pin[2]/scale - ydelta) 128plot(numeric(0), numeric(0), 129 xlim = range(x)+c(-1,1)*xadd, ylim = range(y)+c(-1,1)*yadd, 130 type = "n", ann = FALSE) 131usr <- par("usr") 132rect(usr[1], usr[3], usr[2], usr[4], col="green3") 133contour(x, y, volcano, levels = lev, col="yellow", lty="solid", add=TRUE) 134box() 135title("A Topographic Map of Maunga Whau", font= 4) 136title(xlab = "Meters North", ylab = "Meters West", font= 3) 137mtext("10 Meter Contour Spacing", side=3, line=0.35, outer=FALSE, 138 at = mean(par("usr")[1:2]), cex=0.7, font=3) 139 140## Conditioning plots 141 142par(bg="cornsilk") 143coplot(lat ~ long | depth, data = quakes, pch = 21, bg = "green3") 144 145par(opar) 146