Skip to content

Instantly share code, notes, and snippets.

@larnsce
Last active July 27, 2020 09:36
Show Gist options
  • Save larnsce/c9efc871933f90a4b139af6256208353 to your computer and use it in GitHub Desktop.
Save larnsce/c9efc871933f90a4b139af6256208353 to your computer and use it in GitHub Desktop.
Set default colour/fill scale functions via global options
# source: https://twitter.com/yutannihilat_en/status/1287402204987396096
# reference: https://ggplot2.tidyverse.org/reference/scale_colour_discrete.html
library(ggplot2)
# functions to define scaling options ----------------
scale_fill_statZH <- function() {
ggplot2::scale_fill_manual(values = statR::zhpal$zhdiagonal)
}
scale_colour_statZH <- function() {
ggplot2::scale_colour_manual(values = statR::zhpal$zhdiagonal)
}
# ggplot2 scaling options ---------------------------------
options(
ggplot2.discrete.fill = scale_fill_statZH,
ggplot2.discrete.colour = scale_colour_statZH
)
# examples ---------------------------------------------
## fill scale
ggplot(mpg, aes(class, fill = drv)) +
geom_bar()
## colour scale
ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment