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.

Sebastian Sauer
  • 1,555
  • 15
  • 24
  • 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: https://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 '18 at 19:55

1 Answers1

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")
}
randy
  • 1,031
  • 10
  • 20
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • I am worried to try this, as I don't know how to revert it to the default. @[ben-bolker](https://stackoverflow.com/users/190277/ben-bolker) any thoughts on that? – massisenergy Dec 12 '18 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 '18 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 '18 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) – Sebastian Sauer Dec 12 '18 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. – Sebastian Sauer Dec 13 '18 at 09:18
  • I use the 'paired' palette and would want to move one discrete jump up, how would I do that? I wanna start with the dark blue not the light blue. Do you happen to know? Thank you! – canIchangethis Nov 14 '22 at 17:08
  • @canIchangethis, please post that as a new question (linking to this one if appropriate), with a [mcve] ... – Ben Bolker Nov 14 '22 at 17:10
  • I thought it is close enough but will do. Thank you! – canIchangethis Nov 14 '22 at 17:34
  • Here is my new question @BenBolker https://stackoverflow.com/questions/74438348/r-ggplot-colorscale-how-to-skip-first-discrete-color-in-a-scale – canIchangethis Nov 14 '22 at 22:00