0

I am trying to add regression line equation, R square and P value to my plot, any thoughts on how I can do it?

Here is the code that I am using in R:

plot<- plot(Q,CALCIUM.DISSOLVED)
fit<-(lm(CALCIUM.DISSOLVED ~ Q))
abline (fit)
Gregor Thomas
  • 119,032
  • 17
  • 152
  • 277
soroush
  • 3
  • 3

1 Answers1

2

Using the data set cars

m <- lm(dist ~ speed, data = cars)

plot(dist ~ speed,data = cars)

abline(m, col = "red")

Here we use summary() to extract the R-squared and P-value.

m1 <-  summary(m)

mtext(paste0("R squared: ",round(m1$r.squared,2)),adj = 0)

mtext(paste0("P-value: ", format.pval(pf(m1$fstatistic[1], # F-statistic
                                     m1$fstatistic[2], # df
                                     m1$fstatistic[3], # df
                                     lower.tail = FALSE))))

mtext(paste0("dist ~ ",round(m1$coefficients[1],2)," + ", 
                   round(m1$coefficients[2],2),"x"),
      adj = 1)
  • Thanks, how do you adjust the number of digits for P value and adding the regression equation? – soroush Oct 04 '17 at 16:51
  • For ggplot2 users: https://stackoverflow.com/questions/7549694/adding-regression-line-equation-and-r2-on-graph – lawyeR Oct 04 '17 at 18:52