How to show Trendline EQ and R^2 value on scatter plot

Hi -
So I'm currently trying to learn how to place the trendline equation and r^2 values onto a scatter plot.
Here's a sample data set that I'm working with -
starchSOH <- c(5.28,7.54,9.83,10.53,10.82,11.0,11.18)
pH <- c(0,.2,.4,.6,.8,1,1.2)

What would be the proper format for the AB line, and how do I display the trend line equation and r^2 value onto the scatter plot itself?

Feel free to use a different set of data for an example - that was just an arbitrary one I made up.

Thanks in advance!

Here is how I would do it with ggplot2 and ggmisc.

library(ggplot2)
library(ggpmisc)
DF <- data.frame(starchSOH = c(5.28,7.54,9.83,10.53,10.82,11.0,11.18),
                 pH = c(0,.2,.4,.6,.8,1,1.2))

ggplot(DF,aes(starchSOH,pH)) + geom_point() + geom_smooth(method="lm") +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
             label.x.npc = "left", label.y.npc = 0.90, #set the position of the eq
             formula = y ~ x, parse=TRUE,rr.digits = 3)
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-10-15 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.