Skip to content

Instantly share code, notes, and snippets.

@bradlindblad
Created March 4, 2019 20:08
Show Gist options
  • Save bradlindblad/7e0882a9d129f208b1ca6be09c36dab7 to your computer and use it in GitHub Desktop.
Save bradlindblad/7e0882a9d129f208b1ca6be09c36dab7 to your computer and use it in GitHub Desktop.
full_colors.R
# Palette main colors
ketch.styles <- c(
`salmon` = "#F16876",
`light_blue`= "#00A7E6",
`light_grey` = "#E8ECF8",
`brown` = "#796C68")
# Fn to extract them by hex codes
styles <- function(...) {
cols <- c(...)
if (is.null(cols))
return (ketch.styles)
ketch.styles[cols]
}
# Create separate palettes
ketch.palettes <- list(
`main` = styles('salmon','light_blue', 'brown', 'light_grey'),
`cool` = styles('light_blue', 'light_grey')
)
# Fn to access them
ketch_pal <- function(palette = "main", reverse = FALSE, ...) {
pal <- ketch.palettes[[palette]]
if (reverse) pal <- rev(pal)
colorRampPalette(pal, ...)
}
# Fn for customer scale
scale_color_ketch <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) {
pal <- ketch_pal(palette = palette, reverse = reverse)
#' Scale color using AgC color palette.
#' @param palette: main, greens or greys
#' @param discrete: T or F
#' @param reverse: reverse the direction of the color scheme
if (discrete) {
discrete_scale("colour", paste0("ketch_", palette), palette = pal, ...)
} else {
scale_color_gradientn(colours = pal(256), ...)
}
}
scale_fill_ketch <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) {
#' Scale fill using AgC color palette.
#' @param palette: main, greens or greys
#' @param discrete: T or F
#' @param reverse: reverse the direction of the color scheme
pal <- ketch_pal(palette = palette, reverse = reverse)
if (discrete) {
discrete_scale("fill", paste0("ketch_", palette), palette = pal, ...)
} else {
scale_fill_gradientn(colours = pal(256), ...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment