Lab02
Lab02
Basic Plotting
Principles of Statistics
1 / 28
R Graphics
2 / 28
Using Plot with Coordinate Vectors
3 / 28
foo <- c(1.1, 2, 3.5, 3.9, 4.2)
bar <- c(2, 2.2, -1.3, 0, 0.2)
plot(foo, bar)
2.0
1.5
1.0
0.5
bar
0.0
−0.5
−1.0
foo
4 / 28
Graphical Parameters
5 / 28
▶ pch (point character): selects which character to use for
plotting individual points
6 / 28
Automatic Plot Types (type)
▶ type="p" (points only), "l" (lines only), "b" (both points
and lines), "o" (overplotting the points with lines), "n" (no
points or lines plotted)
par(mfrow=c(1,2))
plot(foo,bar,type="l");plot(foo,bar,type="b")
2.0
2.0
1.5
1.5
1.0
1.0
0.5
0.5
bar
bar
0.0
0.0
−0.5
−0.5
−1.0
−1.0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0
foo foo
7 / 28
Title and Axis Labels (main, xlab, ylab)
par(mfrow=c(1,2))
plot(foo,bar,type="o",main="My plot",
xlab="x axis label", ylab="location y")
plot(foo,bar,type="b",main="My plot\n title on two lines",
xlab="", ylab="")
My plot
My plot
title on two lines
2.0
2.0
1.5
1.5
1.0
1.0
location y
0.5
0.5
0.0
0.0
−0.5
−0.5
−1.0
−1.0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0
x axis label
8 / 28
Color (col; can make data much clearer)
par(mfrow=c(1,2))
plot(foo,bar,type="b",main="My plot",xlab="",ylab="",col=2)
plot(foo,bar,type="b",main="My plot",xlab="",ylab="",col="seagreen4")
My plot My plot
2.0
2.0
1.5
1.5
1.0
1.0
0.5
0.5
0.0
0.0
−0.5
−0.5
−1.0
−1.0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0
9 / 28
Note: You can set colors with the col parameter in a number of
ways. There are eight possible integer values. Also, you can specify
colors using RGB (red, green, and blue) levels and by creating your
own palettes.
10 / 28
Line and Point Appearances (pch, lty, cex, lwd)
par(mfrow=c(1,2))
plot(foo,bar,type="b",main="My plot",xlab="",
ylab="",col=4,pch=8,lty=2,cex=2.3,lwd=3.3)
plot(foo,bar,type="b",main="My plot",xlab="",
ylab="",col=6,pch=15,lty=3,cex=0.7,lwd=2)
My plot My plot
2.0
2.0
1.5
1.5
1.0
1.0
0.5
0.5
0.0
0.0
−0.5
−0.5
−1.0
−1.0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0
11 / 28
Plotting Region Limits (xlim, ylim)
par(mfrow=c(1,2))
plot(foo,bar,type="b",main="My plot",xlab="",ylab="",
col=4,pch=8,lty=2,cex=2.3,lwd=3.3,xlim=c(-10,5),ylim=c(-3,3))
plot(foo,bar,type="b",main="My plot",xlab="",ylab="",
col=6,pch=15,lty=3,cex=0.7,lwd=2,xlim=c(3,5),ylim=c(-0.5,0.2))
My plot My plot
0.2
3
0.1
2
0.0
1
−0.1
0
−0.2
−1
−0.3
−2
−0.4
−0.5
−3
12 / 28
Adding Points, Lines, and Text to an Existing Plot
Each call to plot will refresh the active graphics device for a new
plotting region.
Here are some useful, ready-to-use functions that will add a plot
without refreshing or clearing the window:
▶ points (add points)
▶ lines, abline, segments (add lines)
▶ text (writes text), arrows (add arrows)
▶ legend (adds a legend)
13 / 28
x <- 1:20
y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,
-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
plot(x,y,type="n",main="")
abline(h=c(-5,5),col="red",lty=2,lwd=2)
segments(x0=c(5,15),y0=c(-5,-5),x1=c(5,15),y1=c(5,5),
col="red",lty=3,lwd=2)
15
10
5
y
0
−5
−10
−15
5 10 15 20
14 / 28
plot(x,y,type="n",main="")
points(x[y>=5],y[y>=5],pch=4,col="darkmagenta",cex=2)
points(x[y<=-5],y[y<=-5],pch=3,col="darkgreen",cex=2)
points(x[(x>=5&x<=15)&(y>-5&y<5)],y[(x>=5&x<=15)&(y>-5&y<5)],
pch=19,col="blue")
points(x[(x<5|x>15)&(y>-5&y<5)],y[(x<5|x>15)&(y>-5&y<5)])
15
10
5
y
0
−5
−10
−15
5 10 15 20
15 / 28
plot(x,y,type="n",main="")
lines(x,y,lty=4)
arrows(x0=8,y0=14,x1=11,y1=2.5)
text(x=8,y=15,labels="sweet spot")
15
sweet spot
10
5
y
0
−5
−10
−15
5 10 15 20
16 / 28
plot(x,y,type="b",main="")
legend("bottomleft",
legend=c("overall process","sweet","standard",
"too big","too small","sweet y range","sweet x range"),
pch=c(NA,19,1,4,3,NA,NA),lty=c(4,NA,NA,NA,NA,2,3),
col=c("black","blue","black","darkmagenta","darkgreen","red","red"),
lwd=c(1,NA,NA,NA,NA,2,2),pt.cex=c(NA,1,1,2,2,NA,NA))
15
10
5
y
0
−5
overall process
sweet
−10
standard
too big
too small
sweet y range
−15
sweet x range
5 10 15 20
17 / 28
My final plot
15
sweet spot
10
5
y
0
−5
overall process
sweet
−10
standard
too big
too small
sweet y range
−15
sweet x range
5 10 15 20
18 / 28
The ggplot2 Package
19 / 28
A Quick Plot with qplot
▶ You must install ggplot2 package.
▶ ggplot2 plots are stored as objects, meaning that they have
an underlying, static representation until you change the
object.
20 / 28
foo <- c(1.1, 2, 3.5, 3.9, 4.2)
bar <- c(2, 2.2, -1.3, 0, 0.2)
qplot(foo, bar, main="My qplot",
xlab="x axis label", ylab="location y")
My qplot
1
location y
−1
1 2 3 4
x axis label
21 / 28
Setting Appearance Constants with Geometric Modifiers
▶ To customize ggplot2 graphic, you alter the object itself.
▶ First store the qplot object you created earlier and then use
geom_point and geom_line with different style.
1
bar
−1
1 2 3 4
foo
22 / 28
qplot(foo,bar,geom="blank") +
geom_point(size=3,shape=6,color="blue") +
geom_line(color="red",linetype=2)
1
bar
−1
1 2 3 4
foo
23 / 28
myqplot <- qplot(foo,bar,geom="blank") +
geom_line(color="red",linetype=2)
a <- myqplot + geom_point(size=3,shape=3,color="blue")
b <- myqplot + geom_point(size=4,shape=7,color="green")
grid.arrange(a,b, nrow=1, ncol=2) # gridExtra package
2 2
1 1
bar
bar
0 0
−1 −1
1 2 3 4 1 2 3 4
foo foo
24 / 28
Aesthetic Mapping with Geoms
▶ Geoms and ggplot2 provide efficient, automated ways to
apply different styles to different subsets of a plot.
▶ ggplot2 apply particular styles to different categories using a
factor object.
▶ The factor that holds these categories is called a variable,
which ggplot2 can map to aesthetic values.
Note: The above gets rid of much of the effort that goes into
isolating subsets of data and plotting them separately using base R
graphics.
25 / 28
▶ Based on x and y values, define several categories.
x <- 1:20
y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,
-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
ptype <- rep(NA,length(x=x))
ptype[y>=5] <- "too_big"
ptype[y<=-5] <- "too_small"
ptype[(x>=5&x<=15)&(y>-5&y<5)] <- "sweet"
ptype[(x<5|x>15)&(y>-5&y<5)] <- "standard"
ptype <- factor(x=ptype)
ptype
## [1] standard standard standard standard sweet sweet too_
## [8] too_big sweet too_small too_small too_small sweet too_
## [15] too_big standard too_small too_small standard too_big
## Levels: standard sweet too_big too_small
26 / 28
a <- qplot(x,y,color=ptype,shape=ptype)
b <- qplot(x,y,color=ptype,shape=ptype) + geom_point(size=4) +
geom_line(mapping=aes(group=1),color="black",lty=2) +
geom_hline(mapping=aes(yintercept=c(-5,5)),color="red") +
geom_segment(mapping=aes(x=5,y=-5,xend=5,yend=5),color="red",lty=3) +
geom_segment(mapping=aes(x=15,y=-5,xend=15,yend=5),color="red",lty=3)
grid.arrange(a,b, nrow=1, ncol=2) # gridExtra package
10 10
ptype ptype
standard standard
sweet sweet
y
y
0 0
too_big too_big
too_small too_small
−10 −10
5 10 15 20 5 10 15 20
x x
27 / 28
Reference
28 / 28