完整范围不会将geom_smooth(method ="lm")行扩展到数据之外 [英] fullrange isn't extending geom_smooth(method="lm") line beyond the data

查看:63
本文介绍了完整范围不会将geom_smooth(method ="lm")行扩展到数据之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用geom_smooth进行绘制时,我试图将lm线扩展到数据范围之外.但是,将fullrange = TRUE设置似乎无法解决问题.

I am trying to extend the lm line beyond the range of the data when plotting using geom_smooth. However, setting fullrange=TRUE doesn't seem to do the trick.

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

I have set the xlim beyond the data range using coord_cartesian and scale_x_log10 as shown in the code.

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行在数据结尾处停止:

The lm line stops at the end of the data:

推荐答案

如果限制是在 scale _ * _ log10()内部而不是在 coord_cartesian()中设置的,线性模型显示在整个范围内:

If the limits are set inside scale_*_log10() instead of in coord_cartesian(), the linear model is shown over the entire range:

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():

However, because setting limits on the scales remove data that lies outside of these limits, the grey error bands do not extend to the end of the lines. This can be amended by setting the limits on the scales somewhat wider and then using coord_cartesian()again:

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)

这篇关于完整范围不会将geom_smooth(method ="lm")行扩展到数据之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆