Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning off some legends in a ggplot

Tags:

r

ggplot2

Suppose I have a ggplot with more than one legend.

mov <- subset(movies, length != "") (p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +   geom_point() ) 

I can turn off the display of all the legends like this:

(p1 <- p0 + theme(legend.position = "none")) 

Passing show_guide = FALSE to geom_point (as per this question) turns off the shape legend.

(p2 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +   geom_point(show_guide = FALSE) ) 

But what if I want to turn off the colour legend instead? There doesn't seem to be a way of telling show_guide which legend to apply its behaviour to. And there is no show_guide argument for scales or aesthetics.

(p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +   scale_colour_discrete(show_guide = FALSE) +   geom_point() ) # Error in discrete_scale  (p4 <- ggplot(mov, aes(year, rating, shape = mpaa)) +   aes(colour = length, show_guide = FALSE) +   geom_point() ) #draws both legends 

This question suggests that the modern (since ggplot2 v0.9.2) way of controlling legends is with the guides function.

I want to be able to do something like

p0 + guides(   colour = guide_legend(show = FALSE)  ) 

but guide_legend doesn't have a show argument.

How do I specify which legends get displayed?

like image 862
Richie Cotton Avatar asked Jan 30 '13 12:01

Richie Cotton


People also ask

How do I get rid of legends?

Tip: To quickly remove a legend or a legend entry from a chart, you can select it, and then press DELETE. You can also right-click the legend or a legend entry, and then click Delete.

How do I remove a legend label in R?

To remove legend title, its legend. title attribute is set to element_blank(). Example: Removing legend title with theme().

How do I change the legend value in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do I remove the legend from a bar in R?

We can improve the plot by removing the legend from the plot. One of the ways to remove legend from ggplot2 is to use theme() function as another layer with argument legend. position = “None”.


2 Answers

You can use guide = "none" in scale_..._...() to suppress legend.

For your example you should use scale_colour_continuous() because length is continuous variable (not discrete).

(p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +    scale_colour_continuous(guide = "none") +    geom_point() ) 

Or using function guides() you should set "none" for that element/aesthetic that you don't want to appear as legend, for example, fill, shape, colour.

p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +   geom_point()     p0+guides(colour = "none") 

UPDATE

Both provided solutions work in new ggplot2 version 3.3.5 but movies dataset is no longer present in this library. Instead you have to use new package ggplot2movies to check those solutions.

library(ggplot2movies) data(movies) mov <- subset(movies, length != "") 
like image 63
Didzis Elferts Avatar answered Oct 12 '22 18:10

Didzis Elferts


You can simply add show.legend=FALSE to geom to suppress the corresponding legend

like image 38
fc9.30 Avatar answered Oct 12 '22 17:10

fc9.30