Illustration by W. E. B. Du Bois, Courtesy Library of Congress
Build Custom Color Scales
dubois_colors <-function(...) {# define colors dubois_cols <-c(`black`="#000000",`purple`="#582f6c",`violet`="#94679C",`pink`="#ef849f",`softred`="#f4b7a7",`iceblue`="#bccbf3",`palegrey`="#e4e4e4" )# if no colors are specified, return all cols <-c(...)if (is.null(cols)) return (dubois_cols)# if colors are specified, return those dubois_cols[cols]}
dubois_pal_d <-function(palette ="default", reverse =FALSE) {# nested function to return colors via `dubois_pal_d()(n)`function(n) {# check if number of colors is sufficientif(n >5) stop('Palettes only contain 5 colors')# check argumentsif (!palette %in%c("default", "dark", "light")) stop('palette should be "default", "dark" or "light".')if (!is.logical(reverse) &!is.numeric(reverse)) stop('reverse should be logical or numeric')# define palette stylesif (palette =="default") { pal <-dubois_colors("black", "violet", "softred", "iceblue", "palegrey")[1:n] }if (palette =="dark") { pal <-dubois_colors(1:5)[1:n] }if (palette =="light") { pal <-dubois_colors(3:7)[1:n] }# return unnamed vector of color codes pal <-unname(pal)# check reverse argumentif (reverse) rev(pal) else pal }}
scale_color_dubois_d <-function(palette ="default", reverse =FALSE, ...) {# check argumentsif (!palette %in%c("default", "dark", "light")) stop('palette should be "default", "dark" or "light".')if (!is.logical(reverse) &!is.numeric(reverse)) stop('reverse should be logical or numeric')# retrieve color set pal <-dubois_pal_d(palette = palette, reverse = reverse)# apply to discrete scale ggplot2::discrete_scale("colour", paste0("dubois_", palette), palette = pal, ...)}
Build Custom Color Scales: Categorical
scale_color_dubois_d <-function(palette ="default", reverse =FALSE, ...) {# check argumentsif (!palette %in%c("default", "dark", "light")) stop('palette should be "default", "dark" or "light".')if (!is.logical(reverse) &!is.numeric(reverse)) stop('reverse should be logical or numeric')# retrieve color set pal <-dubois_pal_d(palette = palette, reverse = reverse)# apply to discrete scale ggplot2::discrete_scale("colour", paste0("dubois_", palette), palette = pal, ...)}scale_fill_dubois_d <-function(palette ="default", reverse =FALSE, ...) {if (!palette %in%c("default", "dark", "light")) stop('palette should be "default", "dark" or "light".')if (!is.logical(reverse) &!is.numeric(reverse)) stop('reverse should be logical or numeric') pal <-dubois_pal_d(palette = palette, reverse = reverse) ggplot2::discrete_scale("fill", paste0("dubois_", palette), palette = pal, ...)}
dubois_pal_c <-function(palette ="dark", reverse =FALSE, ...) {# check argumentsif (!palette %in%c("dark", "light")) stop('palette should be "dark" or "light".')if (!is.logical(reverse) &!is.numeric(reverse)) stop('reverse should be logical or numeric')# define palette styles dubois_palettes <-list(`dark`=dubois_colors("black", "purple", "violet", "pink"),`light`=dubois_colors("purple", "violet", "pink", "palegrey") )# retrieve color set as unnamed vector pal <- dubois_palettes[[palette]] pal <-unname(pal)# check reverse argumentif (reverse) pal <-rev(pal)# create a color gradient with n colors grDevices::colorRampPalette(pal, ...)}
scale_color_dubois_c <-function(palette ="dark", reverse =FALSE, ...) {# check function argumentsif (!palette %in%c("dark", "light")) stop('Palette should be "dark" or "light".')if (!is.logical(reverse) &!is.numeric(reverse)) stop('reverse should be logical or numeric.')# apply color set to ggplot's gradientn scale pal <-dubois_pal_c(palette = palette, reverse = reverse) ggplot2::scale_color_gradientn(colours =pal(256), ...)}
Build Custom Color Scales: Sequential
scale_color_dubois_c <-function(palette ="dark", reverse =FALSE, ...) {# check function argumentsif (!palette %in%c("dark", "light")) stop('Palette should be "dark" or "light".')if (!is.logical(reverse) &!is.numeric(reverse)) stop('reverse should be logical or numeric.')# apply color set to ggplot's gradientn scale pal <-dubois_pal_c(palette = palette, reverse = reverse) ggplot2::scale_color_gradientn(colours =pal(256), ...)}scale_fill_dubois_c <-function(palette ="dark", reverse =FALSE, ...) {if (!palette %in%c("dark", "light")) stop('Palette should be "dark" or "light".')if (!is.logical(reverse) &!is.numeric(reverse)) stop('reverse should be logical or numeric.') pal <-dubois_pal_c(palette = palette, reverse = reverse) ggplot2::scale_fill_gradientn(colours =pal(256), ...)}
Use Your Custom Color Scales: Sequential
ggplot(filter(bikes, is_weekend ==TRUE),aes(x = temp_feel, y = count, color = humidity)) +geom_point(alpha = .7) +scale_color_dubois_c()
Use Your Custom Color Scales: Sequential
ggplot(filter(bikes, is_weekend ==TRUE),aes(x = temp_feel, y = count, color = humidity)) +geom_point(alpha = .7) +scale_color_dubois_c(palette ="light",reverse =TRUE )