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

## Changing the Order of Items on a Categorical Axis

### Solution

Entire Site

Free Trial

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

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"`

`))`