3

I'm trying to create a scatter plot with fitted linear models and log scale axes, but it is causing my model lines to disappear and I can't figure out why.

My data looks like:

  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  

The code I am using is as follows:

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

If I remove the log="xy" then the model lines show up and the plots look perfect. How do I keep the log scale axes and the ablines?

Fbj9506
  • 231
  • 3
  • 11

1 Answers1

1

A linear fit on a log-log graph is not a straight line, thus the abline function will not work with the default option. You will have to use the predict function and manually draw the line on the graph or change the default options in the abline function.

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

Edit: Since the axis is transformed, the abline function does have a untf option, setting this to TRUE will also correctly draw the line.

abline(shade_lm3, untf = TRUE)

enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50