Skip to content

Commit 052ed23

Browse files
authored
Merge pull request #1116 from ethanwhite/updates-for-workshop
Update pub figures and gganimate for workshop
2 parents a97e584 + 6d17b7d commit 052ed23

File tree

2 files changed

+138
-55
lines changed

2 files changed

+138
-55
lines changed

materials/gganimate.md

+51-36
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,79 @@ title: gganimate
55
language: R
66
---
77

8-
> * ImageMagick needs to be installed to save as gif (the default)
9-
> * Save to html file if installing ImageMagick isn't an option
10-
> * E.g., gganimate(p, "output.html")
11-
128
### Installation
139

14-
* Some R packages are not published to CRAN
15-
* Can install from other places using `devtools`
16-
17-
```
18-
install.packages("devtools")
19-
devtools::install_github("dgrtwo/gganimate")
10+
```r
11+
# gifski required for default gif output
12+
install.packages(c("gganimate", "gifski", "rnoaa"))
2013
```
2114

2215
### Example
2316

2417
* Example using weather data
2518
* Get this data using `rnoaa`
2619

20+
```r
21+
library(gganimate)
22+
library(ggplot2)
23+
library(lubridate)
24+
library(rnoaa)
25+
26+
gnv_weather <- meteo_pull_monitors("USW00012816", date_min = "1984-01-01", date_max = "2024-12-31")
2727
```
28-
install.packages('rnoaa')
29-
library('rnoaa')
30-
gnv_weather <- meteo_pull_monitors("USW00012816", date_min = "1984-01-01", date_max = "2016-12-31")
31-
```
28+
3229
* Plot annual temperature pattern through time
3330
* First make a plot of the annual temperature trend using ggplot
3431

32+
```r
33+
gnv_weather$year = as.integer(year(gnv_weather$date))
34+
gnv_weather$jday = yday(gnv_weather$date)
35+
gnv_weather$tmax = gnv_weather$tmax / 10
36+
37+
ggplot(gnv_weather, aes(x = jday, y = tmax)) +
38+
geom_line()
3539
```
36-
library(ggplot2)
37-
library(gganimate)
3840

39-
gnv_weather$year = format(gnv_weather$date, "%Y")
40-
gnv_weather$jday = as.numeric(format(gnv_weather$date, "%j"))
41+
* This plot shows the temperatures of each day of the year for all years from 1984 to 2024
42+
* Let's animate them so we can see the pattern over time
43+
44+
* Add `transition_time` element to
4145

42-
ggplot(gnv_weather, aes(x = jday, y = tmax / 10)) +
43-
geom_point()
46+
```r
47+
ggplot(gnv_weather, aes(x = jday, y = tmax)) +
48+
geom_line() +
49+
transition_time(year)
4450
```
4551

46-
* Then add `frame` element to `aes()`
52+
* Now let's add some information on year to the graph
53+
* Do this by adding a title that shows the time (in our case year) for each frame
4754

55+
```r
56+
ggplot(gnv_weather, aes(x = jday, y = tmax)) +
57+
geom_line() +
58+
labs(title = 'Year: {frame_time}') +
59+
transition_time(year)
4860
```
49-
p <- ggplot(gnv_weather, aes(x = jday, y = tmax / 10, frame = year)) +
50-
geom_point()
5161

52-
gganimate(p)
62+
* Add a shadow mark to the plot so that we can see where previous years were
63+
64+
```r
65+
ggplot(gnv_weather, aes(x = jday, y = tmax)) +
66+
geom_line() +
67+
labs(title = 'Year: {frame_time}') +
68+
transition_time(year) +
69+
shadow_mark(color = 'gray', linewidth = 0.2)
5370
```
5471

55-
* Uses the `animation` package
56-
* More complicated but more powerful
72+
* If we want more control over the animation store the ggplot object and use `animate()`
5773

58-
```
59-
install.packages('animation')
60-
library(animation)
61-
62-
saveGIF({
63-
for (yr in 1984:2016){
64-
data = dplyr::filter(gnv_weather, year == yr)
65-
plot(data$jday, data$tmax)
66-
}
67-
})
74+
```r
75+
p <- ggplot(gnv_weather, aes(x = jday, y = tmax)) +
76+
geom_line() +
77+
labs(title = 'Year: {frame_time}') +
78+
transition_time(year) +
79+
shadow_mark(color = 'gray', linewidth = 0.2) +
80+
ease_aes('linear')
81+
82+
animate(p, fps = 2, end_pause = 50)
6883
```

materials/publication-figures-R.md

+87-19
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ element: notes
44
title: Publication quality figures
55
language: R
66
---
7-
8-
> * Have students install `devtools` and `patchwork` (using `devtools`)
7+
8+
> * Have students install `ggplot2`, `readr`, `patchwork`
99
> * Open https://ggplot2.tidyverse.org/reference/ggtheme.html in browser
1010
1111
### File formats
@@ -18,16 +18,17 @@ language: R
1818
* PNG is a good compromise format
1919
* Vector
2020
* Right choice for plots, line drawings
21+
* Describes the objects that make up the image, including their shapes, colors, and positions
2122
* Provides infinite scaling
22-
* EPS, AI, PDF, SVG
23+
* EPS (printers), AI, PDF, SVG (web)
2324

2425
* Save in different file formats using different extensions
2526

2627
```r
2728
library(ggplot2)
2829
library(readr)
2930

30-
trees <- read_tsv("https://ndownloader.figshare.com/files/5629536")
31+
trees <- read_tsv("https://ndownloader.figshare.com/files/5629536", na = c("", "NA", "missing", "MISSING", "?", "3.3."))
3132

3233
ggplot(trees, aes(x = HEIGHT, y = CIRC)) +
3334
geom_point()
@@ -58,13 +59,16 @@ ggsave("acacia_size_scaling.png", dpi = 300)
5859
ggsave("acacia_size_scaling.png", dpi = 30)
5960
```
6061

62+
* When submitting to a journal first think about how large the image will be
63+
* Then set the dpi to at least the journal's minimum resolution
64+
6165
### Color palettes
6266

6367
* Choose colors that work well for everyone
64-
* People with different kinds of color blindness
68+
* People with different kinds of color vision
6569
* People who printed your paper out without a color printer
6670
* Need to be correctly interpreted
67-
* Viridis is a new color scale that is designed to provide a good set of default
71+
* Viridis is a new(ish) color scale that is designed to provide a good set of default
6872
colors for addressing all of these concerns
6973

7074
```r
@@ -88,7 +92,7 @@ ggplot(trees, aes(x = HEIGHT, y = CIRC, color = HEIGHT)) +
8892
### Themes
8993

9094
* Can customize every aspect of plots in `ggplot`
91-
* Themes are an easy way to change the overall look of figures
95+
* Themes are an easy way to change the overall look of figures
9296
* These can be used to make coordinated changes to groups of options
9397

9498
```r
@@ -117,17 +121,6 @@ ggsave("species_scaling.jpg", species_scaling)
117121

118122
* Often want to combine multiple distinct plots into a single figure
119123
* Two popular packages for this, `patchwork` and `cowplot`
120-
121-
* `patchwork` is not on CRAN so install using `devtools`
122-
* `devtools` lets us install packages from a variety of sources, including
123-
GitHub, one of the major hubs of software development
124-
125-
```r
126-
install.packages('devtools')
127-
library(devtools)
128-
install_github('thomasp85/patchwork')
129-
```
130-
131124
* `patchwork` works by "adding" plots to one another
132125

133126
```r
@@ -171,4 +164,79 @@ height_dist <- height_dist +
171164
theme(legend.position='none')
172165

173166
height_dist + species_scaling + plot_layout(ncol = 1, heights = c(1, 5))
174-
```
167+
```
168+
169+
### Reusing custom plots
170+
171+
* Once you've created a plot you might want to reuse the same basic plotting code
172+
* The most common way folks do this is to copy and paste the code and change the pieces they want to change
173+
* A better way is using functions
174+
* Let's start with just the first piece of our graph
175+
176+
```r
177+
make_plot <- function(data, xcolumn, ycolumn, colorcolumn) {
178+
species_scaling <- ggplot(data, aes(x = xcolumn, y = ycolumn, color = colorcolumn)) +
179+
geom_point() +
180+
scale_color_viridis_d()
181+
}
182+
183+
plot_circ <- make_plot(trees, HEIGHT, CIRC, SPECIES)
184+
plot_circ
185+
```
186+
187+
* This doesn't work, but why?
188+
* There is an extra step we need to take when working with tidyverse functions that work with "data variables", i.e., names of columns that are not in quotes
189+
* These functions use tidy evaluation, a special type of non-standard evaluation
190+
* This basically means they do fancy things under the surface to make them easier to work with
191+
* But it means they don't work if we just pass things to functions in the most natural way
192+
* To fix this we have to tell our code which inputs/arguments are this special type of data variable
193+
* We do this by "embracing" them in double braces
194+
195+
```r
196+
make_plot <- function(data, xcolumn, ycolumn, colorcolumn) {
197+
{% raw %}scaling <- ggplot(data, aes(x = {{ xcolumn }}, y = {{ ycolumn }}, color = {{ colorcolumn }})) +{% endraw %}
198+
geom_point() +
199+
scale_color_viridis_d()
200+
}
201+
202+
plot_circ <- make_plot(trees, HEIGHT, CIRC, SPECIES)
203+
plot_circ
204+
```
205+
206+
* Now let's add the rest of our plot
207+
208+
```r
209+
make_plot <- function(data, xcolumn, ycolumn, colorcolumn) {
210+
{% raw %}scaling <- ggplot(data, aes(x = {{ xcolumn }}, y = {{ ycolumn }}, color = {{ colorcolumn }})) +{% endraw %}
211+
geom_point() +
212+
scale_color_viridis_d()
213+
214+
{% raw %}distribution <- ggplot(data, aes(x = {{ xcolumn }}, fill = {{ colorcolumn }})) +{% endraw %}
215+
geom_histogram() +
216+
scale_fill_viridis_d() +
217+
theme_void() +
218+
theme(legend.position='none')
219+
220+
distribution + scaling + plot_layout(ncol = 1, heights = c(1, 5))
221+
}
222+
223+
plot_circ <- make_plot(trees, HEIGHT, CIRC, SPECIES)
224+
plot_circ
225+
```
226+
227+
* Having done this we can now plot whatever we want
228+
229+
```r
230+
plot_axis1 <- make_plot(trees, HEIGHT, AXIS_1, SPECIES)
231+
plot_axis1
232+
```
233+
234+
```r
235+
plot_axis1_year <- make_plot(trees, HEIGHT, AXIS_1, as.factor(YEAR))
236+
plot_axis1_year
237+
```
238+
239+
```r
240+
plot_axis1_axis2_year <- make_plot(trees, AXIS_1, AXIS_2, as.factor(YEAR))
241+
plot_axis1_axis2_year
242+
```

0 commit comments

Comments
 (0)