knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 5 )
library(hurwitzLab)
This package provides palettes and scales based on these colors:
library(dplyr) library(gt) hurwitz_colors <- get_hurwitz_colors() tibble( name = names(hurwitz_colors), color_hex = hurwitz_colors, ) %>% gt() %>% data_color(columns = "color_hex", colors = scales::col_factor(palette = hurwitz_colors, domain = hurwitz_colors, ordered = TRUE)) %>% tab_header(title = "hurwitzLab colors") %>% cols_label(name = "Name", color_hex = "Color")
The following palettes are included:
{width=75% height=75%}
ggplot2
Scale functions are provided which can be added directly to a
gplot2 object. Usage is similar to rColorBrewer::scale_color_brewer()
or viridis::scale_color_viridis()
.
library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point(size = 2) + theme_light() + scale_color_hurwitz(palette = "distinguish")
ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) + geom_boxplot() + theme_light() + scale_fill_hurwitz(palette = "shallow_ocean")
A pallette can be applied to a continuous variable by setting discrete = FALSE
:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Petal.Length)) + geom_point() + theme_light() + scale_color_hurwitz(discrete = FALSE, palette = "classic")
Colors to use can be manually assigned using ggplot2::scale_color_gradientn()
. To access colors we can use get_hurwitz_colors()
which is detailed below.
library(scales) ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Width)) + geom_point() + theme_light() + scale_color_gradientn(colors = get_hurwitz_colors("red", "teal"))
ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) + geom_boxplot() + theme_light() + scale_fill_hurwitz(palette = "shallow_ocean", reverse = TRUE)
All colors in this package can be accessed by calling get_hurwitz_colors()
:
get_hurwitz_colors()
Alternatively, the hex code of specific colors can be accessed:
get_hurwitz_colors("charcoal", "teal")
To see the colors and their hex codes in a specific palette:
hurwitz_pal("distinguish2")
Palettes can be previewed using the function display_hurwitz_pal()
:
display_hurwitz_pal("main")
You can also see which colors will be used when the number of categories is different from the number of colors in a pallette by specifying n
.
display_hurwitz_pal("main", 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.