Fit line to data on a log scale in R

Jessica B picture Jessica B · Apr 3, 2013 · Viewed 10.2k times · Source

Basically, I don't know how to plot a line of best fit on my data once it's a logarithmic scale. I put a linear regression trend line on my plot using lm() and abline(), but now that log="xy" has been added this just produces a horizontal line.

enter image description here

Here's a very simplified example of what I'm trying to do (the line is completely missing here, however):

lengths = c(10000,3000,3005,3005,3010,20000)
counts = c(3,1,1,2,1,3)
line=lm(counts~lengths)
plot(lengths, counts, col="green", log="xy")
abline(line, col="blue")

I've tried lots of things I've found on similar questions (e.g. using log10() and lines()) and they haven't worked with my data.

Answer

Ndr picture Ndr · Apr 3, 2013
abline(line, col="blue",untf=TRUE)

Oops, sorry, I didn't noticed the comment above.