Would just like some clarity here and a different example if someone has one.
Initially I wanted to use this example because it has the graph, the mean line, and the r value all presented in the graph: 
CodePudding user response:
You could use the geom_smooth function from ggplot2 and implement the correlation coefficient aswell as the p-value as follows:
library(ggplot2)
my_data <- mtcars
cor_coefs <- cor.test(my_data$mpg, my_data$wt)
ggplot(data = my_data, aes(x = mpg, y = wt))
geom_point()
geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE)
annotate("text", x = 30, y = 4, label = paste0("R: ", round(cor_coefs$estimate, 2)))
annotate("text", x = 30, y = 3.5, label = paste0("p-value: ", round(cor_coefs$p.value, 10)))
cor_coefs safes the correlation test and you can use it to get the desired values. With annotate from ggplot2, you need to specify the x and y position of your text. You could implement that dynamically based on your needs (since you have not provided any data).
