fullrange 没有将 geom_smooth(method="lm") 行扩展到数据之外

fullrange isn't extending geom_smooth(method="lm") line beyond the data

我试图在使用 geom_smooth 绘图时将 lm 线延伸到数据范围之外。但是,设置 fullrange=TRUE 似乎并不能解决问题。

我已使用 coord_cartesian 和 scale_x_log10 将 xlim 设置为超出数据范围,如代码中所示。

library(ggplot2)

    df<-data.frame(Time=c(1:9 %o% 10^(2:3),10^4),Shift=seq(0.01,0.03,length.out=19),Treatment=2.2)
    df<-rbind(df,data.frame(Time=c(1:9 %o% 10^(2:3),10^4),Shift=seq(0.02,0.06,length.out=19),Treatment=2.4))
    df<-rbind(df,data.frame(Time=c(1:9 %o% 10^(2:3),10^4),Shift=seq(0.06,0.14,length.out=19),Treatment=2.6))
    df<-rbind(df,data.frame(Time=c(1:9 %o% 10^(2:3),10^4),Shift=seq(0.09,0.22,length.out=19),Treatment=2.8))

    myPlot <- ggplot(df,aes(x=Time,y=Shift,col=as.factor(Treatment))) +
      scale_y_log10(breaks = c(1 %o% 10^(-2:1)), minor_breaks = c(1:9 %o% 10^(-1:2)),labels=paste0(c(1 %o% 10^(0:3)),"%")) +
      scale_x_log10(breaks = c(1 %o% 10^(2:9)), minor_breaks = c(1:9 %o% 10^(0:10))) +
      labs(color="Treatment") +
      coord_cartesian(xlim=c(100,1E9),ylim=c(0.001,1))+
      theme_bw() +
      annotation_logticks() +
      geom_point()+
      geom_smooth(method="lm",fullrange=TRUE)
    plot(myPlot)

lm 行在数据末尾停止:

如果限制设置在 scale_*_log10() 内而不是 coord_cartesian() 内,则在整个范围内显示线性模型:

myPlot <- ggplot(df,aes(x=Time,y=Shift,col=as.factor(Treatment))) +
  scale_y_log10(limits = c(0.001, 1), breaks = c(1 %o% 10^(-2:1)), minor_breaks = c(1:9 %o% 10^(-1:2)),labels=paste0(c(1 %o% 10^(0:3)),"%")) +
  scale_x_log10(limits = c(100, 1e9), breaks = c(1 %o% 10^(2:9)), minor_breaks = c(1:9 %o% 10^(0:10))) +
  labs(color="Treatment") +
  theme_bw() +
  annotation_logticks() +
  geom_point()+
  geom_smooth(method="lm",fullrange=TRUE)
plot(myPlot)

但是,由于对比例设置限制会删除位于这些限制之外的数据,因此灰色误差带不会延伸到线条的末尾。这可以通过将比例限制设置得更宽然后再次使用 coord_cartesian() 来修改:

myPlot <- ggplot(df,aes(x=Time,y=Shift,col=as.factor(Treatment))) +
  scale_y_log10(limits = c(0.001, 3), breaks = c(1 %o% 10^(-2:1)), minor_breaks = c(1:9 %o% 10^(-1:2)),labels=paste0(c(1 %o% 10^(0:3)),"%")) +
  scale_x_log10(limits = c(100, 1e9), breaks = c(1 %o% 10^(2:9)), minor_breaks = c(1:9 %o% 10^(0:10))) +
  labs(color="Treatment") +
  coord_cartesian(xlim=c(100,1E9),ylim=c(0.001,1)) +
  theme_bw() +
  annotation_logticks() +
  geom_point()+
  geom_smooth(method="lm",fullrange=TRUE)
plot(myPlot)