简体   繁体   中英

Split facet_wrap panel when x is a factor in ggplot2

I want to add a vertical line to split facet_wrap panel in ggplot2.

ggplot(mpg, aes(drv, hwy))+
  geom_boxplot(aes(colour = drv))+
  facet_wrap(~year)+
  theme_bw()+
  theme(panel.grid=element_blank())

my question is how could we add vertical lines when x is a factor ?

在此处输入图片说明

From ?scale_x_discrete :

You can use continuous positions even with a discrete position scale - this allows you (eg) to place labels between bars in a bar chart.

You can add vertical lines with geom_vline() & set the position at 1.5, 2.5, etc:

ggplot(mpg, aes(drv, hwy)) +
  geom_boxplot(aes(colour = drv))+
  geom_vline(xintercept = c(1.5, 2.5), linetype = "dashed") +
  facet_wrap(~ year) +
  theme_bw() +
  theme(panel.grid=element_blank())

情节

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM