-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathediting-figures-in-r.qmd
527 lines (427 loc) · 22.1 KB
/
editing-figures-in-r.qmd
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
---
format:
html:
css: styles.css
toc: true
title: "Editing Figures in R"
title-block-banner: images/urban-cover-photo.png
title-block-banner-color: white
execute:
echo: false
code-overflow: wrap
---
# Editing Figures in R
Editing figures in R for Urban style can be done quite easily if you know what to look for. The first step is to ask the researcher for the R files associated with the project in a folder (usually an R project folder). The preferred method is opening a new branch on a GitHub repo. For more information on how to make a new branch on an existing repo, see the [Using GitHub](using-github.qmd) page. If the researcher only has a local folder saved, you can ask them to send you the zipped project folder via email.
Before you begin, make sure the researcher has used the [urbnthemes ggplot2 package](https://urbaninstitute.github.io/urbnthemes/) for generating the figures.
### Opening the R Project
Go to RStudio on your desktop and open the R project via the zipped file or through GitHub. All of the files will open in RStudio in the side panel. Ask the researchers which file has the associated figure that needs editing. Also ask the researcher for the numbered lines of code, so you can navigate to the figure more easily.
<!-- Figure -->
::: {#fig-Rstudio-files}
::: no-indent
<box-title-text>RStudio Example</box-title-text>
{fig-align="left"}
<source-note-text>**Source:** Author's screenshot.</source-note-text>
<source-note-text>**Notes:** Highlighted in green are the numbered lines of code. Circled in orange is where you can see all the files in an R project.</source-note-text>
:::
:::
### What to Look for in the File
Next, you will need to find the figure that requires editing or formatting work. The researcher can point you to those lines of code. The key identifier to look out for is <code>ggplot</code>. Figure 1 below is a fully formatted figure using <code>urbnthemes</code> and custom formatting parameters in <code>ggplot</code>. If you toggle to figure 2, you'll see <code>ggplot</code> and <code>urbnthemes</code> was used without custom parameters. Code for the associated figures can be found in the tabset below the figures. Take note of the extra code it takes to customize a figure when toggling between tabs.
```{r chart-setup}
#| include: false
#| echo: false
library(tidyverse)
library(knitr)
library(kableExtra)
library(urbnthemes)
library(grid)
library(gridExtra)
library(formattable)
library(kableExtra)
library(ggplot2)
library(dplyr)
library(scales)
library(extrafont)
category <- c("All people", "Children\nunder age 18", "Adults\nages 18–64", "Adults\nages 65 and older")
df2 <- read.csv("data/data-fact-sheets.csv", header=FALSE)
filtered_df2 <- df2 %>%
filter(V1 %in% c("Alabama", "State"))
baseline <- unname(unlist(filtered_df2[, c(11,13,15,17)]))
baseline <- baseline[-c(1,3,5,7)]
full_funding <- unname(unlist(filtered_df2[, c(12,14,16,18)]))
full_funding <- full_funding[-c(1,3,5,7)]
df_fig2 <- data.frame("Category" = category, "Baseline" = baseline, "Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing
" = full_funding)
df_fig2_long <- tidyr::gather(df_fig2, Series, Value, -"Category")
df_fig2_long$Category <- factor(df_fig2_long$Category, levels = unique(df_fig2_long$Category))
df_fig2_long$Value <- as.numeric(df_fig2_long$Value)
decimal_to_percentage <- function(x) {
sprintf("%.1f%%", x * 1)
}
```
::: {.panel-tabset}
## Figure 1
```{r ggplot-chart}
#With custom parameters applied
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -0.5,
position = position_dodge2(width = 0.7, padding = 0.5),
size = 2,
color = "black",
family = "Lato") +
scale_y_continuous(expand = expansion(mult = 0),
limits = c(0, max(df_fig2_long$Value) * 1.5),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "top",
legend.justification = "left",
legend.margin = margin(t = -8),
plot.margin = unit(c(1, 0, 0, 0), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "black"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "black"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "black"),
legend.text = element_text(size = 7),
legend.title = element_text(size = 7),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
guides(fill = guide_legend(title = ""))
```
## Figure 2
```{r ggplot-chart-2}
#| warning: false
#Without custom parameters applied
ggplot(df_fig2_long, aes(x= Category, y = Value, fill = Series)) +
geom_bar(stat = "identity") +
labs(y = "Percent", x = "", fill = "")
```
:::
::: {.panel-tabset}
## Figure 1 code
```{r ggplot-chart-code}
#| echo: true
#| eval: false
#| code-overflow: scroll
#| code-line-numbers: true
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -0.5,
position = position_dodge2(width = 0.7, padding = 0.5),
size = 2,
color = "black",
family = "Lato") +
scale_y_continuous(expand = expansion(mult = 0),
limits = c(0, max(df_fig2_long$Value) * 1.5),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "top",
legend.justification = "left",
legend.margin = margin(t = -8),
plot.margin = unit(c(1, 0, 0, 0), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "black"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "black"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "black"),
legend.text = element_text(size = 7),
legend.title = element_text(size = 7),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
guides(fill = guide_legend(title = ""))
```
## Figure 2 code
```{r ggplot-chart-code-2}
#| echo: true
#| eval: false
#| code-overflow: scroll
#| code-line-numbers: true
#| warning: false
ggplot(df_fig2_long, aes(x= Category, y = Value, fill = Series)) +
geom_bar(stat = "identity") +
labs(y = "Percent", x = "", fill = "")
```
:::
### Common Changes to Figures
Below you will find common changes for figures and how to make those changes.
#### Capitalization and hypenation changes in data labels, legend text, and axis text
To change capitalization, hyphenation, or other text changes in legend and axis text, look at the example code again, this time with the relevant text highlighted. If some of the chart text gets pulled in from the dataset itself, ask the researchers to make the required changes.
```{r ggplot-chart-code-legend}
#| echo: true
#| eval: false
#| code-overflow: scroll
#| code-line-numbers: true
#| source-line-numbers: "3-5"
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -0.5,
position = position_dodge2(width = 0.7, padding = 0.5),
size = 2,
color = "black",
family = "Lato") +
scale_y_continuous(expand = expansion(mult = 0),
limits = c(0, max(df_fig2_long$Value) * 1.5),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "top",
legend.justification = "left",
legend.margin = margin(t = -8),
plot.margin = unit(c(1, 0, 0, 0), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "black"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "black"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "black"),
legend.text = element_text(size = 7),
legend.title = element_text(size = 7),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
guides(fill = guide_legend(title = ""))
```
You can change the text for <code>labs(y = "Y AXIS LABEL HERE", x = "X AXIS LABEL HERE", fill = "")</code> and <code>labels = c("SERIES 1 LABEL",
"SERIES 2 LABEL")</code>
#### Color and position changes for chart text
Highlighted below are lines of code associated with data labels and axis text. If you need to change the styling of those, look for similar lines of code. The text has been converted to different colors to make the changes more apparent. You can also use hex codes in place of colors (especially useful for Urban-sepcific colors) like this <code>"#1696d2"</code>. Notice in the same lines of code, you can change the font style and font size.
Also notice that the last line of code is highlighted to show the addition of a legend title.
::: {.panel-tabset}
## Code
```{r ggplot-chart-code-legend-2}
#| echo: true
#| eval: false
#| code-overflow: scroll
#| code-line-numbers: true
#| source-line-numbers: "9-11,20-24,29"
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -0.5,
position = position_dodge2(width = 0.7, padding = 0.5),
size = 5,
color = "red",
family = "Lato") +
scale_y_continuous(expand = expansion(mult = 0),
limits = c(0, max(df_fig2_long$Value) * 1.5),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "top",
legend.justification = "left",
legend.margin = margin(t = -8),
plot.margin = unit(c(1, 0, 0, 0), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "green"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "blue"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "orange"),
legend.text = element_text(size = 7, family = "Lato", colour = "yellow"),
legend.title = element_text(size = 7, family = "Lato", colour = "purple"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
guides(fill = guide_legend(title = "Example Title Here"))
```
## Figure
```{r ggplot-chart-legend-2}
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -0.5,
position = position_dodge2(width = 0.7, padding = 0.5),
size = 2,
color = "red",
family = "Lato") +
scale_y_continuous(expand = expansion(mult = 0),
limits = c(0, max(df_fig2_long$Value) * 1.5),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "top",
legend.justification = "left",
legend.margin = margin(t = -8),
plot.margin = unit(c(1, 0, 0, 0), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "green"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "blue"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "orange"),
legend.text = element_text(size = 7, family = "Lato", colour = "yellow"),
legend.title = element_text(size = 7, family = "Lato", colour = "purple"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
guides(fill = guide_legend(title = "Example Title Here"))
```
:::
Below you will see how to make positional changes to the charts. More information about the specifics of text labels can be found in [ggplot2's documentation](https://ggplot2-book.org/annotations#sec-text-labels). Below I've adjusted the original numbers to show how changes affect the text.
Note that <code>vjust</code> numbers are counterintuitive. A negative number moves the label up, and a positive number moves the label down.
Also note that <code>plot.margin</code> increases or decreases the margin around the whole plot, which is not to be confused with <code>legend.margin</code>, which changes the margin of the legend only.
::: {.panel-tabset}
## Code
```{r ggplot-chart-code-legend-3}
#| echo: true
#| eval: false
#| code-overflow: scroll
#| code-line-numbers: true
#| source-line-numbers: "7,8,16-19"
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -2,
position = identity(),
size = 5,
color = "red",
family = "Lato") +
scale_y_continuous(expand = expansion(mult = 0),
limits = c(0, max(df_fig2_long$Value) * 1.5),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "bottom",
legend.justification = "right",
legend.margin = margin(t = 8),
plot.margin = unit(c(2, 2, 2, 2), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "green"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "blue"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "orange"),
legend.text = element_text(size = 7, family = "Lato", colour = "yellow"),
legend.title = element_text(size = 7, family = "Lato", colour = "purple"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
guides(fill = guide_legend(title = ""))
```
## Figure
```{r ggplot-chart-legend-3}
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -2,
position = position_identity(),
size = 2,
color = "black",
family = "Lato") +
scale_y_continuous(expand = expansion(mult = 0),
limits = c(0, max(df_fig2_long$Value) * 1.5),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "bottom",
legend.justification = "right",
legend.margin = margin(t = 8),
plot.margin = unit(c(2, 2, 2, 2), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "black"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "black"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "black"),
legend.text = element_text(size = 7, family = "Lato", colour = "black"),
legend.title = element_text(size = 7, family = "Lato", colour = "black"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
guides(fill = guide_legend(title = ""))
```
:::
#### Axes and grid lines
See the code below to change the axis positioning. In this example, the changes are being made relative to the y axis using <code>scale_y_continuous</code>, but the same principles apply to the x axis using <code>scale_x_continuous</code>.
The example below also shows the default values for the chart (as if no parameters were set). The original chart at the top with the correct parameters set uses <code>max(df_fig2_long$Value) * 1.5</code> to provide head space above the bars in the chart. In the example below, <code>expand = expansion(mult = 0)</code> has been removed, which is why the bars look like they are floating. See documentation on ggplot2 expansion parameters [here](https://ggplot2.tidyverse.org/reference/expansion.html).
See the panel and axis parameters at the end of the code block. <code>element_blank()</code> has been changed to <code>element_rect()</code> to show the differences, and <code>axis.line</code> has been changed to <code>element_blank()</code>.
Lastly, see line 15. <code>theme_bw()</code> ensures there is no background color for the chart. If there's a gray background, most likely the code is missing this line.
::: {.panel-tabset}
## Code
```{r ggplot-chart-code-legend-4}
#| echo: true
#| eval: false
#| code-overflow: scroll
#| code-line-numbers: true
#| source-line-numbers: "12-15,25-28"
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -0.5,
position = position_dodge2(width = 0.7, padding = 0.5),
size = 5,
color = "red",
family = "Lato") +
scale_y_continuous(
limits = c(0, max(df_fig2_long$Value) * 1),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "top",
legend.justification = "left",
legend.margin = margin(t = -8),
plot.margin = unit(c(1, 0, 0, 0), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "black"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "black"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "black"),
legend.text = element_text(size = 7, family = "Lato", colour = "black"),
legend.title = element_text(size = 7, family = "Lato", colour = "black"),
panel.border = element_rect(colour = "red"),
panel.grid.major = element_line(colour = "blue"),
panel.grid.minor = element_line(colour = "green"),
axis.line = element_blank()) +
guides(fill = guide_legend(title = ""))
```
## Figure
```{r ggplot-chart-legend-4}
ggplot(df_fig2_long, aes(x = Category, y = Value, fill = Series)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Percent", x = "", fill = "") +
scale_fill_discrete(labels = c('Current funding and participation',
'Full funding and participation in SSI, SNAP, WIC, TANF, CCDF, LIHEAP, and housing')) +
geom_text(aes(label = decimal_to_percentage(Value)),
vjust = -0.5,
position = position_dodge2(width = 0.7, padding = 0.5),
size = 2,
color = "black",
family = "Lato") +
scale_y_continuous(
limits = c(0, max(df_fig2_long$Value) * 1),
labels = decimal_to_percentage) +
theme_bw() +
theme(legend.position = "top",
legend.justification = "left",
legend.margin = margin(t = -8),
plot.margin = unit(c(1, 0, 0, 0), "lines"),
axis.title.y = element_text(size = 7, family = "Lato", colour = "black"),
axis.text.x = element_text(size = 8, family = "Lato", colour = "black"),
axis.text.y = element_text(size = 8, family = "Lato", colour = "black"),
legend.text = element_text(size = 7, family = "Lato", colour = "black"),
legend.title = element_text(size = 7, family = "Lato", colour = "black"),
panel.border = element_rect(colour = "red"),
panel.grid.major = element_line(colour = "blue"),
panel.grid.minor = element_line(colour = "green"),
axis.line = element_blank()) +
guides(fill = guide_legend(title = ""))
```
:::
#### Low-quality exported images
If the images in a report generated in R are not high-enough quality for print publications, you can change the parameters by using the following code underneath the ggplot2 code. Make sure you change the plot name to the correct plot name or use <code>plot = last_plot()</code> instead.
The image will save in the same folder as the R file you are working in.
```{r chart-save-code}
#| echo: true
#| eval: false
#| code-overflow: wrap
ggsave("plot-name.png", plot = plot_name, width = 8.25, units = "in", dpi = 300)
```