ggplot2中带阴影置信区间的平均线图

时间:2019-01-31 10:33:41

标签: r ggplot2

我正在绘制线图,并希望在其旁边的阴影中以置信区间绘制平均值。我可以像折线图一样绘制数据,但是效果不佳,因为数据分散得太多。

数据如下:

     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

对端口的补贴从0到100%以20的步幅变化,我希望这一因素能够在一个图中显示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命令,但无法弄清楚它是如何工作的。所有帮助表示赞赏!非常感谢。

最大

2 个答案:

答案 0 :(得分: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回归,但您可能想尝试一下并尝试其他方法,例如简单的线性回归。

答案 1 :(得分:1)

时,它类似的东西你想要什么?

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

enter image description here