Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add means of each x level in a regression plot?

I am plotting a linear regression fit for the Loblolly data in R and trying to add each mean y for each x level. My code is below:

 plot(Loblolly$height~Loblolly$age, xlab = "Age", ylab = "Height", main = "Loblolly Data")
    lob.fit <- lm(Loblolly$height~Loblolly$age)
    lob.sum <- summary(lob.fit)
    str(lob.sum)
    abline(lob.fit)

This is probably a really easy question with a really easy answer but I don’t know how to add the means. When I google all that shows up is ggplot and I’m not supposed to be using that. Any help?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As @AndroMine stated in comments, you could use aggregate in points to get the means. Choose a nice pch= such as "-", give it a col=or and increase the size of the cex=. Note that the functions you use have a data= argument, so you don’t have to keep typing Loblolly$.

plot(height ~ age, Loblolly, xlab="Age", ylab="Height", main="Loblolly Data")
abline(lm(height ~ age, Loblolly))
points(aggregate(height ~ age, Loblolly, mean), pch='-', cex=4, col=4)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading