10

I would like to change the default color scheme in ggplot2. That is, I would like to define a color scheme (say: viridis) at the one point in the script so that all subsequent ggplot diagrams will use this color scheme without having to call + scale_color_viridis() each time.

I've seen this SO post featuring update_geom_defaults(geom, new), but I could not find a way to explain this function to use a scheme such as viridis.

I have also tried to update the ggplot color, similar to this post, but, as @baptise pointed out, this approach does not really work.

In short:

  1. define new color scheme, eg., viridis

  2. call ggplot subsequently without adding + scale_color_viridis() but still this ggplot diagram uses the viridis color scheme.

1
  • As that other question already indicates, this is not really a feature ggplot supports. Basically it seems like the request as the existing feature request: github.com/tidyverse/ggplot2/issues/2691. The idea would be to set default scales with the theme that you can customize. But this still does not exist in production.
    – MrFlick
    Dec 12, 2018 at 19:55

1 Answer 1

6

It looks like

options(ggplot2.continuous.colour="viridis")

will do what you want (i.e. ggplot will look for a colour scale called

scale_colour_whatever

where whatever is the argument passed to ggplot2.continuous.colourviridis in the above example).

library(ggplot2)
opts <- options(ggplot2.continuous.colour="viridis")
dd <- data.frame(x=1:20,y=1:20,z=1:20)

ggplot(dd,aes(x,y,colour=z))+geom_point(size=5)
options(oldopts) ## reset previous option settings

For discrete scales, the answer to this question (redefine the scale_colour_discrete function with your chosen defaults) seems to work well:

scale_colour_discrete <- function(...) {
  scale_colour_brewer(..., palette="Set1")
}
9
  • I am worried to try this, as I don't know how to revert it to the default. @ben-bolker any thoughts on that? Dec 12, 2018 at 20:07
  • Shouldn't be an issue after restarting. If you wanted to make it more permament, you could add the line to your rprofile.
    – Ben G
    Dec 12, 2018 at 20:23
  • see my edit that restores original options. You could also look at ?scale_colour_continuous to see that the default option is "gradient" ...
    – Ben Bolker
    Dec 12, 2018 at 20:25
  • Thanks, that worked. However, I could not find a similar variable for discrete color scales (the continuous kind of works too for discrete colors, as you showed in your example, but a discrete scale would be better) Dec 12, 2018 at 21:12
  • @ben-bolker: Thanks again. I just discovered that ggplot2 uses viridis as a default for ordered factors: mtcars$am_f <- ordered(mtcars$am); ggplot(mtcars, aes(am, cyl, color = factor(am))) + geom_boxplot(). That's helpful, because I could not yet convince to redefine scale_colour_discrete() to use viridis in sensible way. Dec 13, 2018 at 9:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.