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