将stat_smooth约束到特定的范围 [英] Constraining stat_smooth to a particular range

查看:135
本文介绍了将stat_smooth约束到特定的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算两条不同的最适合我的情节的两部分。
我可以对数据进行子集化处理,但是想知道是否可以定义一个范围来处理stat_smooth的操作。



例如,我想适合两个

  test< -data.frame(ecdf)这个数据的分隔线,一个用于lat <100和一个用于lat> 100。 = C(0.02040816,0.04081633,0.06122449,0.08163265,0.10204082,0.14285714,0.14285714,0.16326531,0.24489796,0.24489796,0.24489796,0.24489796,0.26530612,0.28571429,0.30612245,0.32653061,0.36734694,0.36734694,0.38775510,0.40816327,0.42857143,0.46938776,0.46938776,0.48979592 ,0.53061224,0.53061224,0.59183673,0.59183673,0.59183673,0.61224490,0.63265306,0.65306122,0.67346939,0.69387755,0.71428571,0.73469388,0.75510204,0.77551020,0.79591837,0.81632653,0.83673469,0.85714286,0.87755102,0.89795918,0.91836735,0.93877551,0.95918367,0.97959184,0.99900000 ),LAT = C(50.7812,66.4062,70.3125,97.6562,101.5620,105.4690,105.4690,109.3750,113.2810,113.2810,113.2810,113.2810,125.0000,136.7190,148.4380,164.0620,167.9690 ,167.9690,171.8750,175.7810,183.5940,187.5000,187.5000,191.4060,195.3120,195.3120,234.3750,234.3750,234.3750,238.2810,261.7190,312.5000,316.4060,324.2190,417.9690,507.8120,511.7190,562.5000,664.0620,683.5940,957.0310,1023.4400 ,1050.7800,1070.3100,1109.3800,1484.3800,1574.2200,1593.7500,1750.0000))

p < - ggplot(test,aes(lat,ecdf))
p + geom_point()+ scale_y_probit( )+ scale_x_log10()+ stat_smooth(method =lm)

解决方案

像这样:

pre $ gent_point()+ scale_y_probit()+ scale_x_log10()+
geom_smooth(data = subset (test,lat> 100),method =lm)+
geom_smooth(data = subset(test,lat <= 100),method =lm)

但是最好先定义一个因子(这里 latcat ),点你想分离平稳。然后,在你的审美中包含这个因素,并且 geom_smooth()将为你完成剩下的工作:

 test $ latcat < -  cut(test $ lat,
breaks = c(-Inf,100,Inf),
labels = c(<= 100)> 100))

p < - ggplot(test,aes(lat,ecdf,color = latcat))
p + geom_point()+ scale_y_probit()+ scale_x_log10 ()+
geom_smooth(method =lm)


I would like to calculate two different lines of best fit for 2 parts of my plot. I could subset the data, but was wondering whether it's possible to define a range over which stat_smooth will operate.

For instance, I would like to fit two separate lines to this data, one for lat<100 and one for lat>100.

test<-data.frame(ecdf=c(0.02040816,0.04081633,0.06122449,0.08163265,0.10204082,0.14285714,0.14285714,0.16326531,0.24489796,0.24489796,0.24489796,0.24489796,0.26530612,0.28571429,0.30612245,0.32653061,0.36734694,0.36734694,0.38775510,0.40816327,0.42857143,0.46938776,0.46938776,0.48979592,0.53061224,0.53061224,0.59183673,0.59183673,0.59183673,0.61224490,0.63265306,0.65306122,0.67346939,0.69387755,0.71428571,0.73469388,0.75510204,0.77551020,0.79591837,0.81632653,0.83673469,0.85714286,0.87755102,0.89795918,0.91836735,0.93877551,0.95918367,0.97959184,0.99900000),lat=c(50.7812,66.4062,70.3125,97.6562,101.5620,105.4690,105.4690,109.3750,113.2810,113.2810,113.2810,113.2810,125.0000,136.7190,148.4380,164.0620,167.9690,167.9690,171.8750,175.7810,183.5940,187.5000,187.5000,191.4060,195.3120,195.3120,234.3750,234.3750,234.3750,238.2810,261.7190,312.5000,316.4060,324.2190,417.9690,507.8120,511.7190,562.5000,664.0620,683.5940,957.0310,1023.4400,1050.7800,1070.3100,1109.3800,1484.3800,1574.2200,1593.7500,1750.0000))

p <- ggplot( test, aes(lat, ecdf) ) 
p+geom_point()+scale_y_probit()+scale_x_log10()+ stat_smooth(method = "lm")

解决方案

You could always do something like this:

p + geom_point() + scale_y_probit() + scale_x_log10() +
geom_smooth(data=subset(test, lat>100), method = "lm") +
geom_smooth(data=subset(test, lat<=100), method = "lm")

But it's probably preferable to first define a factor (here latcat) that marks the two groups of points you want to separately smooth. Then, include the factor in your aesthetic, and geom_smooth() will do the rest of the work for you:

test$latcat <- cut(test$lat, 
                   breaks = c(-Inf, 100, Inf), 
                   labels = c("<=100", ">100"))

p <- ggplot( test, aes(lat, ecdf, colour = latcat))
p + geom_point() + scale_y_probit() + scale_x_log10() +
geom_smooth(method = "lm")

这篇关于将stat_smooth约束到特定的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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