Francis Sherwood
Francis Sherwood

Reputation: 37

abline() function not showing a line in plot

Attempting to plot a line of best fit along with my data set in R. The data was imported from a .csv file.

Everything seems to work properly except for the abline() function.

Data=read.csv("R/SpecArea.csv")
Data

new=log10(Data)
new

fit=lm(new)

fit

plot(new)
abline(fit)

Thanks for any help.

This is the output of the entire program:

Data=read.csv("R/SpecArea.csv")
Data
    area species
1  44164      79
2  29979      79
3   4411      68
4   3423      54
5    583      34
6    385      41
7    305      40
8    233      44
9    160      16
10   133      38
11   120      22
12   108      23
13    80      29
14    71      29
15    68      21
16    62      22
17    38      24
18    33      17
19    32      24
> 
> new=log10(Data)
> 
> new
       area  species
1  4.645068 1.897627
2  4.476817 1.897627
3  3.644537 1.832509
4  3.534407 1.732394
5  2.765669 1.531479
6  2.585461 1.612784
7  2.484300 1.602060
8  2.367356 1.643453
9  2.204120 1.204120
10 2.123852 1.579784
11 2.079181 1.342423
12 2.033424 1.361728
13 1.903090 1.462398
14 1.851258 1.462398
15 1.832509 1.322219
16 1.792392 1.342423
17 1.579784 1.380211
18 1.518514 1.230449
19 1.505150 1.380211
> 
> fit=lm(new)
> 
> fit

Call:
lm(formula = new)

Coefficients:
(Intercept)      species  
     -3.508        3.941  


 plot(new)
 abline(fit)

Sorry for the poor formatting... I’m posting on a mobile device

Upvotes: 1

Views: 7325

Answers (1)

Dave2e
Dave2e

Reputation: 24079

Your equation for fit is not matching your plot statement. lm(new) is assuming "species" is the independent variable while plot(new) is assuming "area" is.

Try using this:

fit<-lm(species ~ area, data = new)

#Call:
#lm(formula = species ~ area, data = new)

#Coefficients:
#(Intercept)         area  
#     1.0223       0.2002  

enter image description here

Upvotes: 1

Related Questions