用ggplot填充R中两条黄土平滑线之间的区域 [英] Fill region between two loess-smoothed lines in R with ggplot

查看:504
本文介绍了用ggplot填充R中两条黄土平滑线之间的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下数据框用于图片:

p>

  xy ymin ymax grp ydiff 
1 1 3.285614 3.285614 10.14177 min 6.8561586
2 1 10.141773 3.285614 10.14177 max 6.8561586
3 2 5.061879 5.061879 11.24462分钟6.1827368
4 2 11.244615 5.061879 11.24462最大6.1827368
5 3 8.614408 8.614408 13.45030分钟4.8358931
6 3 13.450301 8.614408 13.45030最大4.8358931
7 4 6.838143 6.838143 12.34746分钟5.5093150
8 4 12.347458 6.838143 12.34746最大5.5093150
9 5 10.390673 10.390673 14.55314分钟4.1624713
10 5 14.553144 10.390673 14.55314最大4.1624713
11 6 12.166937 12.166937 15.65599分钟3.4890495
12 6 15.655987 12.166937 15.65599最大3.4890495
13 7 13.943202 13.943202 16.75883分钟2.8156277
14 7 16.758830 13.943202 16.75883最大2.8156277
15 8 5 .950011 5.950011 11.79604分钟5.8460259
16 8 11.796037 5.950011 11.79604最大5.8460259
17 9 17.495731 17.495731 18.96452分钟1.4687841
18 9 18.964515 17.495731 18.96452最大1.4687841
19 10 15.719466 15.719466 17.86167分钟2.1422059
20 10 17.861672 15.719466 17.86167最大2.1422059
21 11 19.271996 19.271996 20.06736分钟0.7953623
22 11 20.067358 19.271996 20.06736最大0.7953623

以下源代码会生成一个具有普通线条(未平滑)的图形:

  ggplot (intdf)+ 
geom_point(aes(x = x,y = y,color = grp))+
geom_ribbon(aes(x = x,ymin = ymin,ymax = ymax),fill =灰色,alpha = .4)+
geom_line(aes(x = x,y = y,color = grp))

其中x和y是连续的数值。 ymin和ymax每个都包含位置x处绿色和红色线的y值。



现在我想使线条平滑。我只需用下面的代码来做到这一点:

  ggplot(intdf)+ 
stat_smooth(aes(x = x,y = ymin,color =min),method =loess,se = FALSE)+
stat_smooth(aes(x = x,y = ymax,color =max),method =黄土 se = FALSE)

给出如下图:



但我没有设法填补这两条线之间的区域。我试图拟合一个黄土模型并使用预测值,但我想我完全使用了错误的预测因子。



谁能告诉我如何填充平滑线?



在此先感谢
Daniel

解决方案

一个可能的解决方案,即从plot对象中抓取黄土平滑数据并用于 geom_ribbon

<$ p $使用黄色回归线创建阴谋对象
g1 < - ggplot(df)+
stat_smooth(aes(x = x,y = ymin,color =min ),method =loess,se = FALSE)+
stat_smooth(aes(x = x,y = ymax,color =max),method =loess,se = FALSE)
g1

#为渲染建立阴谋对象
gg1 < - ggplot_build(g1)

#从'数据'槽中提取黄土线的数据
df2 < - data.frame(x = gg1 $ data [[1]] $ x,
ymin = gg1 $ data [[1]] $ y,
ymax = gg1 $ data [ [2]] $ y)的

#使用黄土数据添加'ribbon'来绘制
g1 +
geom_ribbon(data = df2,aes(x = x,ymin = ymin,ymax = ymax ),
fill =gray,alpha = 0.4)


I'd like to know how to fill the area between to loess-smoothed lines in ggplot.

The following data frame is used for the pictures:

    x         y      ymin     ymax grp     ydiff
1   1  3.285614  3.285614 10.14177 min 6.8561586
2   1 10.141773  3.285614 10.14177 max 6.8561586
3   2  5.061879  5.061879 11.24462 min 6.1827368
4   2 11.244615  5.061879 11.24462 max 6.1827368
5   3  8.614408  8.614408 13.45030 min 4.8358931
6   3 13.450301  8.614408 13.45030 max 4.8358931
7   4  6.838143  6.838143 12.34746 min 5.5093150
8   4 12.347458  6.838143 12.34746 max 5.5093150
9   5 10.390673 10.390673 14.55314 min 4.1624713
10  5 14.553144 10.390673 14.55314 max 4.1624713
11  6 12.166937 12.166937 15.65599 min 3.4890495
12  6 15.655987 12.166937 15.65599 max 3.4890495
13  7 13.943202 13.943202 16.75883 min 2.8156277
14  7 16.758830 13.943202 16.75883 max 2.8156277
15  8  5.950011  5.950011 11.79604 min 5.8460259
16  8 11.796037  5.950011 11.79604 max 5.8460259
17  9 17.495731 17.495731 18.96452 min 1.4687841
18  9 18.964515 17.495731 18.96452 max 1.4687841
19 10 15.719466 15.719466 17.86167 min 2.1422059
20 10 17.861672 15.719466 17.86167 max 2.1422059
21 11 19.271996 19.271996 20.06736 min 0.7953623
22 11 20.067358 19.271996 20.06736 max 0.7953623

Following source produces a figure with normal lines (not smoothed):

ggplot(intdf) + 
    geom_point(aes(x=x, y=y, colour=grp)) +
    geom_ribbon(aes(x=x, ymin=ymin, ymax=ymax), fill="grey", alpha=.4) +
    geom_line(aes(x=x, y=y, colour=grp))

where x and y are continuous numeric values. ymin and ymax each contain the y-values from the green and red line at position x.

Now I'd like to smoothen the lines. I simply do this with following code:

ggplot(intdf) + 
    stat_smooth(aes(x=x, y=ymin, colour="min"), method="loess", se=FALSE) +
    stat_smooth(aes(x=x, y=ymax, colour="max"), method="loess", se=FALSE)

which gives following plot:

But I did not manage to fill the area between these two lines. I tried to fit a loess-model and use the predicted values, but I guess I completely used the wrong predictors.

Who could tell me how to fill the region between the smoothed lines?

Thanks in advance Daniel

解决方案

A possible solution where the loess smoothed data is grabbed from the plot object and used for the geom_ribbon:

# create plot object with loess regression lines
g1 <- ggplot(df) + 
  stat_smooth(aes(x = x, y = ymin, colour = "min"), method = "loess", se = FALSE) +
  stat_smooth(aes(x = x, y = ymax, colour = "max"), method = "loess", se = FALSE)
g1

# build plot object for rendering 
gg1 <- ggplot_build(g1)

# extract data for the loess lines from the 'data' slot
df2 <- data.frame(x = gg1$data[[1]]$x,
                  ymin = gg1$data[[1]]$y,
                  ymax = gg1$data[[2]]$y) 

# use the loess data to add the 'ribbon' to plot 
g1 +
  geom_ribbon(data = df2, aes(x = x, ymin = ymin, ymax = ymax),
              fill = "grey", alpha = 0.4)

这篇关于用ggplot填充R中两条黄土平滑线之间的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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