Reputation: 23014
I want to make some points stand out on a ggplot2 chart by giving them less transparency while the rest fade to the background. But no matter what two alpha values I give the sets of points, their relative transparency is the same.
Here's 0.8
vs 0.7
:
x <- mtcars
x$opacity <- ifelse(x$cyl == 6, 0.8, 0.7)
ggplot(x, aes(x = wt, y = mpg, color = cyl, alpha = opacity)) +
geom_point()
And here's 0.8
vs 0.1
-- looks the same:
x$opacity <- ifelse(x$cyl == 6, 0.8, 0.1)
ggplot(x, aes(x = wt, y = mpg, color = cyl, alpha = opacity)) +
geom_point()
How can I fine-tune that relative alpha so that the two sets are closer in transparency? Right now the values of the two numbers don't seem to matter. Specifically, in this case I want the darker points (with the higher alpha) to be more transparent.
Upvotes: 2
Views: 3796
Reputation: 43334
You're mapping the values 0.7 and 0.8 to alpha, not necessarily using them for alpha. A quicker way is to map the condition and then set alpha:
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg, color = cyl, alpha = cyl == 6)) +
geom_point() +
scale_alpha_discrete(range = c(0.2, 0.8))
#> Warning: Using alpha for a discrete variable is not advised.
Upvotes: 1
Reputation: 206207
Since you are trying to pass actual alpha values to the aesthetic mapping, be sure to use
scale_alpha_identity()
Otherwise ggplot will rescale your values just like it created the colors for you automatically.
Upvotes: 4
Reputation: 198
Add scale_alpha_continuous to your plot and define the range. e.g.
scale_alpha_continuous(range = c(0.7, 0.8))
Upvotes: 1