Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding correlation test results to ggplot

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?

like image 411
B.Shermeister Avatar asked Feb 16 '18 16:02

B.Shermeister


People also ask

How do you add correlation coefficient to ggplot2?

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.


2 Answers

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")
like image 60
B.Shermeister Avatar answered Sep 27 '22 01:09

B.Shermeister


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')):

enter image description here

Check out the documentation of the function for more.

like image 38
Indrajeet Patil Avatar answered Sep 25 '22 01:09

Indrajeet Patil