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%}

Scaling Colors or Fill in 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().

Scaling Colors

library(ggplot2)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 2) +
  theme_light() +
  scale_color_hurwitz(palette = "distinguish")

Scaling Fill

ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
  geom_boxplot() +
  theme_light() +
  scale_fill_hurwitz(palette = "shallow_ocean")

Scales for Continuous Variable

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"))

Reversing a Palette

ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
  geom_boxplot() +
  theme_light() +
  scale_fill_hurwitz(palette = "shallow_ocean", reverse = TRUE)

Accessing Colors and Palettes

Names and Hex Codes

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")

Previewing Palettes

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)


schackartk/hurwitzLab documentation built on Dec. 22, 2021, 10:21 p.m.