Correlation plot between two variables with line and person r value in graph - seeking alternate example
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: http://www.sthda.com/english/wiki/correlation-test-between-two-variables-in-r
However, I'm using r studio server and creating a shiny app. Library ggpubr will simply not install. I've tried several ways to get this library to install.
So, does anyone have an alternate example that might work?
Cheers ~!
Solution 1:
How about this:
library(ggplot2)
data(mtcars)
r <- round(cor(mtcars$wt, mtcars$mpg), 2)
p <- cor.test(mtcars$wt, mtcars$mpg)$p.value
ggplot(mtcars, aes(y=wt, x=mpg)) +
geom_point() +
geom_smooth(method="lm", col="black") +
annotate("text", x=20, y=4.5, label=paste0("r = ", r), hjust=0) +
annotate("text", x=20, y=4.25, label=paste0("p = ", round(p, 3)), hjust=0) +
theme_classic()