在单一方向上扩展geom_smooth

Ale*_*ock 4 r ggplot2

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

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

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

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

ags*_*udy 12

在内部运作中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
}
Run Code Online (Sandbox Code Playgroud)

然后,对于每个方法,我们创建一个特化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)
  }
Run Code Online (Sandbox Code Playgroud)

左侧扩展名同样如此:

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)
  }
Run Code Online (Sandbox Code Playgroud)

最后一个使用示例:

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 优秀的代码。此功能应包含在“ggplot2”的下一版本中。 (2认同)