Skip to content

Commit 6d17b7d

Browse files
committed
Update and expand pub quality figures
Include using functions to make the same graph repeatedly
1 parent 1b45129 commit 6d17b7d

File tree

1 file changed

+87
-19
lines changed

1 file changed

+87
-19
lines changed

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)