Jim G. Jim G. - 2 months ago 22
R Question

How can I flip and then zoom in on a boxplot?

Consider the following code:

library(ggplot2)
ggplot(diamonds, aes("", price)) + geom_boxplot() + coord_flip()


Diamond Prices

After flipping the box plot, how can I zoom in to
c(0,7000)
on price (which is the new x-axis)?

I feel like it has something to do with
coord_cartesian(ylim=c(0, 7000))
, but this doesn't seem to work in conjunction with
coord_flip()
.

J_F J_F
Answer

Here is my solution:

ggplot(diamonds, aes("", price)) + 
  geom_boxplot() + 
  coord_flip(ylim=c(0, 7000))

Just combine the ylim command as argument in coord_flip().

enter image description here