如何在ggplot线性模型图中将截距拟合为特定值?

时间:2019-09-13 14:17:00

标签: r ggplot2 lm intercept

我想使用R中的ggplot生成分解与时间的关系图,如下所示:

ggplot(data=deco, aes(x = week, y = mass, group=interaction(treatment,habitat),
                      colour=habitat, linetype=treatment)) +   
 geom_point() +  
 theme_classic() + 
 geom_smooth(method='lm',formula=y~x)

因为最初的垃圾重量是5克,所以我希望所有的线在0周内从5克开始。但是,我意识到所有的线都有不同的起点。我试图找到任何解决方案来迅速解决所有问题,但所有方法对我而言都不奏效。如果你们中的任何一个都能解决这个问题,那就太好了。

decomposition

1 个答案:

答案 0 :(得分:2)

正如Gregor所指出的,这并不重要,因为组的大小未知且geom_smooth受到限制。有两个选项,可以在ggplot之外适合您的模型。或抑制截距,移动y,然后重新标记轴。

ggplot(mtcars, aes(wt, drat, col = factor(am))) + 
  geom_point() + 
  geom_smooth(method = 'lm') +
  xlim(0, 6)


Intercept <- 5
ggplot(mtcars, aes(wt, drat - Intercept, col = factor(am))) + 
  geom_point() + 
  geom_smooth(method = 'lm', formula = y ~ x + 0) +
  scale_y_continuous(labels = function(x) x + Intercept) +
  xlim(0, 6)

enter image description here