Remove extra legends in ggplot2 Remove extra legends in ggplot2 r r

Remove extra legends in ggplot2


Aesthetics can be set or mapped within a ggplot call.

  • An aesthetic defined within aes(...) is mapped from the data, and a legend created.
  • An aesthetic may also be set to a single value, by defining it outside aes().

In this case, it appears you wish to set alpha = 0.8 and map colour = group.

To do this,

Place the alpha = 0.8 outside the aes() definition.

g <- ggplot(df, aes(x = x, y = y, group = group))g <- g + geom_line(aes(colour = group))g <- g + geom_point(aes(colour = group), alpha = 0.8)g

enter image description here

For any mapped variable you can supress the appearance of a legend by using guide = 'none' in the appropriate scale_... call. eg.

g2 <- ggplot(df, aes(x = x, y = y, group = group)) +         geom_line(aes(colour = group)) +        geom_point(aes(colour = group, alpha = 0.8))g2 + scale_alpha(guide = 'none')

Which will return an identical plot

EDIT @Joran's comment is spot-on, I've made my answer more comprehensive


Just add the show.legend = F code after the part where you don't want it.

g <- ggplot(df, aes(x=x, y=y, group=group))g <- g + geom_line(aes(colour=group))g <- g + geom_point(aes(colour=group, alpha = .8), show.legend = F)


For old versions of ggplot2 (versions before 0.9.2, released in late 2012), this answer should work:

I tried this with a colour_scale and it did not work. It appears that the colour_scale_hue item works like a function with a default parameter TRUE. I added scale_colour_hue(legend=FALSE) and it worked.

I am not sure if this is the case for all color scale items in ggplot