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 > Reversing a Continuous Axis

Reversing a Continuous Axis

Problem

You want to reverse the direction of a continuous axis.

Solution

Use scale_y_reverse or scale_x_reverse (Figure 8-6). The direction of an axis can also be reversed by specifying the limits in reversed order, with the maximum first, then the minimum:

ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() + scale_y_reverse()

# Similar effect by specifying limits in reversed order
ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() + ylim(6.5, 3.5)
Reversed y-axis
Figure 8-6. Box plot with reversed y-axis

Discussion

Like scale_y_continuous(), scale_y_reverse() does not work with ylim. (The same is true for the x-axis properties.) If you want to reverse an axis and set its range, you must do it within the scale_y_reverse() statement, by setting the limits in reversed order (Figure 8-7):


  

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