
This package generates a monochrome palette from a starting colour
for a specified number of colours. Users can decide whether to go
darker, lighter, or both ways from that starting colour, or to blend it
towards a second colour. Colours can be provided as a vector of rgb
values (e.g. c(15, 75, 99)), a hex colour code
(e.g. #0F4B63) or a recognised colour name
(e.g. "purple").
The package can also be use to display any palette as a ggplot object in the plot window, with or without hex colour code labels. By default, the labels also show how well black and white contrast with each colour, to help you check the accessibility of your palette.
This package is available on CRAN, so can be installed using
install.packages("monochromeR").
Alternatively, to install it from here, use
remotes::install_github("cararthompson/monochromeR"). (To
do this, you need to have installed the remotes package. To
do that, use install.packages("remotes").)
Sure! Here goes. To make the examples easy to read, I will use recognised colour names rather than hex codes or rgb values.
generate_palette()library(monochromeR)
generate_palette(
"navy",
modification = "go_lighter",
n_colours = 5,
view_palette = TRUE
)
## [1] "#000080" "#323299" "#6666B2" "#9999CC" "#CCCCE5"
The functions allow for British spelling and US spelling of colour/color.
generate_palette(
"navy",
modification = "go_darker",
n_colors = 5,
view_palette = TRUE,
view_labels = FALSE
)
## [1] "#000080" "#000066" "#00004C" "#000033" "#000019"
With more colours, the hex codes get harder to view in the plot. They are printed in the console when the function is called on its own, and can also be assigned to an object for later use.
navy_palette <- generate_palette(
"navy",
modification = "go_both_ways",
n_colours = 20,
view_palette = TRUE,
view_labels = FALSE
)
navy_palette## [1] "#CCCCE5" "#B6B6DA" "#A1A1D0" "#8B8BC5" "#7676BA" "#6060B0" "#4B4BA5"
## [8] "#35359A" "#202090" "#0A0A85" "#00007A" "#00006F" "#000065" "#00005A"
## [15] "#00004F" "#000044" "#000039" "#00002F" "#000024" "#000019"
To blend two colours together, give the second colour as the
modification:
generate_palette(
"navy",
"pink",
n_colours = 10,
view_palette = TRUE,
view_labels = FALSE
)
## [1] "#000080" "#161186" "#2D228D" "#433394" "#5A449A" "#7155A1" "#8866A8"
## [8] "#9E77AE" "#B588B5" "#CC99BC"
If you ask for something that isn’t possible, such as going lighter
from white, generate_palette() will let you know, and the
error messages aim to point you towards a fix.
# Get hex code from rgb
rgb_to_hex(c(15, 75, 99))## [1] "#0F4B63"
# Get hex code from rgba
rgba_to_hex(c(15, 75, 99, 0.8))## [1] "#3E6E82"
# Get the rgb values from the hex code
hex_to_rgb("#FFFFFF")## r g b
## 255 255 255
# Get the rgb values from the hex code
hex_to_rgb("#0F4B63")## r g b
## 15 75 99
# Get the rgb values for several colours at once
hex_to_rgb(c("unripe" = "#89973d", "ripe" = "#e8b92f"))## r g b
## unripe 137 151 61
## ripe 232 185 47
The output can be passed straight to the other functions. For example, to get a lighter version of a colour, as it would look at 70% opacity on a white background:
rgba_to_hex(c(hex_to_rgb("#0F4B63"), 0.7))## [1] "#578191"
colours <- c("pink", "#2c3d4f")
purrr::map_vec(colours, \(x) rgba_to_hex(c(hex_to_rgb(x), 0.7)))## [1] "#FFD2DA" "#6B7783"
view_palette(c("pink", "#2c3d4f", "orange", "darkgreen"), view_labels = FALSE)
view_palette(c(
wesanderson::wes_palettes$Moonrise1,
wesanderson::wes_palettes$Moonrise2[1:2]
))
If you have created a named vector for your colours (which I highly recommend!), the names you have provided are displayed alongside the hex codes.
banana_palette <- c(
"unripe" = "#89973d",
"ripe" = "#e8b92f",
"overripe" = "#a45e41"
)
view_palette(banana_palette)
From version 0.3.0 onwards, the labels show the WCAG
contrast ratio between each colour and black and white by default.
For text, aim for a contrast over 7:1; for shapes, aim for a contrast
over 3:1. The ratios are rounded down, so they never overstate the
contrast. To hide them, use view_contrast = FALSE.
view_palette(banana_palette, view_contrast = FALSE)
To check the contrast ratios with other colours, specify up to two
colours as contrast_colours. The labels are written in
those colours, and show the contrast ratio against each. If you name
them, the labels use the names you provide.
view_palette(
banana_palette,
contrast_colours = c(my_brand_blue = "#2c3d4f", "#ffffff")
)
To get the contrast ratios themselves, use
contrast_ratio(). It compares each colour against white by
default, or against any other colour you choose.
contrast_ratio(banana_palette)## unripe ripe overripe
## 3.202342 1.840202 4.934309
contrast_ratio(banana_palette, against = "black")## unripe ripe overripe
## 6.557701 11.411793 4.255915
contrast_ratio(banana_palette, against = "#2c3d4f")## unripe ripe overripe
## 3.474289 6.046000 2.254796
Where colours have a contrast ratio lower than 3, adding an outline
which does have sufficient contrast to the shape allows you to use them
in a way that remains accessible (e.g. geom_point with
shape = 21).
The exported palette-viewing plot is a ggplot object, which can be
passed to functions such as colorblindr::cvd_grid() to
check how the palette is perceived by people with different types of
colour blindness. With view_labels = TRUE, the labels are
displayed in black and white on top of the colour, to allow users to
easily see how readable the text is.
view_palette(c("pink", "#2c3d4f", "orange", "darkgreen"), view_labels = TRUE)
colorblindr::cvd_grid()
monochromeR within datavisualisationsgenerate_palette within
scale_colour_manual()Here’s a simple example, using {monochromeR}’s
generate_palette() to create a colour palette on the fly
within ggplot().
library(ggplot2)
library(monochromeR)
penguin_plot <- penguins |>
ggplot() +
geom_point(
aes(
x = flipper_len,
y = bill_len,
fill = species,
size = body_mass
),
alpha = 0.8,
shape = 21
) +
labs(
title = "Perfectly proportional penguins",
subtitle = paste(
"Each dot represents a penguin.",
"The bigger the dot, the heavier the penguin. \nLook at them go!"
),
x = "Flipper length (mm)",
y = "Bill length (mm)"
) +
scale_size(guide = "none") +
theme_minimal() +
theme(
text = element_text(family = "Karla"),
plot.subtitle = element_text(margin = margin(6, 0, 12, 0)),
legend.title = element_blank(),
legend.position = "top"
)
penguin_plot
penguin_plot <- penguin_plot +
scale_fill_manual(
values = generate_palette(
"orange",
"go_both_ways",
n_colours = 3
)
)
penguin_plot
Here’s an example using {monochromeR}’s
generate_palette() to generate all the colours used in the
plot, resulting in a more polished look with minimal effort.
penguin_palette <- generate_palette(
c(15, 75, 99),
modification = "go_both_ways",
n_colours = 8,
view_palette = TRUE,
view_labels = FALSE
)
penguins |>
ggplot() +
geom_point(
aes(
x = flipper_len,
y = bill_len,
fill = species,
colour = species,
size = body_mass
),
alpha = 0.8,
shape = 21,
stroke = 0.2
) +
labs(
title = "Perfectly proportional penguins",
subtitle = paste(
"Each dot represents a penguin.",
"The bigger the dot, the heavier the penguin. Look at them go!"
),
x = "Flipper length (mm)",
y = "Bill length (mm)"
) +
scale_size(guide = "none") +
scale_fill_manual(values = penguin_palette[c(1, 3, 5)]) +
scale_colour_manual(values = penguin_palette[c(8, 8, 1)]) +
theme_minimal() +
theme(
text = element_text(family = "Karla", colour = "white"),
legend.title = element_blank(),
legend.position = "top",
plot.background = element_rect(
fill = penguin_palette[8],
colour = penguin_palette[8]
),
panel.grid = element_line(colour = penguin_palette[7]),
panel.background = element_rect(
fill = penguin_palette[8],
colour = penguin_palette[8]
),
axis.text = element_text(colour = penguin_palette[1]),
plot.title = element_text(
colour = penguin_palette[1],
hjust = 0,
size = rel(1.5),
face = "bold"
),
plot.subtitle = ggtext::element_textbox_simple(
colour = "white",
margin = margin(6, 0, 12, 0),
hjust = 0
),
plot.margin = margin_auto(24)
)
I’ve done my best to make the functions in this package user-friendly, and to make the error messages easy to understand. If you come across a bug or an error message that doesn’t make sense, or if there’s something you think would make this package better, please let me know!