user1896450
user1896450

Reputation: 13

Add line/equation to scatter plot

I have 3 models, all of which are significant and I want to create a linear graph with my data. This is what I have so far:

>morpho<-read.table("C:\\Users\\Jess\\Dropbox\\Monochamus\\Morphometrics.csv",header=T,sep=",")
> attach(morpho)
> wtpro<-lm(weight~pronotum)
> plot(weight,pronotum)
> abline(wtpro)

I have tried entering the abline as:

abline(lm(weight~pronotum))

I can't figure out what I'm doing wrong. I want to add my equation, I have all of my coefficients but can't get past the line...I have even started over thinking maybe I messed up along the way and it still will not work. Is there a separate package that I am missing?

Upvotes: 1

Views: 13066

Answers (2)

lilster
lilster

Reputation: 933

Plot is in the format plot(x, y, ...) and it looks like you've ordered your dependent variable first. Easy mistake to make.

For example:

Set up some data

y <- rnorm(10)
x <- rnorm(10) + 5

A plot with the dependent variable placed on the x axis will not display the regression line as it's outside of the visible plane.

plot(y,x)
abline(lm(y~x), col='red', main='Check the axis labels')

Flip the variables in the plot command. Now it will be visible.

plot(x,y)
abline(lm(y~x), col='red', main='Check the axis labels')

Upvotes: 2

IRTFM
IRTFM

Reputation: 263301

Try:

abline(coef(lm(weight~pronotum))  # works if dataframe is attached.

I try to avoid attach(). It creates all sorts of anomalies that increase as you do more regression work. Better would be:

wtpro<-lm(weight~pronotum, data= morpho)
with( morpho ,  plot(weight,pronotum) )
abline( coef(wtpro) )

Upvotes: 3

Related Questions