In ggplot2, transparency that is defined in geom_XXX is reflected in the legend. For example:
df <- data.frame(x=runif(10000), z=ifelse(runif(10000) > 0.5, 'a', 'b')); df$y <- runif(10000); df$y[df$z == 'b'] <- cos(df$x[df$z == 'b']*10)
ggplot(df) + geom_point(aes(x, y, color=z), alpha=0.1)
Gives the following result:
Since the points are very transparent, they are hardly seen on the legend. I would like to remove point transparency from the legend, so that the graph looks like this:
How is this possible?
You can use function guides()
and override.aes=
to set alpha value just for legend entries.
ggplot(df) + geom_point(aes(x, y, color=z), alpha=0.1)+
guides(colour = guide_legend(override.aes = list(alpha=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