-
Notifications
You must be signed in to change notification settings - Fork 3
/
03-notes.Rmd
440 lines (310 loc) · 12 KB
/
03-notes.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
---
layout: topic
title: Data visualization with ggplot2 (notes)
author: Data Carpentry contributors
---
```{r, echo=FALSE}
knitr::opts_chunk$set(results='hide', fig.path='img/r-lesson-notes-', results='hide', eval=FALSE)
```
```{r, data cleaning, echo=FALSE}
library(dplyr)
surveys <- read.csv("http://kbroman.org/datacarp/portal_data_joined.csv")
surveys_complete <- surveys %>%
filter(species_id != "", !is.na(weight)) %>%
filter(!is.na(hindfoot_length), sex != "")
species_counts <- surveys_complete %>%
group_by(species_id) %>%
tally
frequent_species <- species_counts %>%
filter(n >= 10) %>%
select(species_id)
reduced <- surveys_complete %>%
filter(species_id %in% frequent_species$species_id)
```
Load the cleaned/reduced data:
```{r load_clean_data, eval=FALSE}
reduced <- read.csv("http://kbroman.org/datacarp/portal_data_reduced.csv")
```
Or:
```{r download_then_load, eval=FALSE}
download.file("http://kbroman.org/datacarp/portal_data_reduced.csv",
"CleanData/portal_data_reduced.csv")
reduced <- read.csv("CleanData/portal_data_reduced.csv")
```
## load libraries
```{r, load_lib}
library(ggplot2)
library(dplyr)
```
**Mention ggplot vs base graphics**
First plot:
```{r first-ggplot}
ggplot(reduced, aes(x = weight, y = hindfoot_length)) + geom_point()
```
Key concepts of "grammar of graphics":
- _aesthetics_ map features of the data to features of the
visualization
(for example, the `weight` variable is mapped to the y-axis coordinate)
- _geoms_ concern what actually gets plotted (here, each row in the data
becomes a point in the plot)
- `ggplot()` creates a graphics objects
- additional controls added with the `+` operator
- actual plot made when object is printed
```{r exlicit_printing, results='hide'}
p1 <- ggplot(reduced, aes(x=weight, y=hindfoot_length))
p2 <- p1 + geom_point()
print(p2)
```
Can be convenient to have saved the results. For example, to make that
last plot have `weight` on the log scale:
```{r weight_on_log_scale}
p2 + scale_x_log10()
```
and on square-root scale:
```{r weight_on_sqrt_scale}
p2 + scale_x_sqrt()
```
### Challenge
Make a scatterplot of `hindfoot_length` vs `weight`, but only for the
`species_id`, `"DM"`.
## Other aesthetics
For scatterplot, `shape`, `size`, `color`, and `alpha`.
```{r create-ggplot-object}
surveys_plot <- ggplot(reduced, aes(x = weight, y = hindfoot_length))
surveys_plot + geom_point(alpha = 0.1)
surveys_plot + geom_point(alpha = 0.1, color = "slateblue")
surveys_plot + geom_point(alpha = 0.1, color = "slateblue", size=0.5)
```
Things get more interesting when we assign these aesthetics to data.
```{r scatter_colored_by_species}
surveys_plot + geom_point(aes(color = species_id))
```
### Challenge
Use dplyr to calculate the mean `weight` and `hindfoot_length` as well
as the sample size for each species.
Make a scatterplot of mean `hindfoot_length` vs `mean_weight`, with
the sizes of the points corresponding to the sample size.
## Layers
You can use `geom_line()` to make a line plot. For example, let's plot
the counts of animals by year.
```{r plot_counts_by_year}
count_by_year <- reduced %>%
group_by(year) %>%
tally
p <- ggplot(count_by_year, aes(x=year, y=n))
p + geom_line()
```
You can use _both_ `geom_line` and `geom_point` to make a line plot
with points at the data values.
```{r line_plus_point}
p + geom_line() + geom_point()
```
**layers**: a given plot can have multiple layers of geometric
objects, plotted one on top of another.
If we add colors, we'll see this better.
```{r line_plus_point_wcolors}
p + geom_line(color="lightblue") + geom_point(color="violetred")
```
If we switch the order of `geom_point` and `geom_line`, we'll reverse
the layers.
```{r line_plus_point_wcolors_alt}
p + geom_point(color="violetred") + geom_line(color="lightblue")
```
**global aesthetics**: esthetics included in the call to `ggplot()` (or completely
separately) are made to be the defaults for all layers, but we can
separately control the aesthetics for each layer. For example, we
could color the points by year:
```{r points_colored_by_year}
p + geom_line() + geom_point(aes(color=year))
```
Compare that to the following:
```{r points_colored_by_year_2}
p + geom_line() + geom_point() + aes(color=year)
```
### Challenge
Make a plot of counts of `species_id` `"DM"` and `"DS"` by year.
## Groups
Suppose, in that last challenge, we'd wanted to have black lines but
the points colored by species. We might have done this:
```{r misgrouped}
counts_dm_ds <- reduced %>% filter(species_id %in% c("DM", "DS")) %>%
group_by(species_id, year) %>% tally
p <- ggplot(counts_dm_ds, aes(x=year, y=n))
p + geom_line() + geom_point(aes(color=species_id))
```
The points get connected left-to-right, which is not what we want.
If we make the `color=species_id` aesthetic _global_, we don't have
this problem.
```{r color_global}
p + geom_line() + geom_point() + aes(color=species_id)
```
Alternatively, we can use the `group` aesthetic, which indicates that
certain data points go together. This way the lines can be a constant
color.
```{r group_aes}
p + geom_line(aes(group=species_id)) + geom_point(aes(color=species_id))
```
We could also make the group aesthetic global
```{r group_global}
p + aes(group=species_id) + geom_line() + geom_point(aes(color=species_id))
```
## Univariate geoms
We've focused so far on scatterplots, but one can also create
one-dimensional summaries, such as histograms or boxplots.
### Challenge
Try using `geom_histogram()` to make a histogram visualization of the
distribution of `weight`.
Hint: You want `weight` as the x-axis aesthetic. Try specifying `bins`
in `geom_histogram()`.
### Boxplot
Visualising the distribution of weight within each species.
```{r boxplot}
ggplot(reduced, aes(x = species_id, y = hindfoot_length)) +
geom_boxplot()
```
By adding points to boxplot, we can have a better idea of the number of
measurements and of their distribution:
```{r boxplot-with-points}
ggplot(reduced, aes(x = species_id, y = hindfoot_length)) +
geom_boxplot(alpha = 0) +
geom_jitter(alpha = 0.3, color = "tomato")
```
Notice how the boxplot layer is behind the jitter layer? What do you need to
change in the code to put the boxplot in front of the points such that it's not
hidden.
### Challenge
A variant on the box plot is the violin plot. Use `geom_violin()` to
make violin plots of `hindfoot_length` by `species_id`.
## Faceting
ggplot has a special technique called *faceting* that allows to split one plot
into multiple plots based on a factor included in the dataset. We will use it to
make one plot for a time series for each species.
```{r first-facet}
yearly_counts <- reduced %>% group_by(year, species_id) %>% tally
ggplot(yearly_counts, aes(x = year, y = n, group = species_id, colour = species_id)) +
geom_line() +
facet_wrap(~ species_id)
```
Now we would like to split line in each plot by sex of each individual
measured. To do that we need to make counts in data frame grouped by sex.
### Challenge
- Calculate counts grouped by year, species_id, and sex
- make the faceted plot splitting further by sex (within each panel)
- color by sex rather than species
Suppose I make a similar plot of average weight by species:
```{r average-weight-timeseries}
yearly_weight <- reduced %>%
group_by(year, species_id, sex) %>%
summarise(avg_weight = mean(weight, na.rm = TRUE))
ggplot(yearly_weight, aes(x=year, y=avg_weight, color = species_id, group = species_id)) +
geom_line() +
facet_wrap(~ species_id)
```
Why do we see those steps in the plot?
**Oops** need to group by sex
```{r average-weight-timeseries-fixed}
yearly_weight <- reduced %>%
group_by(year, species_id, sex) %>%
summarise(avg_weight = mean(weight, na.rm = TRUE))
ggplot(yearly_weight, aes(x=year, y=avg_weight, color = sex, group = sex)) +
geom_line() +
facet_wrap(~ species_id)
```
### facet_grid
The `facet_wrap` geometry extracts plots into an arbitrary number of dimensions
to allow them to cleanly fit on one page. On the other hand, the `facet_grid`
geometry allows you to explicitly specify how you want your plots to be
arranged via formula notation (`rows ~ columns`; a `.` can be used as
a placeholder that indicates only one row or column).
```{r average-weight-time-facet_sex_rows}
## One column, facet by rows
yearly_weight %>% filter(species_id %in% c("DM", "DO", "DS")) %>%
ggplot(aes(x=year, y=avg_weight, color = species_id, group = species_id)) +
geom_line() +
facet_grid(sex ~ .)
```
```{r average-weight-time-facet_sex_columns}
# One row, facet by column
yearly_weight %>% filter(species_id %in% c("DM", "DO", "DS")) %>%
ggplot(aes(x=year, y=avg_weight, color = species_id, group = species_id)) +
geom_line() +
facet_grid( ~ sex)
```
```{r average-weight-time-facet_sex_columns_species_rows}
# separate panel for each sex and species
yearly_weight %>% filter(species_id %in% c("DM", "DO", "DS")) %>%
ggplot(aes(x=year, y=avg_weight, color = species_id, group = species_id)) +
geom_line() +
facet_grid(species_id ~ sex)
```
## Saving plots to a file
If you want to save a plot, to share with others, use the `ggsave`
function.
The default is to save the last plot that you created, but I think
it's safer to first save the plot as an object and pass that to
`ggsave`. Also give the height and width in inches.
```{r ggsave_example, eval=FALSE}
p <- ggplot(reduced, aes(x=weight, y=hindfoot_length)) + geom_point()
ggsave("scatter.png", p, height=6, width=8)
```
The image file type is taken from the file name extension.
To make a PDF instead:
```{r ggsave_pdf, eval=FALSE}
ggsave("scatter.pdf", p, height=6, width=8)
```
Use `scale` to adjust the sizes of things, for example for a talk/poster
versus a paper/report. Use `scale < 1` to make the various elements
bigger relative to the plotting area.
```{r ggsave_scale, eval=FALSE}
ggsave("scatter_2.png", p, height=6, width=8, scale=0.8)
```
## Customizing plots
### Axis limits
When faceting, the different panels are given common x- and y-axis
limits. If we were to create separate plots (say one for each
country), we would need to do a bit extra to ensure that common axis
limits are used.
Recall the `scale_x_log10()` function that we had used to create the log
scale for the x axis. This can take an argument `limits` (a
vector of length 2) defining the minimum and maximum values plotted.
There is also a `scale_y_log10()` function, but if you want to change
the y-axis limits without going to a log scale, you would use
`scale_y_continuous()`. (Similarly, there's a `scale_x_continuous`.)
For example, to plot the data for China, using axis limits defined by
the full data, we'd do the following:
```{r limits}
xrange <- range(reduced$weight)
yrange <- range(reduced$hindfoot_length)
p <- reduced %>% filter(species_id=="DM") %>%
ggplot(aes(x=weight, y=hindfoot_length)) +
geom_point()
p + scale_x_log10(limits=xrange) +
scale_y_continuous(limits=yrange)
```
### Color choices
If you don't like the choices for point colors, you can customize
them in a number of ways. First, you can use `scale_color_manual()`
with a vector of your preferred choices. (If it's `fill` rather than
`color` that you want to change, you'll need to use `scale_fill_manual()`.)
```{r custom_colors}
p <- reduced %>% filter(species_id %in% c("DM", "DS", "DO")) %>%
ggplot(aes(x=weight, y=hindfoot_length)) +
geom_point(aes(color=species_id))
colors <- c("blue", "green", "orange")
p + scale_color_manual(values=colors)
```
You can also use RGB hex values.
```{r custom_colors_rgb}
hexcolors <- c("#001F3F", "#0074D9", "#01FF70")
p + scale_color_manual(values=hexcolors)
```
## Themes
Not everyone gray background and such in the default ggplot plots.
But you can apply one of a variety of "themes" to control the overall
appearance of plots.
One that a lot of people like is `theme_bw()`. Add it to a plot, and
the overall appearance changes.
```{r theme}
p + theme_bw()
```
<br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>