记录刻度轴,导致掉头丢失

时间:2018-12-06 09:07:10

标签: r scatter-plot

我正在尝试使用拟合的线性模型和对数刻度轴创建散点图,但是这导致我的模型线消失了,我不知道为什么。

我的数据如下:

  Environment Min_light_reqs  Leaf_lifespan
1 Shade           1.4          3.5  
2 Shade           3.48         0.7  
3 Shade           0.71         3.8 
4 Shade           3.18         1.4  
5 Shade           2.50         2.2  
6 Shade           1.46         2.5  

我正在使用的代码如下:

shade_lm3 <- lm(Leaf_lifespan ~ Min_light_reqs, data = shade)
gap_lm3 <- lm(Leaf_lifespan ~ Min_light_reqs, data = gap)
plot(d1$Leaf_lifespan ~ d1$Min_light_reqs, log="xy", pch=21, col="black", bg="white",
 xlim=c(0.5, 5), ylim = c(0.5, 10), cex=2, xaxt="n", yaxt="n", cex.lab=1.7)
points(shade$Leaf_lifespan ~ shade$Min_light_reqs, pch=21, col="black",
 bg="black", cex=2)
abline(shade_lm3)
abline(gap_lm3)
axis(1, tick=TRUE, line=0, at=c(0.5, 1, 2, 5), labels=c(0.5, 1, 2, 5))
axis(2, tick=TRUE, line=0, at=c(0.5, 1, 2, 5, 10), labels=c(0.5, 1, 2, 5, 10))

Resulting plot

如果我删除log =“ xy”,则显示模型线,并且图看起来很完美。如何保持对数刻度轴和下斜度?

1 个答案:

答案 0 :(得分:1)

对数-对数图上的线性拟合不是一条直线,因此abline函数不适用于默认选项。您将必须使用预测功能并在图形上手动绘制线条或更改绝对功能中的默认选项。

d1<-read.table(header=TRUE, text="Environment Min_light_reqs  Leaf_lifespan
Shade           1.4          3.5  
Shade           3.48         0.7  
Shade           0.71         3.8 
Shade           3.18         1.4  
Shade           2.50         2.2  
Shade           1.46         2.5  ")

shade_lm3 <- lm(Leaf_lifespan ~ Min_light_reqs, data = d1)

#create the prediction dataframe
p<-data.frame(Min_light_reqs=seq(0.1, 5, 0.2))
p$out<-predict(shade_lm3, p )

plot(d1$Leaf_lifespan ~ d1$Min_light_reqs,  log="xy", pch=21, col="black", bg="white",
     xlim=c(0.5, 5), ylim = c(0.5, 10), cex=2, xaxt="n", yaxt="n", cex.lab=1.7)
points(d1$Leaf_lifespan ~ d1$Min_light_reqs, pch=21, col="black",
       bg="black", cex=2)

#abline(shade_lm3) #does not work with log axis
#plot the prediction
lines(x=p$Min_light_reqs, y=p$out)


axis(1, tick=TRUE, line=0, at=c(0.5, 1, 2, 5), labels=c(0.5, 1, 2, 5))
axis(2, tick=TRUE, line=0, at=c(0.5, 1, 2, 5, 10), labels=c(0.5, 1, 2, 5, 10))

编辑: 由于轴已转换,因此abline函数确实具有untf选项,将其设置为TRUE也会正确绘制线。

abline(shade_lm3, untf = TRUE)

enter image description here