在单一方向上扩展geom_smooth

时间:2014-11-02 23:10:42

标签: r ggplot2

您可以非常轻松地在ggplot2中扩展回归线:

c <- ggplot(mtcars, aes(y=wt, x=mpg)) + xlim(0,50)
c + stat_smooth(method=lm, fullrange=TRUE) + geom_point()

我的问题是,是否可以只向一个方向延伸?

在你提出要求之前,我有充分的理由这样做,我保证!

1 个答案:

答案 0 :(得分:10)

stat_smooth的内部工作中,调用predictdf来创建平滑线。这里的困难是:这是一个未导出的S3方法。它也没有采取...参数,因此很难扩展它。

这里的想法是创建一个新的虚拟类lm_rightlm_left,我们称之为默认的lm方法。

## decorate lm object with a new class lm_right
lm_right <- function(formula,data,...){
  mod <- lm(formula,data)
  class(mod) <- c('lm_right',class(mod))
  mod
}

## decorate lm object with a new class lm_left
lm_left <- function(formula,data,...){
  mod <- lm(formula,data)
  class(mod) <- c('lm_left',class(mod))
  mod
}

然后,对于每个方法,我们创建一个predict_df特化,我们在另一侧截断x值。

predictdf.lm_right <- 
  function(model, xseq, se, level){
    ## here the main code: truncate to x values at the right
    init_range = range(model$model$x)
    xseq <- xseq[xseq >=init_range[1]]
    ggplot2:::predictdf.default(model, xseq[-length(xseq)], se, level)
  }

左侧扩展名同样如此:

predictdf.lm_left <- 
  function(model, xseq, se, level){
    init_range = range(model$model$x)
    ## here the main code: truncate to x values at the left
    xseq <- xseq[xseq <=init_range[2]]
    ggplot2:::predictdf.default(model, xseq[-length(xseq)], se, level)
  }

最后一个使用示例:

library(ggplot2)
library(gridExtra)
## you should set the fullrange option to a true 
p1 <- ggplot(mtcars, aes(y=wt, x=mpg)) + xlim(0,50) +  geom_point() +
     stat_smooth(method="lm_left", fullrange=TRUE,col='green') 
p2 <- ggplot(mtcars, aes(y=wt, x=mpg)) + xlim(0,50) +  geom_point() +
  stat_smooth(method="lm_right", fullrange=TRUE,col='red') 

grid.arrange(p1,p2)

enter image description here

相关问题