-
Notifications
You must be signed in to change notification settings - Fork 9
/
ggplot2.Rmd
346 lines (227 loc) · 11.4 KB
/
ggplot2.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
---
title: 'ggplot: Quick Introduction'
output:
html_document:
df_print: paged
code_download: TRUE
toc: true
toc_depth: 2
editor_options:
chunk_output_type: console
---
```{r, setup, include=FALSE}
knitr::opts_chunk$set(eval=TRUE, fig.width=5, fig.height=3.5)
```
This is an [R Markdown](https://rmarkdown.rstudio.com/) document. Follow the link to learn more about R Markdown and the notebook format used during the workshop.
The cheatsheet is very useful when working with ggplot2: https://www.rstudio.org/links/data_visualization_cheat_sheet
# Setup
```{r, warning=FALSE, error=FALSE, message=FALSE}
library(dplyr)
library(ggplot2)
library(readr)
```
# Data
We're using two data sets, one from a package, one built into R.
You only need to do this once: install the remotes package (or you can substitute devtools if you have that), and then use that package to install the palmerpenguins package from GitHub.
```{r, installpackages, eval=FALSE}
install.packages("palmerpenguins")
```
If you have it installed, load it and look at the penguins data set:
```{r}
library(palmerpenguins)
penguins
```
We're also going to use average yearly temperatures in New Hampshire, a data set built into R (`nhtemp`). But we need to put it in a data frame first to make it easier to use (it's in a special time series object):
```{r}
nh <- data.frame(temp=c(nhtemp), year=1912:1971)
nh
```
# Introductions
Why ggplot? For me, it makes exploring my data, especially groups in my data, much easier. I can easily make some plots with ggplot that are difficult enough to make with base R graphics that I just won't do them routinely when exploring my data. This is bad, because we should visualize our data! I don't want the code to get in the way of that. ggplot is a package where I recommend first learning the basic syntax, and then go back and learn some more about the theory behind the package -- what the logic is to how it works. It took me a few iterations of working with ggplot to get comfortable with it, and to understand how to think in a way that was compatible with how it approaches visualization. But once I did, it made data visualization easier and more fun.
The syntax of ggplot2 is a little bit different than other tidyverse packages, in part because it predates them. But it does share a few things with other tidyverse packages, such as:
* It expects to work with a data frame, and the data frame is the first input
* You can reference the names of columns without quotes or $ syntax
* It's easy to work with groups in your data
* Instead of functions with lots of different arguments (although some of these functions do have a lot), you combine multiple function calls together to achieve what you want to do
The package is called ggplot2 (there is no first version), the main function in the package is called ggplot, and "ggplot" generally refers to anything with the package (I and others often don't bother to say the 2 in the package name).
# ggplot Basics
Each plot with ggplot has the same template for the code:
```{r, eval=FALSE}
## call to ggplot and the name of the data frame
ggplot(dataframename,
## which variables to use for which plot components
aes(x = column_name, y = column_name)) +
# what type of plot
geom_****()
```
The first line says what data and variables to use, and the second line says what type of plot to make. We add the components of the plot together, which is a somewhat unique syntax.
For example:
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_point()
```
The warning message is telling you that two observations (rows) in your data frame are not plotted because there were missing values. You can get this warning from explicit missing values or in cases where you set the limits of the axes in a way that excludes some data points.
The first line alone (without the + at the end) makes a plot set up without adding the data:
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm))
```
If we change the geom function we use, the plot type changes:
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_bin2d()
```
This is a density plot, where the color of each rectangle indicates how many observations (points on the above plot) fall in that area.
### EXERCISE
Fill in the `___` to make a line plot using `geom_line()` with the `nh` data frame, with `year` as the x variable and `temp` as the y:
```{r, eval=FALSE}
ggplot(___, aes(x = ___, y = ___)) +
___()
```
## Different Plot Types
The above plots used two continuous variables. The [cheat sheet](https://www.rstudio.org/links/data_visualization_cheat_sheet) has the geoms organized by how many and what type of variables you want to plot. For example, we can make a histogram, which is of a single variable:
```{r}
ggplot(penguins, aes(x = bill_depth_mm)) +
geom_histogram()
```
The warning message is how many bins (bars) there are in the histogram. It defaults to 30, but it wants you to explicitly pick a number instead:
```{r}
ggplot(penguins, aes(x = bill_depth_mm)) +
geom_histogram(bins = 25)
```
We can also have multiple geoms on the same plot:
```{r}
ggplot(nh, aes(x=year, y=temp)) +
geom_line() +
geom_point()
```
```{r}
ggplot(nh, aes(x=year, y=temp)) +
geom_point() +
geom_line() +
geom_smooth()
```
### EXERCISE
Make a histogram (`geom_histogram()`) of the `temp` variable in the `nh` dataset
```{r, eval=FALSE}
```
## Other Aesthetics and Grouping
We can set other aesthetics of the plot (color, fill, linetype, marker, etc.) according to variables in our data.
Note that if you want a line or color for each group, those groups need to be defined by a categorical variable. This is the opposite of how data usually needs to be structured when using base R plotting functions. If the values for the two groups are in separate columns in your dataset, you'll need to reshape your data to get them in a single column instead -- something covered in the tidyr session of these workshops.
Each aesthetic -- each thing you define in aes() -- needs to be a single column in your data set.
With categorical variables, this helps us see the groups in our data:
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point()
```
Note that a legend was added automatically.
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point() +
geom_smooth(method="lm", se=FALSE)
```
`method=lm` tells it how to fit a line to the data; "lm" means fit a linear model with the `lm` function. `se=FALSE` means to not display the standard errors on the plot.
Note that the smoothing was done by group. Specifying `color` effectively grouped our data, much like what happens with `group_by` in dplyr.
### EXERCISE
Repeat the same plot as above, `bill_length_mm` vs. `bill_depth_mm`, but `color` by `body_mass_g` instead of `species`.
```{r}
```
## Labels/Titles
A convenient way to label axes and legends and set the title is with the labs() function, added as a component of the plot:
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point() +
labs(title="Penguin Bill Dimensions",
y="Depth (mm)",
x="Length (mm)",
color="Body Mass",
subtitle="Three Species",
caption="Source: palmerpenguins")
```
### EXERCISE
Plot `flipper_length_mm` vs. `body_mass_g` and color by `sex`. Label the axes and title your plot.
```{r}
```
## Axis Limits
To change the axes min and max values, like we would with `xlim` or `ylim` in base R graphics, we use a scale function and set the `limits` argument:
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_point() +
scale_x_continuous(limits=c(30, 50))
```
## Facets
In addition to using color, fill, linetype, etc. to denote groups, we can also make a separate plot for each group, where the plots are aligned and share axes. These are called facets:
```{r}
ggplot(penguins, aes(flipper_length_mm)) +
geom_histogram() +
facet_grid(sex ~ .)
```
The facet function syntax is using the formula syntax used elsewhere in R, but it can be tough to remember. It's on the cheat sheet though so you can look it up. There are other options for laying out plots horizontally or in a grid.
Note that the plots share an x-axis, and the y-axis has the same range in each plot.
If you want to put multiple plots in a single image together, but they are not facets of a single plot, see the patchwork package.
### EXERCISE
Make a histogram (set a value for bins) of `body_mass_g` for each `species`:
```{r, eval=FALSE}
ggplot(penguins, aes(___)) +
___(___) +
facet_grid(___ ~ .)
```
## Styling
Themes control the look of the plot not related to the data: fonts, backgrounds, grid lines, axis lines, etc.
There are some built-in themes (`theme_minimal()`, `theme_bw()`, `theme_classic()`, etc.), but you can also control the styling further with the `theme()` function:
```{r}
ggplot(penguins, aes(body_mass_g)) +
geom_histogram(bins = 30) +
theme_minimal()
```
For further control, use `theme` instead or in addition:
```{r}
ggplot(penguins, aes(body_mass_g)) +
geom_histogram(bins = 30) +
theme_minimal() +
theme(panel.grid.major.x = element_blank(), # get rid of the vertical grid lines
panel.grid.minor.x = element_blank())
```
Many theme elements are controlled by setting options in additional `element` functions:
* `element_blank()` - omit the element
* `element_text()`
* `element_line()`
* `element_rect()`
These are options that you will generally google for how to do: "ggplot remove gridlines", "ggplot change font size", etc.
In addition to themes, you can control some aspects of the data-driven elements of the plot directly, without using a variable to define them. For example with color or size, if we specify them outside of a call to `aes()` within the geom function:
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_point(color="red", size=3)
```
it colors all of the points the same color.
### EXERCISE
Change the `fill` color for the bars in our histogram above to be "darkgreen". The code from above is copied below to get you started.
```{r, eval=FALSE}
ggplot(penguins, aes(body_mass_g)) +
geom_histogram(bins = 30) +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
```
## Pipes
We use `+`, not `%>%`, to join together components of our ggplot. But we can still pipe the results of other commands into ggplot - I just didn't above because we were just plotting the data frame as it is. For example, to plot only the Adelie penguins:
```{r}
penguins %>%
filter(species == "Adelie") %>%
ggplot(aes(x=bill_length_mm, y=bill_depth_mm)) +
geom_point()
```
# Learn More
The first chapter of R for Data Science https://r4ds.had.co.nz/ covers ggplot well.
For more on ggplot2, see [our guide](https://sites.northwestern.edu/researchcomputing/2020/04/13/online-learning-resources-r-ggplot2/) with free (for Northwestern folks) resources.
We're happy to help you use ggplot on your own data. Request a free consultation: https://www.it.northwestern.edu/research/consultation/data-services.html