07 Graphics
07 Graphics
Graphics
Graphics
• Box-and-Whisker Plots
• Scatter plots
• Surface Plots
• Page Layout
• Color Management
Simple Plotting
plot(x, y)
> x = 1:10
> y = c(0.45, 1.46, 1.94, 1.45, 2.35,
2.87, 2.39, 2.69, 3.85, 4.07)
> plot(x, y,
xlab = "A Custom x label",
ylab = "A Custom y label",
main = "A Basic Scatter Plot")
A Basic Scatter Plot
●
4
3 ●
A Custom y label
●
●
● ●
2
● ●
1
2 4 6 8 10
A Custom x label
Range Customisation
●
4
●
A Custom y label
●
●
● ●
2
● ●
1
●
0
2 4 6 8 10
A Custom x label
Different Types of Plot
●
● ●
●
●
●
●
● ●
● ● ● ●
● ●
● ●
● ●
● ●
● ●
● ●
More Plot Types
> plot.new()
> plot.window(xlim = range(x), ylim = c(0, 6))
> points(x, y)
> axis(1)
> axis(2)
> box()
> title(xlab = "A Custom x label")
> title(ylab = "A Custom y label")
(i) Axes which cross at (0, 0) rather than lying at the sides
of the plotting region.
−2 −1 1 2
−5
−10
A Cubic Polynomial
Axis Customisation
We start by producing the the data for the curve. (1001 points
may be overkill here.)
> x = seq(-1.85, 1.85, length = 1001)
> y = x * (2 * x - 3) * (2 * x + 3)
Now we set up the plot and draw the actual curve.
> plot.new()
> plot.window(xlim=c(-2, 2), ylim = c(-10, 10))
> lines(x, y)
Finally, we draw the custom axes and annotation.
> axis(1, pos = 0, at = c(-2, -1, 1, 2))
> axis(2, pos = 0, at = c(-10, -5, 5, 10),
las = 1)
> title(xlab = "A Cubic Polynomial")
Low-Level Drawing Functions
> points(x, y)
Things that can be changed are the plotting symbols, the size
of the plotting symbols and the colors that are used to draw the
symbols.
Customising Plotting Symbols
● ● ●
● ●
●
2
●
● ●
● ●
● ● ● ●
●● ●
1
● ●● ●
● ● ●
●
● ●● ● ● ● ● ●
●● ●●
yvals
● ● ● ●
● ●
●
●
0
● ●● ● ●
● ● ● ●
● ● ●
● ● ● ● ●
● ● ●
● ●
● ● ● ●
● ●● ●
−1
● ● ●●
● ● ● ● ●
● ● ●
● ●
● ● ● ●
● ●
●
−2
● ●
● ●
●
0 20 40 60 80 100
xvals
Drawing Connected Line Segments
> lines(x, y)
The type, lwd, lty and col arguments can be used together
to generate a variety of line effects.
Note that in this plot, the size of dot and the gap between them
have both been scaled by the value of lwd.
A Thick Dotted Line
2.0
1.5
1.0
rnorm(10)
0.5
0.0
−0.5
2 4 6 8 10
Index
Drawing Segments and Arrows
11
10
9
●
8
7
6
5
4
1 2 3 4 5
An Alternative Display
11
●
●
●
●
10
●
● ●
● ● ●
●
● ●
● ●
●
9
●
● ●
● ●
● ●
●
● ●
● ● ● ●
●
8
● ●
●
Values
● ●
●
●
● ●
● ● ●
●
● ●
●
7
● ●
●
● ●
● ●
● ●
●
● ● ●
●
●
6
● ●
● ●
●
● ●
● ●
●
●
●
● ●
5
●
● ●
●
●
●
●
●
4
1 2 3 4 5
Group
Jittering the Points
11
●
●
●
●●
10
●
●● ●
● ● ●
●● ●
●● ●
●
9
●
● ●
●
● ●
●● ●● ●●
● ● ● ●
●
8
● ●
●
Values
●● ●
●
●
● ● ●
● ● ●●
● ●●
●
7
● ●
●
●●
● ●
● ●
● ●
●● ● ●●
●●
6
●● ●●
●● ●
●● ●
● ● ●
●●
●
● ●
5
●●
●● ●
●
●●
●
●
4
●●
1 2 3 4 5
Group
Area Fill Primitives
10
8
6
4
2
0
−10 −5 0 5 10
Creating the Display
The figure at the center of the array is drawn with a single call
to rect. It creates 10 rectangles, going bottom to top.
xl = -(10:1)
xr = 10:1
yb = 0:9
yt = 1:10
rect(xl, yb, xr, yt, col = "tan")
Examples:
• Etc.
A Custom Time Series Display
0.6
0.4
Temperature Anomaly
0.2
0.0
−0.2
Year
Producing the Display
The polygon is drawn going below the x axis and the part
below the axis is “clipped” out.
hcl(h)
hue
Other Ways to Specify Colour
rgb(r, g, b)
hsv(h, s, v)
> n = 17
> theta = seq(0, 2 * pi, length = n + 1)[1:n]
> x = sin(theta)
> y = cos(theta)
> plot.new()
> plot.window(xlim = c(-1, 1),
ylim = c(-1, 1),
asp = 1)
> for(i in 2:n)
for(j in 1:(i - 1))
segments(x[i], y[i], x[j], y[j])
> title(main="A 17-Rosette")
A 17−Rosette
Spiral Squares
and the passing the i-th colour to the polygon function with
(In this example the s and v arguments take their default value
of 1.)
A Spiral Square
Drawing Text in Plots
text(x, y, labels)
r=
,0
−4
5
0.
5,
c(
ad
j=
j
ad
=
c(
,
0.
45
5
r=
,0
.5
st
)
str = 0, adj = c(0.5 ,0.5)
Two
GHI
DEF
ABC
One Three
Two
Eye-Catching Visual “Tricks”
● ●
● ●
5
●
●
●
●
●
●
4
●
● ●
●
Y Coordinates
●
3
● ●
2
● ● ● ●
●
●
●
1
● ●
0
●
−1
0 5 10 15 20 25 30
X Coordinates
Drop Shadows
The shadow for the line can be drawn with the expression
●
●
6 ●
●
●
● ●
●
● ●
5 ●
●
●
●
● ●●● ●
●
4 ●
●
●
●
●
●●●●
●●●
3 ●
● ●
●
● ●
●
● ●
●
2 ●
● ● ●
●
●
1 ●
●
● ●
●
●
0 ●
● ●
●
●
●
0 5 10 15 20 25 30
Lamb
Mutton
Pigmeat
Poultry
Beef
0 10 20 30 40 50
6
5
4
3
y
2
1
0
0 5 10 15 20 25 30
x
Textured Line Example
2
1
0
0 5 10 15 20 25 30
x
Colour Gradients
0.6
0.4
0.2
0.0
−0.2
Time
The par Function
Margin 2
Margin 4
Plot Region
Margin 1
Controlling The Margins
The par function can be used for querying the margin sizes.
> par("mar")
[1] 5.1 4.1 4.1 2.1
> par("mai")
[1] 1.02 0.82 0.82 0.42
> par("pin")
[1] 5.76 5.16
Multifigure Layouts
rnorm(10)
1.0
0 ● ●
0.0
−2
2 4 6 8 10 2 4 6 8 10
Index Index
● ● ●
1
● ● ●
● ●
0.5
● ●
●
rnorm(10)
rnorm(10)
0
●
●
−0.5
●
−2
● ●
●
−1.5
● ● ●
2 4 6 8 10 2 4 6 8 10
Index Index
Outer Margins
The size of outer margins can be specified with the oma or omi
arguments to par.
Figure 1 Figure 2
Outer Margin 2
Outer Margin 4
Figure 3 Figure 4
Outer Margin 1
Eliminating Waste Margin Space
Here we have trimmed space from the top of each plot, and
placed a 4 line outer margin at the top of of the layout.
Plots with Margins Trimmed
0.5
●
1.0 ●
rnorm(10)
rnorm(10)
−0.5
●
● ●
0.0
●
●
●
−1.5
●
−1.5
2 4 6 8 10 2 4 6 8 10
Index Index
1.5
● ● ●
● ●
0.5
● ● ●
●
0.5
●
rnorm(10)
rnorm(10)
●
0.0
●
●
−0.5
●
● ●
●
●
−1.0
−1.5
● ●
2 4 6 8 10 2 4 6 8 10
Index Index
An Example: Scatterplot Matrices
● ● ●
● ● ●
● ●● ●
● ● ●● ●●
●● ●● ●●● ● ●
● ●● ● ● ●
● ●● ● ●● ● ●● ●
●●● ● ●
●●● ●● ●
● ● ●●● ● ● ●● ●●●
●● ● ●● ●●● ● ●●
●● ● ● ●● ● ●● ● ● ● ● ●
● ●● ● ● ●● ●●● ● ●●
●● ●●● ●● ●● ● ● ●● ● ● ●
3.0
● ●● ● ●●
●● ●●● ● ●● ●●● ●●●● ●● ●● Sepal.Width ●●
●
●●
●
●
●●
● ●●● ●
●
●● ●
●●
●
●
●●● ●● ● ●●●
●● ●● ● ● ●●
●●●●●●● ●●●●
● ●● ●●●●● ● ● ● ● ● ●●●● ● ● ● ●●● ●
●●● ●●●●● ● ● ● ●●● ●●●●● ● ● ● ●●●● ●●●●● ●
● ● ● ● ●● ●●● ●●● ● ●●● ● ●●
● ●● ● ● ● ●● ● ● ● ● ● ●
● ● ●●● ● ● ● ●● ●● ● ● ● ● ● ●●●●
● ● ●● ● ●●
● ● ● ● ● ● ●● ● ● ●
●● ● ● ● ● ●
2.0
● ● ●
1 2 3 4 5 6 7
● ● ● ●●●●
●● ● ● ●
●
●●● ● ●●● ● ●●● ● ●
● ● ●● ●● ● ●
●● ●
● ● ●● ●
●● ●
●●●● ● ●
●●● ●● ●●●●●●● ● ● ●
● ●●
● ●●●
●●●
●●●● ●● ●●● ●
●● ●●●
●● ●
●●
●
●●●●
●●
● ●
●●
●●●
●●●●●●●●
● ● ●
● ●●
●
●
●
●
●●
●●
●●
●
●●
●●● ●●●
●●●●
●
● ●●
●
●●● ●
●●● ● ● ●
● ●●●● ●● ●● ●● ●● ●● ●
●●●
●●
● ● ●
●●
●●
● ●
●
● ●●●
●●
● ●●●●● ●
●●
● ●●●● ●
●●
●●●
●●
●●● ●●
●●●
● ●
●●
● ●
●● ● ● Petal.Length ●●●●
●
●● ● ● ●● ● ●
●
● ● ●
● ●●●●●
● ● ●●
●● ●
●● ●
●●●
●●●
●●●●●
●●●
●●
●●
●● ● ●●
●● ●● ●●
●●●●
●●●
● ●
●● ●
●●●●●●●● ●
● ●
●●
●
●●
●●●
●●
●
●●●●● ● ● ●
● ● ● ●●
●
●
●
2.5
● ● ● ● ● ●●●
● ● ● ● ● ● ● ●
● ● ●●● ● ● ●●● ● ●
●●● ●●● ●
●● ● ● ● ● ●● ●
● ●●● ● ● ● ●● ● ●
●●●● ●
●● ● ●● ● ● ● ● ● ●●●
● ●●
● ●● ● ● ●● ●●● ●
●●●●●●● ● ●● ● ●●●●●● ●
●● ● ●●● ●
● ● ● ● ● ●
1.5
● ● ● ● ● ●● ●● ● ●
● ● ●● ●●●● ● ● ● ● ●●●●● ●● ●●●●●
● ● ●●● ● ●●●●●●● ● ●● ●● ●
●●● ●●●● ● ● ● ●●●● ●● ●
●●●
●●
● ●●
● ●● ●
●●
●●● ●
● ●
●
●
●●● ● Petal.Width
●● ● ●● ● ● ●●● ●● ●●● ●●
0.5
● ● ●
● ● ●
●● ● ● ● ●●● ● ●● ●
●●
●● ● ●● ● ● ● ●● ● ●
●●
●●
● ●●●●●●●●●● ● ●●●●
●●●●●●●● ● ● ●●●
●●●
●●
● ●● ● ●● ● ● ●●●
par(opar)
Drawing The Scatterplots
for(i in 1:n)
for(j in 1:n) {
xrange = range(x[,j])
yrange = range(x[,i])
plot.new()
plot.window(xlim = xrange, ylim = yrange)
if (i == j)
text(mean(xrange), mean(yrange),
colnames(x)[j])
else points(x[,j], x[,i])
box()
}
Drawing Axes
Only axes at the edge of the plot are drawn, and then only
alternate axes are drawn to avoid label collisions.
if (i == 1 && j %% 2 == 0)
axis(3)
if (i == n && j %% 2 == 1)
axis(1)
if (j == 1 && i %% 2 == 0)
axis(2)
if (j == n && i %% 2 == 1)
axis(4)
Printing a Title
This draws the text in the (middle of the) third line of the third
outer margin, in bold font.
Final Cleanup
par(opar)
Customisation
points(x[,j], x[,i])
pairs(x,
panel = function(x, y, ...) {
points(x, y)
abline(lm(y ~ x))
})
Using par
> par()
> names(par())
The Full Set of Graphical Parameters
xlog ylog adj ann ask
bg bty cex cex.axis cex.lab
cex.main cex.sub cin col col.axis
col.lab col.main col.sub cra crt
csi cxy din err family
fg fig fin font font.axis
font.lab font.main font.sub gamma lab
las lend lheight ljoin lmitre
lty lwd mai mar mex
mfcol mfg mfrow mgp mkh
new oma omd omi pch
pin plt ps pty smo
srt tck tcl usr xaxp
xaxs xaxt xpd yaxp yaxs
yaxt
Important Groups of Graphics Parameters
Setting the value of font controls the default font of the text
elements in graphs.
The value of font is 1 for normal font, 2 for bold, 3 for italic
and 4 for bold-italic.
An Example
● ● ●
●
● ●●● ● ● ●● ● ●
rnorm(100)
● ●● ●● ● ●●
● ●● ● ● ● ●
●●● ●
● ●● ●●
0
●
● ● ● ●● ● ●●● ●
● ●● ● ●● ●
● ● ●
●
●● ● ●
● ●
−1
● ● ● ● ●
● ●
● ●●
●
−2
0 20 40 60 80 100
Index
All Those Other pars . . .
> ?par
> help("par")
More Flexible Layouts
All the panels in the plot have exacly the same size.
4 5 6
7 8 9
Combining Figures
In the next arrangement, the 4th and 5th figures of the previous
layout are combined into a single figure.
4 5
6 7 8
Arguments to Layout
The second layout combined the 4-th and 5-th figures from
this layout as follows
The specification
widths = c(lcm(1), 1, 2)
says that the first column is exactly 1cm wide and the third
column is twice the width of the second.
All par values which work within figures (such as mar and
mai) still have an effect.
● ●
3
●
●
2 ● ●
●● ●
● ● ● ●
●
● ●● ● ●
●
1 ● ●
● ● ●●
● ● ●
● ● ● ●
● ● ● ●
y
● ● ● ● ●
● ●● ●●
0 ● ● ● ●●● ● ● ● ● ●
● ●● ●● ●
● ●● ● ● ● ● ●
● ● ● ●
● ● ●
●● ● ● ●
● ● ●
●
−1 ● ●
●
●
●
● ● ●
−2 ●
●
● ●
−3
● ●
−3 −2 −1 0 1 2
Choosing a Layout
We need a big figure for the scatterplot, regions along its top
and right for the scatterplots and a figure at the very top for a
title.
> layout(rbind(c(0,4,4,0),
c(0,2,0,0),
c(0,1,3,0),
c(0,0,0,0)),
height = c(lcm(2), lcm(2), 1, lcm(2)),
width = c(lcm(2), 1, lcm(2), lcm(1)))
> layout.show(4)
> box("outer", lty = "dotted")
4
1 3
Producing the Graph
● ●
3
●
●
2 ● ●
●● ●
● ● ● ●
●
● ●● ● ●
●
1 ● ●
● ● ●●
● ● ●
● ● ● ●
● ● ● ●
y
● ● ● ● ●
● ●● ●●
0 ● ● ● ●●● ● ● ● ● ●
● ●● ●● ●
● ●● ● ● ● ● ●
● ● ● ●
● ● ●
●● ● ● ●
● ● ●
●
−1 ● ●
●
●
●
● ● ●
−2 ●
●
● ●
−3
● ●
−3 −2 −1 0 1 2
A Two-Panel Plot with Labelling
> layout(matrix(c(0,0,0,0,0,
0,1,3,2,0,
0,0,0,0,0), nc = 5, byrow = TRUE),
widths = c(lcm(2), 1, lcm(2), 1, lcm(2)),
heights = c(lcm(2), 1, lcm(2)))
> layout.show(3)
> box("outer", lty = "dotted")
Note that this just a start. You need extra panels for the
labelling at the top and bottom, and some space for the axes to
appear in.
1 3 2
New Zealand Population (1996 Census)
Male Female
95+
90−94
85−89
80−84
75−79
70−74
65−69
60−64
55−59
50−54
45−49
40−44
35−39
30−34
25−29
20−24
15−19
10−14
5−9
0−4
4 3 2 1 0 0 1 2 3 4
Percent of Population
Layouts and Plots
> layout(matrix(c(0, 0, 0,
0, 1, 0,
0, 0, 0), nc = 3, byrow = TRUE),
widths = c(lcm(2), 1, lcm(2)),
heights = c(lcm(2), 1, lcm(2)))
> layout.show(1)
1
Choice of Margin Size
> llines =
function(x)
lcm(x * par("csi") * 2.54)
●
1.0
●
●
●
0.5
●
0.0
●
−0.5
2 4 6 8 10
Axis Labels
The plot labels are produced by moving to the region for the
label (with plot.new) and drawing the text at the centre of the
region.
> plot.new()
> text(0.5, 0.5, "An X Label")
> plot.new()
> text(0.5, 0.5, "A Y Label", srt = 90)
A Label for the Y Axis
−1.0 −0.5 0.0 0.5 1.0
●
●
2
●
●
4
●
6
A Label for the X Axis
●
8
●
●
10
Other Features
Cheetah ●
Pronghorn Antelope ●
Thomson's Gazelle ●
Lion ●
Wildebeest ●
Quarterhorse ●
Cape Hunting Dog ●
Elk ●
Coyote ●
Gray Fox ●
Mongolian Wild Ass ●
Zebra ●
Hyena ●
Greyhound ●
Whippet ●
Jackal ●
Mule Deer ●
Rabbit (domestic) ●
Giraffe ●
Reindeer ●
Cat (domestic) ●
Grizzly Bear ●
Wart Hog ●
White−Tailed Deer ●
Human ●
Elephant ●
Black Mamba Snake ●
Six−Lined Race Runner ●
Wild Turkey ●
Squirrel ●
Pig (domestic) ●
Chicken ●
Spider (Tegenaria atrica) ●
●
Giant Tortoise ● ●
Three−Toed Sloth ● ●
Garden Snail ● ●
0 2 25 50 75 100
Speed (km/hr)