Home > Net >  Equivalent of theme_set for scale_color/scale_fill?
Equivalent of theme_set for scale_color/scale_fill?

Time:03-19

I am working on a small Corporate Design package for ggplot visualizations and was wondering if there was an equivalent of theme_set() for scale_color and scale_fill. Thus instead of the standard palette functions a custom continuous or discrete palette in CD-colors is choosen.

Otherwise: Is there a good entry point to overwrite a function of ggplot2 from an external package to provide this function? I came up with ggplot() function like:

cd_ggplot_continuous <- function(...) {
  ggplot(...)  
    cd_scale_color_continuous(...)  
    cd_scale_fill_continuous(...)
}

and

cd_ggplot_discrete <- function(...) {
   ggplot(...)  
     cd_scale_color_discrete(...)  
     cd_scale_fill_discrete(...)
}

Though I am not a big fan of such a solution.

CodePudding user response:

You can set the default scale object in options.

Here is a vanilla ggplot:

p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species))   geom_point()

p

enter image description here

We set a default color palette like this:

options(ggplot2.discrete.colour = c("purple2", "blue3", "red4")) 

So drawing any plot that uses a discrete color scale (unless otherwise specified) will use this palette.

p

enter image description here

  • Related