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

8. Axes > Changing the Order of Items on a Categorical Axis

Changing the Order of Items on a Categorical Axis

Problem

You want to change the order of items on a categorical axis.

Solution

For a categorical (or discrete) axis—one with a factor mapped to it—the order of items can be changed by setting limits in scale_x_discrete() or scale_y_discrete().

To manually set the order of items on the axis, specify limits with a vector of the levels in the desired order. You can also omit items with this vector, as shown in Figure 8-8:

p <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()

p + scale_x_discrete(limits=c("trt1","ctrl","trt2"))
Left: with manually specified items on the x-axis; right:
            with only two items
Figure 8-8. Left: box plot with manually specified items on the x-axis; right: with only two items

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free Trial


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