Position-dodge warning with ggplot boxplot?

The number of groups is not the problem; I can see the same thing even when there are only 2 groups. The issue is that ggplot2 draws boxplots vertically (continuous along y, categorical along x) and you are trying to draw them horizontally (continuous along x, categorical along y).

Also, your example has several syntax errors and isn’t reproducible because we don’t have data/d.

Start with some mock data

dat <- data.frame(scores=rnorm(1000,sd=500), 
                  names=sample(LETTERS, 1000, replace=TRUE))

Corrected version of your example code:

ggplot(dat, aes(scores, reorder(names, scores, median))) + geom_boxplot()

This is the horizontal lines you saw.

If you instead put the categorical on the x axis and the continuous on the y you get

ggplot(dat, aes(reorder(names, scores, median), scores)) + geom_boxplot()

Finally, if you want to flip the coordinate axes, you can use coord_flip(). There can be some additional problems with this if you are doing even more sophisticated things, but for basic boxplots it works.

ggplot(dat, aes(reorder(names, scores, median), scores)) + 
  geom_boxplot() + coord_flip()

Leave a Comment