Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot: How to set default color for all geoms?

Tags:

r

ggplot2

I'm trying to set the default color for all geoms in a ggplot to something other than black. Note this is not about setting scale_color...

Simple example:

# linear model with confidence bands...
set.seed(1)
df <- data.frame(x=1:50, y=5 + 2*(1:50)+rnorm(50,sd=10))
lm <- lm(y~x,df)
se <- summary(lm)$sigma           # standard error of fit
Z  <- qnorm(0.05/2,lower.tail=F)  # 95% confidence bands
df <- cbind(df,predict(lm,se.fit=T)[c("fit","se.fit")])
# plot the result...
library(ggplot2)
ggplot(df, aes(x=x)) + 
  geom_point(aes(y=y), size=3) +
  geom_line(aes(y=fit)) +
  geom_line(aes(y=fit+Z*se.fit), linetype=2)+
  geom_line(aes(y=fit-Z*se.fit), linetype=2)

Now, suppose I want to make everything red. Leaving aside the advisability of doing that, I would think ggplot(df, aes(x=x), colour="red") would do it. But the colour= parameter seems to be ignored: everything is still black. I can add colour="red" to every geom_ call, but I'm trying to avoid that.

Edit: Using ggplot(df, aes(x=x, color="red")) is not an option because it creates a color scale using the default ggplot palette (evenly spaced around an HSL color circle). With one color, this is #F8766D, which happens to be light red. In addition, this creates a legend which then must be hidden.

like image 721
jlhoward Avatar asked Jan 16 '14 22:01

jlhoward


People also ask

What is the default Ggplot color palette?

By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.

How do I specify colors in ggplot2?

To specify colors of the bar in Barplot in ggplot2, we use the scale_fill_manual function of the ggplot2 package. Within this function, we need to specify a color for each of the bars as a vector. We can use colors using names as well as hex codes.

What is fill in Ggplot R?

Generally, fill defines the colour with which a geom is filled, whereas colour defines the colour with which a geom is outlined (the shape's "stroke", to use Photoshop language).


2 Answers

You can set a default color for each geometry type this way:

update_geom_defaults("point",   list(colour = "red"))
update_geom_defaults("line",   list(colour = "red"))

ggplot(df, aes(x=x)) + 
  geom_point(aes(y=y), size=3) +
  geom_line(aes(y=fit)) +
  geom_line(aes(y=fit+Z*se.fit), linetype=2)+
  geom_line(aes(y=fit-Z*se.fit), linetype=2)

Edit If you want to do to everything then use (Edit borrow from here):

params <- ls(pattern = '^geom_', env = as.environment('package:ggplot2'))
geoms <- gsub("geom_", "", params)

lapply(geoms, update_geom_defaults, list(colour = "red"))
lapply(geoms, update_geom_defaults, list(fill = "red", colour = "red")) ## include fills 

If you want to set the default colour for the just one plot, simply do:

ggplot(df, aes(x=x, colour="red")) + 
  geom_point(aes(y=y), size=3) +
  geom_line(aes(y=fit)) +
  geom_line(aes(y=fit+Z*se.fit), linetype=2)+
  geom_line(aes(y=fit-Z*se.fit), linetype=2)
like image 57
metasequoia Avatar answered Oct 18 '22 18:10

metasequoia


In order to replace a geom default aesthetic with another one (for all geoms using that aesthetic), you can try the following code.

First define a function for getting default aes settings of all geoms from ggplot2

library(ggplot2)
library(purrr)

geom_aes_defaults <- function() {
  geom_names <- apropos("^Geom", ignore.case = FALSE)
  geoms <- mget(geom_names, env = asNamespace("ggplot2"))
  map(geoms, ~ .$default_aes)
}

With geom_aes_defaults() you obtain a long list of all geom aesthetic mappings

$Geom
Aesthetic mapping:
<empty>

$GeomAbline
Aesthetic mapping:
* `colour`   -> "black"
* `size`     -> 0.5
* `linetype` -> 1
* `alpha`    -> NA

$GeomAnnotationMap
Aesthetic mapping:
* `colour`   -> "NA"
* `fill`     -> "grey20"
* `size`     -> 0.5
* `linetype` -> 1
* `alpha`    -> NA

$GeomArea
Aesthetic mapping:
* `colour`   -> NA
* `fill`     -> "grey20"
* `size`     -> 0.5
* `linetype` -> 1
* `alpha`    -> NA

...

The following function iterates over all geoms matching a given aesthetic and substitutes the corresponding values

replace_geom_aes_defaults <- function(name, old_aes, new_aes) {
  matching_geoms <- 
    map(geom_aes_defaults(), name) %>%
      compact() %>%
      keep(~ !is.na(.) & . == old_aes)
  geoms <- gsub("^Geom(.*)", "\\1", names(matching_geoms))
  walk(geoms, update_geom_defaults, setNames(list(new_aes), name))
}

Now you can replace colors systematically, e.g. turn black into red by

replace_geom_aes_defaults("colour", "black", "red")

or even replace fill colors (for bar plots) by

replace_geom_aes_defaults("fill", "grey35", "red")
like image 23
spren9er Avatar answered Oct 18 '22 18:10

spren9er