2017-02-09 58 views
2

我的问题: 我想显示两个不同组/组合物的回归线。 我刚刚开始使用ggplot,因此对ggplot中的回归行知之甚少。ggplot:添加不同子集的回归线

mel: 
composition diametre volume 
mixed 0.261 71.3645893 
mixed 0.233 392.9487358 
mixed 0.319 284.8683927 
pure 0.248 120.7654642 
pure 0.274 142.1273373 
pure 0.308 215.9924244 
pure 0.188 26.11804847 
pure 0.124 5.795590982 
pure 0.307 136.7732086 
pure 0.283 138.0600194 
pure 0.175 32.43129359 
pure 0.205 32.58726466 
pure 0.159 12.27308951 

下面的代码显示散点图非常清楚,但产生增加geom_smooth(object 'volume' not found)时的误差;

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) + 
    geom_point()+ 
    geom_smooth (data = subset(mel, composition=="mixed"), method='lm', 
       formula= log(volume)~I(diametre^2), 
       se=FALSE, size=2) + 
    geom_smooth (data = subset(mel, composition=="pure"), method = "lm", 
       formula = (volume)~I(diametre^2), se = FALSE, size = 2) 

我以为也许是错误发生,因为我subting的构图?! 所以我试图用一个表来工作的每个组成,因此代码看起来像这样

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) + 
    geom_point()+ 
    geom_smooth (data = mel_mix, method='lm', 
       formula= log(volume)~I(diametre^2), 
       se=FALSE, size=2) + 
    geom_smooth (data = mel_pure, method = "lm", 
       formula = (volume)~I(diametre^2), se = FALSE, size = 2) 

导致:invalid argument to unary operator

回答

1

只要这应该工作(你需要有x,ygeom_smooth):

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) + 
    geom_point() + 
    geom_smooth(method='lm', 
       formula= log(y)~I(x^2), 
       se=FALSE, size=2) 

或者如果你想单独适合子集数据:

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) + 
    geom_point()+ 
    geom_smooth (data = subset(mel, composition=="mixed"), method='lm', 
       formula= log(y)~I(x^2), 
       se=FALSE, size=2) + 
    geom_smooth (data = subset(mel, composition=="pure"), method = "lm", 
      formula = log(y)~I(x^2), se = FALSE, size = 2) 

volume~I(diametre^2)是为您的数据更适合我认为:

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) + 
    geom_point() + 
    geom_smooth(method='lm', 
       formula= y~I(x^2), 
       se=FALSE, size=2) 

enter image description here

+1

大,非常感谢。 – chajamalka