ggplot2中带阴影置信区间的平均线图 [英] average line plot with shaded confidence interval in ggplot2

查看:263
本文介绍了ggplot2中带阴影置信区间的平均线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a line plot and would like to plot the average with confidence interval in shades next to it. I can plot the data just as line plot, but that doesn't work quite well because the data is scattered way to much.

数据如下:

     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逐渐上升.我在正常情节中尝试过此方法:

The subsidy to port goes from 0 to 100 % in steps of 20, I want that factored to show 5 different graphs in one figure. The ticks go from 0 to 32, and the industry_cost_to_store gradually goes up. I tried this for the normal plot:

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命令,但无法弄清楚它是如何工作的.所有帮助表示赞赏!非常感谢.

Choosing points gives a lot of scattered points, geom_line just does not work for this data. As you can see, the data set is called i3. I looked at the geom_ribbon command but cannot figure out how this works. All help appreciated! Thanks a lot.

最大

推荐答案

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

From your description, the following should be what you want:

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回归,但您可能想尝试一下并尝试其他方法,例如简单的线性回归.

Since you mentioned "average line" I’ve used LOESS regression but you might want to play around with this and try others, e.g. simple linear regression.

这篇关于ggplot2中带阴影置信区间的平均线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆