Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

10. Legends > Removing the Legend

Removing the Legend

Problem

You want to remove the legend from a graph.

Solution

Use guides(), and specify the scale that should have its legend removed (Figure 10-1):

# The base plot (with legend)
p <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
p

# Remove the legend for fill
p + guides(fill=FALSE)
Left: default appearance; right: with legend
            removed
Figure 10-1. Left: default appearance; right: with legend removed

Discussion

Another way to remove a legend is to set guide=FALSE in the scale. This will result in the exact same output as the preceding code:

# Remove the legend for fill
p + scale_fill_discrete(guide=FALSE)

Yet another way to remove the legend is to use the theming system. If you have more than one aesthetic mapping with a legend (color and shape, for example), this will remove legends for all of them:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint