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 > Reversing the Order of Items in a Legend

Reversing the Order of Items in a Legend

Problem

You want to reverse the order of items in a legend.

Solution

Add guides(fill=guide_legend(reverse=TRUE)) to reverse the order of the legend, as in Figure 10-7 (for other aesthetics, replace fill with the name of the aesthetic, such as colour or size):

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

# Reverse the legend order
p + guides(fill=guide_legend(reverse=TRUE))
Left: default order for legend. Right: reversed
            order.
Figure 10-7. Left: default order for legend; right: reversed order

Discussion

It is also possible to control the legend when specifying the scale, as in the following:

scale_fill_hue(guide=guide_legend(reverse=TRUE))

  

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