【问题标题】:average line plot with shaded confidence interval in ggplot2ggplot2中带有阴影置信区间的平均线图
【发布时间】:2019-06-24 19:05:00
【问题描述】:

我正在制作一个线图,并希望在其旁边绘制带有置信区间的平均值。我可以将数据绘制成线图,但效果不太好,因为数据非常分散。

数据如下所示:

     subsidy_to_port tick industry_cost_to_store
1                20    7                1900000
2                20    8                2800000
3                20    9                3700000
4                20   10                4600000
5                20   11                5500000
6                20   12                6400000
7                20   13                7300000
8                20   14               10300000
9                20   15               13300000
10               20   16               16300000

对端口的补贴以 20 为步长从 0 到 100%,我希望在一个图中显示 5 个不同的图表。刻度从 0 到 32,industry_cost_to_store 逐渐上升。我为正常情节尝试了这个:

StoredCO215mln <- ggplot(i3, aes(x = tick, y =           
total_co2_emissions_captured)) + geom_point(aes(color =    
factor(subsidy_to_port))) + ylab("Amount CO2 captured") + labs(title   
= "Total CO2 captured in 32 years (subsidy = 15mln)") + 
scale_color_discrete(name="Subsidy to port (%)")

选择点会产生很多分散的点,geom_line 只是不适用于这些数据。如您所见,数据集称为 i3。我查看了 geom_ribbon 命令,但无法弄清楚它是如何工作的。所有帮助表示赞赏!非常感谢。

最大

【问题讨论】:

  • geom_smooth 是你想要的吗?听起来是这样。

标签: r ggplot2


【解决方案1】:

根据您的描述,以下应该是您想要的:

StoredCO215mln <- ggplot(i3) +
    aes(x = tick, y = total_co2_emissions_captured, color = factor(subsidy_to_port)) +
    geom_smooth(method = loess) +
    labs(title = "Total CO2 captured in 32 years (subsidy = 15mln)", y = "Amount CO2 captured") + 
    scale_color_discrete(name = "Subsidy to port (%)")

由于您提到“平均线”,我使用了 LOESS 回归,但您可能想尝试一下并尝试其他方法,例如简单的线性回归。

【讨论】:

  • 你好康拉德!非常感谢您的回答,现在这条线看起来还不错。但是,是否可以在其旁边添加标准差或置信区间作为阴影?
  • @MaxGoessens 我的代码已经包含阴影 95% 置信区间,它们是 geom_smooth 的默认值。
  • 谢谢!这行得通,有没有办法使用与填充线相同的颜色?
  • @MaxGoessens 是的,字面意思是使用aes(fill = factor(subsidy_to_port))。这将使它们变得透明。您可以单独控制alpha 属性,请参阅文档。
  • 谢谢!!像魅力一样工作:)
【解决方案2】:

这是你想要的吗?

ggplot(mpg, aes(displ, hwy)) + geom_point() +
  geom_smooth(method = "lm", formula = y ~ 1)

【讨论】:

  • 感谢 Stéphane 的输入,Konrad 的方法成功了!:)
猜你喜欢
  • 1970-01-01
  • 2017-06-17
  • 2021-06-23
  • 2021-12-22
  • 1970-01-01
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
相关资源
最近更新 更多