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.colour
—viridis
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
Calculating Percentile of Dataset Column
Find Names of Columns Which Contain Missing Values
How to Manually Change the Key Labels in a Legend in Ggplot2
Count the Number of Non-Zero Elements of Each Column
Get Margin Line Locations in Log Space
Extract Part of String Before the First Semicolon
Combinations of Multiple Vectors in R
Aggregating All Unique Values of Each Column of Data Frame
How to Divide a Number of Columns by One Column
Avoid Scientific Notation in Cut Function in R
Using R to Download Zipped Data File, Extract, and Import .Csv
In R Plotly Subplot Graph, How to Show Only One Legend
Convert Quarter/Year Format to a Date
Trouble Passing on an Argument to Function Within Own Function