【问题标题】:mean line for temporal sequence ggplot2时间序列ggplot2的平均线
【发布时间】:2016-04-12 18:18:42
【问题描述】:

我想添加一条线,将一年中每年 3 个值的平均值关联起来。

例如:

Year  Month  Density
2004   06     5
2004   07     6
2004   08     4
2005   06     0.5
2005   07     1.2
2005   08     4.3
2006   06     5.4
...

到目前为止,我有这个代码:

enter code hereData$Dateenter code hereData$Date

ggplot(Data, aes(Date, Densite, group=Month))+ylab("Density (N/ha)")+ xlab("Year") +   geom_point(stat="identity", position = "dodge") + 
  scale_fill_brewer(palette = "Set3") + stat_summary_bin(aes(y = Densite), fun.y = "mean", geom = "bar")

但它不起作用,它为每个组制作一条线...我想要每年一个值以及连接这些每年平均值的线。

希望清楚:)

尼哥

谢谢

【问题讨论】:

  • 我建议您为要绘制的值创建一个数据框,而不是依靠 ggplot 为您进行聚合。也就是说,如果您摆脱group = Month 或将其移至geom_point 层,您可能会走运。您可能需要在 stat_summary 层中使用group = 1...很难判断您使用的是什么数据类型。如果您与dput() 共享您的数据,一切都会变得清晰。
  • 非常好!当我将它移到 geom_point 层时它起作用了: ggplot(Data, aes(Date, Densite))+ylab("Density (N/ha)")+ xlab("Year") + geom_point(aes(group=Month) ,stat="identity", position = "dodge") + scale_fill_brewer(palette = "Set3")+ stat_summary_bin(aes(y = Densite), fun.y = "mean", geom = "line")
  • 很高兴它成功了。下次请使用dput() 分享您的数据。

标签: r ggplot2


【解决方案1】:

这是答案,感谢@Gregor

ggplot(HUM, aes(Date, Densite))+ylab("Density (N/ha)")+ xlab("Year") +   geom_point(aes(group=Month),stat="identity", position = "dodge") + 
  scale_fill_brewer(palette = "Set3")+ scale_fill_discrete(guide=FALSE)+  stat_summary_bin(aes(y = Densite), fun.y = "mean", geom = "line")

【讨论】:

    猜你喜欢
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2016-04-05
    • 1970-01-01
    • 2021-08-28
    • 2011-05-13
    相关资源
    最近更新 更多