I'm trying to create a ggplot
and add results of a correlation test I have done.
Something along the lines of:
p+annotate("text",x=12.5,y=15.25,label=c(cor.test$estimate,cor.test$p.value))
I keep getting error messages no matter what I try. Any ideas?
To add correlation coefficient with P-value to a scatter plot, we use the stat_cor() function of the ggpubr package in the R Language. The ggpubr package provides some easy-to-use functions for creating and customizing ggplot2 plots.
I have actually managed to add stat details to the plot by using stat_cor()
from the package ggpubr
library(ggpubr)
p+stat_cor(method="pearson")
There is a package in development that can do this for you (ggstatsplot
is on CRAN).
Here is an example of how to create correlation plot:
ggstatsplot::ggscatterstats(data = iris, x = Sepal.Length, y = Sepal.Width)
This will produce a plot that looks like the following (you can similarly get results from Spearman's rho (type = 'spearman'
) or robust correlation test (type = 'robust'
)):
Check out the documentation of the function for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With