Ggplot: How to Set Default Color for All Geoms

How to change default color scheme in ggplot2?

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")
}

Change geom default aesthetics as part of theme component only

The released version of ggplot2 doesn't currently offer a way to do this. However, this is a fairly old feature request and has been under development since the summer of 2018.

ggplot2: How do I set the default fill-colour of geom_bar() in a theme

you can't do it in a theme (sadly).

You want to change the default settings of a geom,

  update_geom_defaults("bar",   list(fill = "red"))

and you can also change a default scale, e.g.

  scale_colour_continuous <- function(...) 
scale_colour_gradient(low = "blue", high = "red", na.value="grey50", ...)

Setting Defaults for geoms and scales ggplot2

There is another method for this now. You can essentially overwrite any aesthetics scale, for example:

scale_colour_discrete <- function(...) scale_colour_brewer(..., palette="Set2")
scale_fill_discrete <- function(...) scale_fill_brewer(... , palette="Set2")

Now, your aesthetics will be coloured or filled following that behaviour.'

As per: https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/w0Tl0T_U9dI

With respect to defaults to geoms, you can use update_geom_defaults, for example:

update_geom_defaults("line",   list(size = 2))


Related Topics



Leave a reply



Submit