在ggplot2/geom_smooth中使用跨度 [英] Working with span in ggplot2 / geom_smooth

查看:84
本文介绍了在ggplot2/geom_smooth中使用跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ggplot2 创建具有多个数据集的绘图.对于并非所有数据集都具有相同数量的数据点(或有中断),我想调整范围.

I am using ggplot2 to create a plot with several data sets. For not all datasets have the same amount of datapoints (or have breaks), I would like to adjust the span.

但是我不确定跨度的调整会产生什么影响,它既没有记录在 stat_smooth 中,也没有记录在 geom_smooth 中,无论我在哪里都能找到跨度的任何想法从数据集中获取数据?span如何计算计算平滑器所需的数据点数量?代码如下:

But I am not sure what effects the adjustment of span have, it is neither documented in stat_smooth nor in geom_smooth, any idea where i can find anything how span is taking data from the dataset? How does span calculate the number of datapoints which have to be taken for calculating the smoother? Code looks like this:

t<-ggplot(data=XX1)+
scale_x_date(as.POSIXct(XX1$date1), breaks = "1 month", labels=date_format("%b %Y"))+
geom_vline(xintercept=as.numeric(XX2$Day.of.action, colour="lightgray"))+
geom_point(aes(x=day, y=perc_DP10m, colour=as.factor(station_subunit) ))+
geom_smooth(data=1_F1, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=XX1, aes(x=day, y=perc_DP10m,   
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F3, aes(x=day, y=perc_DP10m,  
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F4, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F5, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F6, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F7, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F8, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F9, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F10, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
geom_smooth(data=1_F11, aes(x=day, y=perc_DP10m, 
 colour=as.factor(station_subunit)),method=loess, span=0.3, se=FALSE, lwd=1)+
theme_bw()
t<-t+labs( list( title = "Detection Positive Ten Minutes / Day \n",
             x = "\n Year (August 2012 - March 2014",
             y = "% DP10M per Day \n"))
t

非常感谢任何提示!

推荐答案

请举一个简短的可复制示例.

Please make a short reproductible example.

此几何的默认统计信息为 stat_smooth

The default stat for this geom is stat_smooth

阅读stat_smooth帮助(?stat_smooth)之后,该函数使用lms中的统计方法,stats基本软件包中的glory loess函数.还有一种关于gam方法的mgcv软件包的参考.因此,stat_smooth的span参数使用这些方法来控制平滑度.

After reading the stat_smooth help (?stat_smooth), the function use statistical methods from lm, glmor loess functions from the stats base package. There is alos a a reference to the mgcv package for the gam method. So, the span argument of stat_smooth use these methods to control the degree of smoothing.

但是验证这一点的简单方法是使用stats软件包的黄土函数,并与stat_smooth获得的结果进行比较.

But the easy way to verify that is to use the loessfunction of the stats package and to compare with your results obtained with stat_smooth.

在此示例中,结果相同:

With this this example the results semm to be the same:

黄土:

period <- 120
x <- 1:120
y <- sin(2*pi*x/period) + runif(length(x),-1,1)
plot(x,y, main="Sine Curve + 'Uniform' Noise")
y.loess <- loess(y ~ x, span=0.75, data.frame(x=x, y=y))
y.predict <- predict(y.loess, data.frame(x=x))
lines(x,y.predict)

geom_smooth:

geom_smooth:

xy <- cbind(x,y)
gp <- ggplot(as.data.frame(xy), aes(x=x,y=y)) + geom_point()
gp + geom_smooth(aes(y=y,x=x), data=as.data.frame(xy), method = "loess", span = 0.75)

这篇关于在ggplot2/geom_smooth中使用跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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