I've found many examples describing the assignment of alpha when in a ggplot2 line like so:
scale_alpha( variable, trans = reverse)
ref
However, is there a method to simply invert the scale in aes()
inside the geom_*()
?
Something like:
geom_point(aes(colour=variableA, alpha=REVERSE(variableB))
(This is a very old question, but I had the same issue and couldn't find an answer. The previous solution by hugh-allan is, as indicated in the Edit note, producing an incorrect legend.)
The settings of the scale should really be in the scale_alpha*
parameter. That's where you manage this. The geoms
are used for adding the data or setting a style for all points, not tuning a specific scale (otherwise, it would need to be inside the aes()
mapping).
To be clear, there are two options in current versions of ggplot2 (using version 3.3.5):
tibble(x = 1:10, y = 1) %>%
ggplot(aes(x, y, alpha = x) +
geom_point(size = 5) +
scale_alpha(trans = reverse_trans())
or, probably more in line with current ggplot documentation:
scale_alpha(range = c(1, 0.1))
i.e., reversing the range of the alpha scale (the default is range = c(1, 0.1)
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With