6

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?

B.Shermeister
  • 427
  • 2
  • 5
  • 13

2 Answers2

16

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")
B.Shermeister
  • 427
  • 2
  • 5
  • 13
4

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.

Community
  • 1
  • 1
Indrajeet Patil
  • 4,222
  • 2
  • 15
  • 44
  • 1
    @B.Shermeister Has this question been answered to your liking? If yes, please accept the answer (stackoverflow.com/help/someone-answers) so that this thread will be closed. – Indrajeet Patil Feb 17 '18 at 22:11
  • I am trying to create a `ggplot` that will show the result of a ttest (or corr test) on the plot. I am now using the line `annotate("text",x=1,y=10,label='atop(bold("P-value = 0.286"))',cex=7,parse=TRUE)`. The only problem is that I have to change the value manually each time. I would like to insert `ttest$p.value` instead of the number in the label – B.Shermeister May 03 '18 at 18:35