gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:41:21 25 4
gpt4 key购买 nike

使用 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)

enter image description here

但是,由于在比例上设置限制会删除位于这些限制之外的数据,因此灰色误差带不会延伸到线的末端。这可以通过将比例尺的限制设置得稍微宽一些然后使用 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)

enter image description here

关于r - fullrange 没有将 geom_smooth(method ="lm") 线扩展到数据之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55307921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com