Cara Thompson | NHS-R 2022 Conference | 17th November 2022
đź‘© Cara Thompson
👩‍💻 Freelance data consultant specialising in dataviz and “enhanced” reproducible outputs
đź’™ Helping others maximise the impact of their expertise
🎨 Five tips for creating and applying bespoke colour schemes to your plots
Find out more: cararthompson.com/talks/nhsr2022-ggplot-themes
Make it easy for the readers to remember what is what.
But my graphs aren’t about penguins baking banana bread!
Make it easy for the readers to remember what is what.
Find out more: cararthompson.com/talks/nhsr2022-level-up
🤫 - I find picking colours really tricky
Luckily, there are others who can help…
Find out more: blog.datawrapper.de/colors-for-data-vis-style-guides/
Once you’ve found your colours, the quick fix…
… might be a dangerous shortcut!
ggplot(penguins %>%
# Oh, that should be a factor,
# let me fix that for you!
mutate(species =
factor(species,
levels = c("Chinstrap",
"Gentoo",
"Adelie"))),
aes(x = bill_depth_mm,
y = bill_length_mm,
colour = species)) +
geom_point(size = 5) +
labs(x = "Baking duration",
y = "Yumminess") +
theme_nhsr_demo() +
scale_colour_manual(values = c("#89973d",
"#e8b92f",
"#a45e41"))
What is going on?
The values will be matched in order (usually alphabetical) with the limits of the scale, or with breaks if provided. If this is a named vector, then the values will be matched based on the names instead. Data values that don’t match will be given
na.value
.
Create a named vector
banana_colours <- c("Adelie" = "#89973d",
"Chinstrap" = "#e8b92f",
"Gentoo" = "#a45e41")
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm,
colour = species)) +
geom_point(size = 5) +
labs(x = "Baking duration",
y = "Yumminess") +
theme_nhsr_demo() +
scale_colour_manual(values = banana_colours)
Create a named vector
banana_colours <- c("Adelie" = "#89973d",
"Chinstrap" = "#e8b92f",
"Gentoo" = "#a45e41")
ggplot(penguins %>%
# Oh, that should be a factor,
# let me fix that for you!
mutate(species =
factor(species,
levels = c("Chinstrap",
"Gentoo",
"Adelie"))),
aes(x = bill_depth_mm,
y = bill_length_mm,
colour = species)) +
geom_point(size = 5) +
labs(x = "Baking duration",
y = "Yumminess") +
theme_nhsr_demo() +
scale_colour_manual(values = banana_colours)
Key advantages
ggtext::element_markdown()
- Level up your plotsFind out more: cararthompson.com/talks/nhsr2022-level-up
📦 {colorblindr}
- Claire D. McWhite and Claus O. Wilke
a package to apply simulations of color vision deficiencies to existing ggplot2 figures. It can simulate various classes and severities of color blindness, as well as desaturate plots.
Find out more: https://github.com/clauswilke/colorblindr
Easiest way to do this? Build a palette that goes from a lighter colour to a darker colour.
A few helpful resources:
What if I don’t know how many colours I need?
R
to pick colours to maximise distance
R
interpolate
colorRampPalette()
my_anchor_colours <- c("green", "blue", "purple")
palmerpenguins::penguins %>%
mutate(island_fm = paste0(island, sex)) %>%
ggplot() +
geom_point(aes(x = bill_length_mm,
y = bill_depth_mm,
colour = island_fm,
size = body_mass_g),
show.legend = FALSE) +
theme_nhsr_demo() +
scale_color_manual(
values = colorRampPalette(
my_anchor_colours)(9))
my_anchor_colours <- c("#4f3c78", "#d3970a", "#c0979c")
palmerpenguins::penguins %>%
ggplot() +
geom_point(aes(x = bill_length_mm,
y = bill_depth_mm,
colour = island,
size = body_mass_g),
show.legend = FALSE) +
theme_nhsr_demo() +
scale_color_manual(
values = colorRampPalette(
my_anchor_colours)(3))
my_anchor_colours <- c("#4f3c78", "#d3970a", "#c0979c")
palmerpenguins::penguins %>%
mutate(island_fm = paste0(island, sex)) %>%
ggplot() +
geom_point(aes(x = bill_length_mm,
y = bill_depth_mm,
colour = island_fm,
size = body_mass_g),
show.legend = FALSE) +
theme_nhsr_demo() +
scale_color_manual(
values = colorRampPalette(
my_anchor_colours)(9))
my_anchor_colours <- c("#375248", "#d3970a", "#7691b1")
palmerpenguins::penguins %>%
mutate(island_fm = paste0(island, sex)) %>%
ggplot() +
geom_point(aes(x = bill_length_mm,
y = bill_depth_mm,
colour = island_fm,
size = body_mass_g),
show.legend = FALSE) +
theme_nhsr_demo() +
scale_color_manual(
values = colorRampPalette(
my_anchor_colours)(9))
There are only so many colours you can use in a plot!
carartemplates::carar_colours()
scale_nhsdemo_colour/fill()
functionsscale_nhsdemo_colour("Birmingham")
?Slides and recording: cararthompson.com/talks/nhsr2022-palatable-palettes