knitr::opts_chunk$set(fig.width = 7, echo = FALSE, warning = FALSE, message=FALSE)
library(tidyverse) library(sotoncolours) library(dichromat) library(colorspace) library(palmerpenguins) library(raster) library(patchwork)
The sotoncolours package contains colour palettes based on the University of Southampton colour scheme.
devtools::install_github("ab604/sotoncolours", build_vignettes = TRUE)
The package contains 19 colours and 7 colour palettes contained within
a named character vector called uos_colours
:
<<<<<<< HEAD
# Named character vector of the colours
uos_colours
# See names of all palettes names(uos_palettes)
To see the colours in a palette by name
# See names of all palettes names(uos_palettes$contrast1)
pal_names <- names(uos_palettes) par(mfrow=c(length(uos_palettes)/2, 2), lheight = 2, mar=rep(1, 4), adj = 0) for (i in 1:length(uos_palettes)){ viz_palette(uos_palettes[[i]], pal_names[i]) }
These plots show how each of the palettes perform for different types of colour vision impairment (using dichromat).
par(mfrow=c(length(uos_palettes)/2, 2), lheight = 2, mar=rep(1, 4), adj = 0) for (i in 1:length(uos_palettes)){ viz_palette(dichromat(uos_palettes[[i]], "deutan"), pal_names[i]) }
par(mfrow=c(length(uos_palettes)/2, 2), lheight = 2, mar=rep(1, 4), adj = 0) for (i in 1:length(uos_palettes)){ viz_palette(dichromat(uos_palettes[[i]], "protan"), pal_names[i]) }
par(mfrow=c(length(uos_palettes)/2, 2), lheight = 2, mar=rep(1, 4), adj = 0) for (i in 1:length(uos_palettes)){ viz_palette(dichromat(uos_palettes[[i]], "tritan"), pal_names[i]) }
par(mfrow=c(length(uos_palettes)/2, 2), lheight = 2, mar=rep(1, 4), adj = 0) for (i in 1:length(uos_palettes)){ viz_palette(desaturate(uos_palettes[[i]]), pal_names[i]) }
Colours can be called directly using uos_cols()
with a character argument
for the colour name e.g. "horizon5"
.
ggplot(mtcars, aes(hp, mpg)) + geom_point(color = uos_cols("horizon5"), size = 4, alpha = .8)
The package contains color scale functions for ggplot2
plots: scale_color_uos()
and scale_fill_uos()
calling one of the palettes.
Here the marine
and horizon5
palettes are used for a scatter plot of mtcars
data.
# Use the marine palette for colours p1 <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(colour = factor(cyl)), size=2, alpha=0.7) + scale_colour_uos(palette="marine") + theme_bw() + theme(aspect.ratio=1, legend.position="bottom") # Use the horizon palette for colours p2 <- ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class), alpha=0.7) + scale_colour_uos(palette="horizon") + theme_bw() + theme(aspect.ratio=1, legend.position="bottom") p1 + p2
Here the default palette marine
is used in a stacked bar chart using the diamonds
data.
ggplot(diamonds) + geom_bar(aes(x = cut, fill = clarity)) + scale_fill_uos() + # defaults to marine theme_bw()
It's possible, but messy, to pick any colour combination from uos_colours
,
but the syntax requires extracting the hex codes using unname()
and subsetting
uos_cols
using a character vector of the desired colour names.
It may be easier to create an object for any bespoke palette and pass then
pass it to the respective ggplot
function.
Here's an example with the Palmer penguin data using
unname(uos_colours[c("black","shamrock","horizon4")])
as described
above by creating a new palette called my_cols
that is passed to the
values
argument of scale_colour_manual()
.
# Create a new vector of colours to pass to scale_colour manual # Use unname() to get hex codes from uos_cols() my_cols <- unname(uos_colours[c("black","shamrock","horizon4")]) ggplot(data = penguins, aes(x = species, y = flipper_length_mm)) + geom_boxplot(aes(color = species), width = 0.3, show.legend = FALSE) + geom_jitter(aes(color = species), alpha = 0.7, show.legend = FALSE, position = position_jitter(width = 0.2, seed = 0)) + scale_colour_manual(values = my_cols) + # pass my_cols to scale_colour_manual theme_minimal() + labs(x = "Species", y = "Flipper length (mm)")
Here is a base R plot
using the horizon
palette directly via the colorRampPalette()
function.
pal <- colorRampPalette(uos_palettes[["horizon"]]) image(volcano, col = pal(20))
Here is the same plot, but by creating a new palette from specific colours as with the penguin data.
# Create new palette my_cols <- unname(uos_cols()[c('marine1','shamrock','horizon3')]) pal <- colorRampPalette(my_cols) image(volcano, col = pal(20))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.