【问题标题】:Add points to grouped bar plot (ggplot2)将点添加到分组条形图 (ggplot2)
【发布时间】:2015-07-09 10:43:36
【问题描述】:

我有一个数据集,其中包含两个时间点的测量值。我已经制作了第一个时间点的数据的条形图,并想为第二个时间点添加点(这只是作为参考)。正如您在下面看到的那样,点在那里,但它们不在正确的 x 轴位置(即它们都在相同的 x 值上,与条形不同)。

如何解决?

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

【问题讨论】:

    标签: r ggplot2 geom-bar


    【解决方案1】:

    在geom_point 中使用position = position_dodge(width = .9)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2018-11-25
      • 2021-07-17
      • 2018-06-04
      • 1970-01-01
      • 2021-07-15
      相关资源
      最近更新 更多