Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit line to data on a log scale in R

Tags:

plot

r

lm

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.

like image 795
Jessica B Avatar asked Apr 03 '13 11:04

Jessica B


People also ask

How do you change data to a log scale?

In your XY (scatter) graph, double-click the scale of each axis. In the Format Axis box, select the Scale tab, and then check Logarithmic scale.

What does Abline do in R?

The R function abline() can be used to add vertical, horizontal or regression lines to a graph.

What is the line of best fit in R?

Correlation Coefficient (r) o If r is close to 1 (or -1), the model is considered a "good fit". o If r is close to 0, the model is "not a good fit". o If r = ±1, the model is a "perfect fit" with all data points lying on the line. o If r = 0, there is no linear relationship between the two variables.


1 Answers

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

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

like image 75
Ndr Avatar answered Sep 26 '22 04:09

Ndr