Jessica B
Jessica B

Reputation: 321

Fit line to data on a log scale in R

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.

Upvotes: 13

Views: 12268

Answers (1)

Ndr
Ndr

Reputation: 574

abline(line, col="blue",untf=TRUE)

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

Upvotes: 14

Related Questions