knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(tidyverse) library(cccolr)
This package contains functions to conveniently use Chrestos colors and Chrestos color palettes in R.
Hex codes of the Chrestos colors are given by the function chrestos_colors()
.
# all implemented colors chrestos_colors() # or just a subset chrestos_colors("turq", "green", "purple")
The shade of the colors can be adjusted by the argument shade
. A shade
below 0.5
leads to darker colors while a shade
above 0.5
makes the colors brighter.
# change the shade of all selected colors chrestos_colors("turq", "green", shade = 0.7) # use different shades for the selected colors chrestos_colors("turq", "green", shade = c(0.7, 0.3)) # one color in different shades chrestos_colors("turq", shade = seq(0, 1, 0.2))
Various color palettes based on the Chrestos colors are implemented in the function chrestos_palettes()
as a named list.
head(chrestos_palettes(), n = 3)
To get a preview of all implemented palettes use
preview_palettes()
Some of these palettes make sense for a discrete scale, while others are more suitable for a continuous scale. For a preview of the color palettes on a continuous scale use
preview_palettes(discrete = FALSE)
The Chrestos color palettes can be added to the color
aesthetic of a ggplot with the layer function scale_color_chrestos()
.
# palette 'full_color' (6 colors) on a discrete color aesthetic with 3 levels iris %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point(size = 4, alpha = 0.8) + scale_color_chrestos(palette = "full_color") + theme_light()
All color palettes can be used for discrete variables with any number of levels. If the number of levels falls below the number of colors in the palette, only the first colors are used. If the number of levels exceeds the number of colors, the color vector is linearly interpolated, which might or might not look good, depending on the selected palette (but at least there is no error).
# palette 'minimal' (3 colors) on a discrete color aesthetic with 7 levels diamonds %>% ggplot(aes(x = price, y = carat, color = color)) + geom_point(size = 2, alpha = 0.5) + scale_color_chrestos(palette = "minimal") + theme_light()
For a continuous color variable, the argument discrete
has to be set to FALSE
. Additional arguments that are valid for the underlying functions of scale_color_chrestos()
(scale_color_gradientn()
in the continuous case or discrete_scale()
in the discrete case) can also be passed. Here, the continuous color scale is log2-transformed.
# palette 'grad_turq' on a continuous color aesthetic with a log2 trafo midwest %>% ggplot(aes(x = area, y = poptotal, color = popdensity)) + geom_point(size = 4, alpha = 0.7) + scale_y_continuous(trans = "log2") + scale_color_chrestos(palette = "grad_turq", discrete = FALSE, trans="log2") + theme_light()
The color palettes can also be added to the fill
aesthetic of a ggplot with the layer functions scale_fill_chrestos()
. It works exactly the same way than the scale_color_chrestos()
function.
faithfuld %>% ggplot(aes(x = waiting, y = eruptions, fill = density)) + geom_raster(interpolate = TRUE) + scale_fill_chrestos(palette = "grad4", discrete = FALSE) + theme_void()
All color palettes can be reversed by setting the argument reverse
to TRUE
. The overall shade of the palette can be adjusted by the shade
argument. Both also works for scale_color_chrestos()
.
faithfuld %>% ggplot(aes(x = waiting, y = eruptions, fill = density)) + geom_raster(interpolate = TRUE) + scale_fill_chrestos(palette = "grad4", discrete = FALSE, reverse = TRUE, shade = 0.7) + theme_void()
Custom palettes can be used in scale_color_chrestos()
and scale_fill_chrestos()
by creating a named list of one or more color palette(s) and using the customPalettes
argument.
# custom palette my_palettes<- list(theonlyone = c("dodgerblue", "forestgreen", "firebrick", "gold")) iris %>% ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) + geom_point(size = 4, alpha = 0.8) + scale_color_chrestos(palette = "theonlyone", customPalettes = my_palettes) + theme_light()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.