294

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?

Community
  • 1
  • 1
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • 8
    After a recent ``ggplot2`` update, ``show_guide`` has been deprecated and replaced by ``show.legend``. – PatrickT Dec 03 '16 at 18:12

2 Answers2

385

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 != "")
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • 9
    The scale_colour_continuous(guide = FALSE) does not work in ggplot2 0.9.3 but guides(colour=FALSE) solves the issue. Thanks a lot Didzis! – Nikolay Nenov May 29 '13 at 14:11
  • 1
    @NikolayNenov Strange, because both solutions works for me with ggplot2 0.9.3.1 – Didzis Elferts May 29 '13 at 14:16
  • 13
    I want to remove **legend of alpha**. `guides(alpha=FALSE)` do the trick. Thank you, Didzis. – l0o0 Apr 28 '16 at 07:25
  • 3
    Note to self: if you have a ``geom_linerange()`` and the legend is showing a cross instead of a line, insert ``show.legend=FALSE`` inside the ``geom_linerange()``. – PatrickT Sep 30 '17 at 08:51
  • 4
    Suggested update: `guide = FALSE` is deprecated and was replaced by `guide = "none"`. – Chr Jul 14 '21 at 08:18
74

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

fc9.30
  • 2,293
  • 20
  • 19