Table of Contents#### Download Safari Books Online apps: Apple iOS | Android | BlackBerry

## Swapping X- and Y-Axes

### Solution

### Discussion

Entire Site

Free Trial

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

Use `coord_flip()`

to flip
the axes (Figure 8-1):

ggplot`(`

PlantGrowth`,`

aes`(`

x`=`

group`,`

y`=`

weight`))`

`+`

geom_boxplot`()`

ggplot`(`

PlantGrowth`,`

aes`(`

x`=`

group`,`

y`=`

weight`))`

`+`

geom_boxplot`()`

`+`

coord_flip`()`

For a scatter plot, it is trivial to change what goes on the
vertical axis and what goes on the horizontal axis: just exchange the
variables mapped to `x`

and `y`

. But not all the geoms in ggplot2 treat the
x- and y-axes equally. For example, box plots summarize the data along
the y-axis, the lines in line graphs move in only one direction along
the x-axis, error bars have a single *x* value and a
range of *y* values, and so on. If you’re using these
geoms and want them to behave as though the axes are swapped, `coord_flip()`

is what you need.