Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove point transparency in ggplot2 legend [duplicate]

Tags:

r

ggplot2

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:

what we have

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:

what I want

How is this possible?

like image 871
Boris Gorelik Avatar asked Nov 04 '13 09:11

Boris Gorelik


1 Answers

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)))
like image 70
Didzis Elferts Avatar answered Nov 02 '22 11:11

Didzis Elferts