Skip to content

Instantly share code, notes, and snippets.

@uribo
Last active June 12, 2019 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uribo/bc5df991469c8024cc3db78aa669df7a to your computer and use it in GitHub Desktop.
Save uribo/bc5df991469c8024cc3db78aa669df7a to your computer and use it in GitHub Desktop.
Data Science Color Palette
library(ggplot2)
theme_set(theme_light(base_size = 14,
base_family = dplyr::if_else(grepl("mac", sessioninfo::os_name()),
"IPAexGothic",
"IPAGothic")))
ds_col <- function(...) {
.ds_cols <-
c(`cave` = "#F4A935",
`crater` = "#E94E77",
`forest` = "#7BA085",
`grain` = "#E1D5A3",
`river` = "#4886a5")
cols <- c(...)
if (rlang::is_null(cols))
return(.ds_cols)
.ds_cols[cols]
}
ds_palettes <- list(`main` = ds_col())
ds_pal <- function(palette = "main", reverse = FALSE, ...) {
pal <- ds_palettes[[palette]]
if (reverse) pal <- rev(pal)
colorRampPalette(pal, ...)
}
scale_color_ds <- function(palette = "main", discrete = TRUE, reverse = FALSE, na.value = "gray60", ...) {
pal <- ds_pal(palette = palette, reverse = reverse)
if (discrete) {
discrete_scale("colour", paste0("dsview_", palette), palette = pal, na.value = na.value, ...)
} else {
scale_color_gradientn(colours = pal(256), na.value = na.value, ...)
}
}
scale_fill_ds <- function(palette = "main", discrete = TRUE, reverse = FALSE, na.value = "gray60", ...) {
pal <- ds_pal(palette = palette, reverse = reverse)
if (discrete) {
discrete_scale("fill", paste0("dsview_", palette), palette = pal, na.value = na.value, ...)
} else {
scale_fill_gradientn(colours = pal(256), na.value = na.value, ...)
}
}
theme_ds_dark <- function() {
theme_light() %+replace%
theme(plot.background = element_rect(fill = "#272728"),
legend.background = element_rect(fill = "#272728", color = NA),
plot.title = element_text(color = "#FEFEFE"),
axis.title = element_text(color = "#FEFEFE"),
axis.text = element_text(color = "#FEFEFE"),
panel.background = element_rect(fill = "#303031",
colour = NA),
panel.grid = element_line(color = "#FEFEFE", linetype = "dotted", size = 0.2),
strip.background = element_rect(fill = "#717297"),
strip.text = element_text(color = "#FEFEFE"),
legend.title = element_text(color = "#FEFEFE"),
legend.text = element_text(color = "#FEFEFE"),
legend.key = element_rect(fill = "#272728", color = NA),
legend.box.background = element_rect(fill = "#272728", color = NA),
complete = TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment