10

For a custom ggplot2 theme I'd like to change the default aesthetic of some geom, say I want red dots instead of black dots.

From this answer I know we can change defaults for a geom using the function update_geom_default but I wonder if it is possible to change the colour only when we call theme_red_dots?

Example of my naive attempt:

library(ggplot2)

theme_red_dots <- function(...) {
  update_geom_defaults("point", list(colour = "red"))
  theme_minimal() +
    theme(...)
}

Looks good here:

ggplot(mtcars, aes(mpg, disp)) + 
  geom_point() + 
  theme_red_dots()

enter image description here

But I'd like the points to be black again when I call

ggplot(mtcars, aes(mpg, disp)) + 
  geom_point()

enter image description here

Thanks in advance!


Below is an example of why I thought this could be useful. We can change panel.background to be black fairly easy but this would make it impossible to see the points if we don't map an aesthetic to colour. (The usefulness of this theme_black can certainly be discussed, but I would like to avoid an argument about that.)

theme_black <- function(...) {
  theme_minimal() +
    theme(panel.background = element_rect(fill = "black")) +
    theme(...)
}

# update_geom_defaults("point", list(colour = "black"))
ggplot(mtcars, aes(mpg, disp)) + 
  geom_point() + 
  theme_black()

enter image description here

Changing the colour of the points inside geom_point() is an option here (see @zx8754 answer), but this requires the user of theme_black() to change it, while I am wondering if there is a way to do this right inside theme_*.

markus
  • 25,843
  • 5
  • 39
  • 58
  • 1
    Why change defaults? Maybe just make red dots within `theme_red_dots` – zx8754 Jan 09 '19 at 09:28
  • 2
    Don't know if this helps, but, I did some research by looking at how `ggthemr` did it, and they too are using `update_geom_defaults` (line 54 https://github.com/cttobin/ggthemr/blob/master/R/ggthemr.R), so probably it is the only way to go? – RLave Jan 11 '19 at 10:28

4 Answers4

5

Another solution is to detach and reattach ggplot2 (apparently you can do this within custom ggplot2 theme function).

library(ggplot2)

theme_red_dots <- function(...) {
    # wanted theme
    update_geom_defaults("point", list(colour = "red"))

    # Plot
    p <- theme_minimal() + 
        theme(...)

    # Detach and reattach ggplot2
    detach("package:ggplot2", unload = TRUE); library(ggplot2)

    # Return wanted plot
    return(p)
}
# red dots
ggplot(mtcars, aes(mpg, disp)) + 
    geom_point() + 
    theme_red_dots()
# black (default) dots
ggplot(mtcars, aes(mpg, disp)) + 
    geom_point()

Works with wanted theme_black too:

theme_black <- function(...) {
    update_geom_defaults("point", list(colour = "red"))
    p <- theme_minimal() +
        theme(panel.background = element_rect(fill = "black")) +
        theme(...)
    detach("package:ggplot2", unload = TRUE); library(ggplot2)
    return(p)
}

# Plots with black background
ggplot(mtcars, aes(mpg, disp)) + 
    geom_point() + 
    theme_black()

# Plots with default background
ggplot(mtcars, aes(mpg, disp)) + 
    geom_point()
pogibas
  • 27,303
  • 19
  • 84
  • 117
  • 1
    Really interesting! I'm surprised this works. Would have assumed re-attaching **ggplot2** would have reset the default colour also for the existing plots. – Mikko Marttila Jan 16 '19 at 16:06
3

Instead of changing defaults, make custom geom_point:

library(ggplot2)

# make custom geom with red as default
geom_point_red <- function()geom_point(col = "red")

ggplot(mtcars, aes(mpg, disp)) + 
  geom_point_red()
zx8754
  • 52,746
  • 12
  • 114
  • 209
3

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.

Mikko Marttila
  • 10,972
  • 18
  • 31
2

As my reputation won't allow me to comment:

@PoGibas: An issue about detaching and re-attaching ggplot2 is that if other packages are loaded that require it, e.g. rstan, it won't work and return an error.

andreas
  • 43
  • 5