@@ -4,8 +4,8 @@ element: notes
4
4
title : Publication quality figures
5
5
language : R
6
6
---
7
-
8
- > * Have students install ` devtools ` and ` patchwork ` (using ` devtools ` )
7
+
8
+ > * Have students install ` ggplot2 ` , ` readr ` , ` patchwork `
9
9
> * Open https://ggplot2.tidyverse.org/reference/ggtheme.html in browser
10
10
11
11
### File formats
@@ -18,16 +18,17 @@ language: R
18
18
* PNG is a good compromise format
19
19
* Vector
20
20
* Right choice for plots, line drawings
21
+ * Describes the objects that make up the image, including their shapes, colors, and positions
21
22
* Provides infinite scaling
22
- * EPS, AI, PDF, SVG
23
+ * EPS (printers) , AI, PDF, SVG (web)
23
24
24
25
* Save in different file formats using different extensions
25
26
26
27
``` r
27
28
library(ggplot2 )
28
29
library(readr )
29
30
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. " ) )
31
32
32
33
ggplot(trees , aes(x = HEIGHT , y = CIRC )) +
33
34
geom_point()
@@ -58,13 +59,16 @@ ggsave("acacia_size_scaling.png", dpi = 300)
58
59
ggsave(" acacia_size_scaling.png" , dpi = 30 )
59
60
```
60
61
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
+
61
65
### Color palettes
62
66
63
67
* Choose colors that work well for everyone
64
- * People with different kinds of color blindness
68
+ * People with different kinds of color vision
65
69
* People who printed your paper out without a color printer
66
70
* 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
68
72
colors for addressing all of these concerns
69
73
70
74
``` r
@@ -88,7 +92,7 @@ ggplot(trees, aes(x = HEIGHT, y = CIRC, color = HEIGHT)) +
88
92
### Themes
89
93
90
94
* 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
92
96
* These can be used to make coordinated changes to groups of options
93
97
94
98
``` r
@@ -117,17 +121,6 @@ ggsave("species_scaling.jpg", species_scaling)
117
121
118
122
* Often want to combine multiple distinct plots into a single figure
119
123
* 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
-
131
124
* ` patchwork ` works by "adding" plots to one another
132
125
133
126
``` r
@@ -171,4 +164,79 @@ height_dist <- height_dist +
171
164
theme(legend.position = ' none' )
172
165
173
166
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