Ah, ha, ha, ha, Parameterise!
A workshop on creating parameterised plot functions, which allowed us to create plots with different data, different variables, and different styling, all from one function.
Published
March 28, 2024
WebR Status
Installing package 2 out of 5: janitor
Click through the slides to retrace our steps, or jump straight to the code and give it a go!
Slides
Parameterised plot code
The setup
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
# Our libraries
library(ggplot2)
library(dplyr)
# The colours we'll be using
penguin_colours <- c(
Adelie = "pink",
Chinstrap = "orange",
Gentoo = "darkgreen",
Biscoe = "steelblue",
Torgersen = "turquoise",
Dream = "skyblue",
male = "#2C3D4F",
female = "#28A569"
)
# Our custom theme
theme_dt_demo <- function(base_size = 12, dark_text = "#222222") {
mid_text <- monochromeR::generate_palette(
dark_text,
"go_lighter",
n_colours = 5
)[2]
light_text <- monochromeR::generate_palette(
dark_text,
"go_lighter",
n_colours = 5
)[3]
theme_minimal(base_size = base_size) +
theme(
text = element_text(
colour = mid_text,
family = "sans",
lineheight = 1.1,
face = "bold"
),
plot.title = ggtext::element_textbox_simple(
colour = dark_text,
family = "Poppins",
size = rel(1.6),
face = "bold",
margin = margin(12, 0, 8, 0)
),
plot.subtitle = element_text(
size = rel(1.1),
margin = margin(4, 0, 0, 0)
),
axis.text.y = element_text(
colour = light_text,
size = rel(0.8),
family = "sans"
),
axis.text.x = element_text(
colour = light_text,
size = rel(0.8),
family = "sans"
),
axis.title.y = element_text(margin = margin(0, 0, 0, 12)),
legend.position = "bottom",
legend.justification = 1,
panel.grid = element_line(colour = "#FFFFFF"),
panel.background = element_rect(colour = "#FFFFFF", fill = "#FEFDFA"),
plot.background = element_rect(fill = "#FEFDFA", colour = "#FEFDFA"),
plot.caption = element_text(size = rel(0.8), margin = margin(8, 0, 0, 0)),
plot.margin = margin(0.25, 0.25, 0.25, 0.25, "cm")
)
}
The function
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
make_penguin_plot <- function(
df = palmerpenguins::penguins,
colours = penguin_colours,
grouping_variable = "species",
subtitle = NA,
add_labels = TRUE,
...
) {
mean_x_y <- df |>
group_by(get(grouping_variable)) |>
summarise(
mean_x = mean(bill_length_mm, na.rm = TRUE),
mean_y = mean(flipper_length_mm, na.rm = TRUE),
mean_weight = mean(body_mass_g, na.rm = TRUE),
count = length(get(grouping_variable))
)
unlabelled_plot <- df |>
ggplot() +
geom_point(
aes(
x = bill_length_mm,
y = flipper_length_mm,
colour = get(grouping_variable),
size = body_mass_g
),
size = 5,
alpha = 0.9
) +
# Reinstated the legend
labs(
title = paste0(
"Flipper lengths are proportional to bill lengths within each ",
grouping_variable
),
x = "Bill length (mm)",
y = "Flipper length (mm)",
colour = ""
) +
theme_dt_demo(16) +
scale_colour_manual(values = colours) +
theme(...)
# Note, this works locally, but not within webR - feel free to copy-paste the code
# to your own R session and give it a go!
if (add_labels == TRUE) {
plot_to_export <- unlabelled_plot +
ggtext::geom_textbox(
data = mean_x_y,
aes(
x = mean_x,
y = mean_y,
label = paste0(
"**",
`get(grouping_variable)`,
"**",
" (N = ",
count,
")",
"<br>Mean weight: ",
janitor::round_half_up(mean_weight / 1000, 2),
"kg"
)
),
size = 7,
family = "Work Sans",
width = unit(20, "lines"),
fill = "#FEFDFA",
box.colour = NA,
alpha = 0.9,
halign = 0.5
) +
theme(legend.position = "none")
} else {
plot_to_export <- unlabelled_plot
}
# End of bit that doesn't work!
if (!is.na(subtitle)) {
plot_to_export +
labs(subtitle = subtitle)
} else {
plot_to_export
}
}
Give it a go!
1
make_penguin_plot()
Recording
Reuse
Citation
For attribution, please cite this work as:
“Ah, Ha, Ha, Ha, Parameterise!” 2024. March 28, 2024. https://www.cararthompson.com/talks/parameterise/.