简体   繁体   English

将点添加到分组的条形图(ggplot2)

[英]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). 如您所见,这些点在下面,但是它们不在正确的x轴位置(即,与条形图不同,它们都在相同的x值上)。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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