简体   繁体   中英

Add points to grouped bar plot (ggplot2)

I have a dataset containing measurements at two timepoints. I have made a barplot of the data of the first timepoint, and would like to add points for the second timepoint (this is just meant as a reference). As you can see below the points are there, but they are not in the correct x-axis position (ie they are all on the same x-value, unlike the bars).

How to solve this?

library(ggplot2)

MyData = data.frame(
  method=rep(c("A","B","C","D","E"),times=3),
  time1=rnorm(30,10,3),
  time2=rnorm(30,8,2),
  lab=rep(rep(c(1,2,3),each=5),times=2),
  cat=rep(c(1,2),each=15)
)

p <- ggplot(data = MyData,
            aes(x=lab)) +
  geom_bar(aes(y=time1,fill=method),
           stat="identity",
           position="dodge",
           alpha=.7
  ) +
  geom_point(aes(y=time2,group=method),
           stat="identity",
           position="dodge",
           alpha=.8,
           size=3) +
  scale_fill_brewer(palette=3) +
  facet_grid(. ~ cat)
p

在此处输入图片说明

geom_point使用position = position_dodge(width = .9)

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