-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-multiple_plots.Rmd
315 lines (247 loc) · 6.51 KB
/
08-multiple_plots.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# Multiple plots
```{r, echo=FALSE}
sapdecay <- read.table(
"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/sapdecay.txt",
header=T
)
```
You may have noticed in one of the previous examples, that two plots were produced one on top of the other. This is a pretty easy thing to accomplish.
All we need to do is split the plotting area with the <tt>par(mfrow=c(x,y))</tt> command. As with subscripts, with this command <tt>x</tt> codes for the number of rows, and <tt>y</tt> represents the number of columns. Check <tt>?par</tt> for more information.
```{r multiple_plots-1}
par(mfrow = c(2,2))
with(
sapdecay,
plot(
x = x,
y = y,
xlab = "Time (hours)",
ylab = "Decay rate",
type = "l",
col = "red",
lwd = 2,
main = "Line"
)
)
with(
sapdecay,
plot(
x = x,
y = y,
xlab = "Time (hours)",
ylab = "Decay rate",
type = "o",
col = "green",
lwd = 1,
main = "Overplot"
)
)
with(
sapdecay,
plot(
x = x,
y = y,
xlab = "Time (hours)",
ylab = "Decay rate",
type = "h",
col = "blue",
lwd = 2,
main = "Histogram"
)
)
with(
sapdecay,
plot(
x = x,
y = y,
xlab = "Time (hours)",
ylab = "Decay rate",
type = "s",
col = "hotpink",
lwd = 2,
main = "Step"
)
)
# It's important that we reset the mfrow parameter at the end of plotting,
# otherwise all subsequent plots will appear on this four by four grid, which
# may not be what we want.
par(mfrow = c(1,1))
```
Using this method, it's also perfectly possible to combine plots of two different types, and plot them next to each other.
Let's imagine that we want to test whether a variable is normally distributed - there are two very simple graphical commands built into R that make this easier: <tt>qqnorm</tt> and <tt>qqline</tt>. These commands produce quantile-quantile plots for the normal distribution. Essentially if the data are normally distributed, they will conform to the straight line<a href="#footnote-1">[1]</a>.
First we will create a vector of random numbers drawn from a normal distribution - so we already know that they are normal!
```{r ,echo=-1,}
set.seed(1337)
normal_data <- rnorm(
100,
mean=0,
sd=1
)
normal_data
```
And now...some plots. First, two plots side by side.
```{r multiple_plots-2, fig.width=12}
par(
mfrow=c(1,2)
)
# Remember that the first number is the number of rows, the latter is the number
# of columns. So here, we have specified one row, two columns...
# Now the plots:
hist(normal_data)
qqnorm(normal_data)
qqline(normal_data)
```
So things should look pretty normal. In this example we have not specified a 'seed' (see <tt>?set.seed</tt> for more info). So every time that you run the code, you will get a different set of randomly generated numbers drawn from a normal distribution.
All well and good, but what about one above the other? Just changed the 1 and 2 around in the <tt>par(mfrow=c(x,y))</tt> command.
```{r multiple_plots-3, fig.height=12}
par(mfrow=c(2,1))
# Remember that the first number is the number of rows, the latter is the number
# of columns. So here, we have specified one row, two columns...
# Now the plots:
hist(normal_data)
qqnorm(normal_data)
qqline(normal_data)
```
<div class="ex">
### Exercise
Using what you have learnt in the previous sections, and the data from the <tt>car_prices</tt>, have a go at reproducing the plots you see below. Most of what you need to know has been covered, though you will need to consult the help files for certain commands.
</div>
```{r multiple_plots-4, fig.width=15, fig.height=15}
car_prices <- read.table(
"discount.csv",
header = T,
sep = ","
)
par(
mar = c( 4.1, 4.5, 0.5, 0.5),
mgp = c( 2.1, 0.6, 0),
mfrow = c(2,2),
cex = 1.4,
cex.lab = 1.2,
cex.axis = 1.2,
lend="square"
)
diesel <- subset(car_prices,fuel == "diesel")
petrol <- subset(car_prices,fuel == "petrol")
p1<-hist(
diesel$price,
ylim = c(0,60),
col = rgb(1,0,0,0.5), # Note that the colours used must be those that allow transparency
main = "",
breaks = 8,
xlab = bquote(Asking~price~(10^3~"£")),
xlim = c(5,5500),
yaxt = "n",
xaxt = "n"
)
# plot second graph - note use of 'add = TRUE' to overlay onto the first plot
p2<-hist(
petrol$price,
breaks = 8,
add = TRUE,
col = rgb(0,0,1,0.5)
)
axis(
side = 2,
at = seq(0,60,10),
las = 2
)
axis(
side = 1,
labels = seq(0,5,1),
at = seq(0,5000,1000),
las = 1
)
legend(
"topleft",
legend = c("Petrol","Diesel"),
fill = c(rgb(0,0,1,0.5),rgb(1,0,0,0.5)),
bty = "n"
)
boxplot(
split(
car_prices$price,
car_prices$fuel
),
col = c(rgb(1,0,0,0.5),rgb(0,0,1,0.5)),
ylab = bquote(Asking~price~(10^3~"£")),
yaxt = "n"
)
axis(
side = 2,
labels = seq(0,10,1),
at = seq(0,10000,1000),
las = 2
)
with(
petrol,
plot(
miles,
price,
col = rgb(0,0,1,0.5),
ylim = c(500,5000),
xlim=c(20000,180000),
xlab = bquote(Mileage~(10^3~miles)),
ylab = bquote(Asking~price~(10^3~"£")),
yaxt = "n",
xaxt = "n",
bty = "l",
pch = 16,
cex = 1.2
)
)
with(
diesel,
points(
miles,
price,
col = rgb(1,0,0,0.5),
pch = 17,
cex = 1.2
)
)
axis(
side = 2,
labels = seq(0,10,1),
at = seq(0,10000,1000),
las = 2
)
axis(
side = 1,
at = seq(0,200000,20000),
labels = seq(0,200,20)
)
legend(
"topright",
col = c(rgb(0,0,1,0.5),rgb(1,0,0,0.5)),
legend = c("Petrol","Diesel"),
pch = c(16:17),
bty = "n",
cex = 1.2
)
###############
qqnorm(
car_prices$miles,
main = "QQ plot of mileage"
)
qqline(
car_prices$miles,
col = "red",
lty = 2
)
# boxplot(
# split(
# car_prices$miles/car_prices$price,
# car_prices$fuel
# ),
# col = c(rgb(1,0,0,0.5),rgb(0,0,1,0.5)),
# ylab = bquote(Miles~"£"^-1),
# yaxt = "n"
# )
# axis(
# side = 2,
# at = seq(0,200,40),
# las = 2
# )
par(mfrow=c(1,1))
```
< id="footnote-1">[1] I'm not going to dwell on the reasons why this is so - we're more interested in the plotting graphs than the statistics behind it in this sections, but if you don't understand what is going on here, you should look it up! <a href="http://en.wikipedia.org/wiki/Q%E2%80%93Q_plot">http://en.wikipedia.org/wiki/Q-Q_plot</a>.