简体   繁体   中英

Add space between bars of different groups in errorbarh of ggplot

I have a plot in which for different groups I have a horizontal bar with different color using errorbarh of ggplot. However, they are all layed on top of each other without any space. I would like to put a little space between bars so I can compare and contrast. A piece of the plot is depicted below: 错误栏组

my code is as below as well:

ggplot(allData, aes(x=score, y=path, xmin = min, xmax = max, group=group, col=group, fill=group)) +
  geom_point() +
  geom_vline(xintercept = c(-1,1), colour="#FA8072", linetype = "longdash") +
  geom_vline(xintercept = c(0), colour="grey", linetype = "solid") +
  geom_errorbarh(height=.1) + theme_bw() +
  xlim(-2.5, 2.5)

I found how I can do this. Using position the spaces between bars can be changed. So the modified code is as follows:

ggplot(allData, aes(x=score, y=path, group=group, col=group, fill=group)) +
  geom_point(position = position_dodge(0.7)) +
  geom_vline(xintercept = c(-1,1), colour="#FA8072", linetype = "longdash") +
  geom_vline(xintercept = c(0), colour="grey", linetype = "solid") +
  geom_errorbarh(height=.1, aes(xmin = min, xmax = max), position = position_dodge(0.7)) + theme_bw() +
  xlim(-2.5, 2.5) 

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