tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

Feature request: allow adding aesthetics together #5884

Closed billdenney closed 2 months ago

billdenney commented 2 months ago

In situations when I am programming around ggplot2 to modify figures, I would like to be able to add aesthetics together.

The goal would be to allow modification of existing figures or plotting routines in a simpler way.

library(ggplot2)

aes(x = A) + aes(y = B) # equals aes(x = A, y = B)
aes(x = A, colour = D) + aes(x = C, y = B) # equals aes(x = C, y = B, colour = D)
aes(x = A) + NULL # equals aes(x = A)

I think that the above is the most useful. But, while setting up operations for aesthetics, I think that having a method to subtract could also be useful. I think that the default subtraction method should be that it removes the aesthetic:

library(ggplot2)

aes(x = A) - aes(y = B) # no change, possibly with a warning aes(x = A)
aes(x = A, colour = D) - aes(x = C, y = B) # equals aes(colour = D)
aes(x = A) - NULL # no change aes(x = A)
teunbrand commented 2 months ago

Hi thanks for the suggestion!

This (or something very similar to this) has been discussed in #4789. The outcome of that discussion was that the splicing mechanism (see #2675) is preferred over further overloading the + operator.

billdenney commented 2 months ago

Ah, thanks for pointing out the prior discussion. My search didn't find that. This is resolved, then.