Add Spearman's test P-value to ggplot using stat_fit_glance

Create issue
Issue #17 resolved
Pedro J. Aphalo repo owner created an issue

I would like to add the p.value of the Spearman's correlation test on my ggplot.

I have a data frame, Global.log.CD8.Density:

  ID     Global.log.CD8.Density   Signature     Weight
IM_186         0.124566              s1       0.56427854
IM_152         5.041160              s1       0.28232970
IM_172         1.385508              s1       0.20986138
IM_148         6.067057              s1       0.42067503
IM_146         2.278153              s1       0.23883911
IM_174         5.481756              s1       0.05284056

There are 7 Signatures, s1-s7, for each ID with different Weight values. I have created a facet dot plot to show the correlation between Global.log.CD8.Density and Weight for each Signature, and displayed the p.value using the following code:

mapping = aes(x=Weight, y=Global.log.CD8.Density))+ 
facet_wrap(~Signature, nrow = 2)+
geom_smooth(method = "lm", se = F) +
   stat_fit_glance(method = 'lm',
                   method.args = list(formula = y~x),
                   geom = 'text',
                   aes(label = paste( "italic (p)== ", signif(..p.value.., digits = 4))),
                       label.x.npc = "right", label.y.npc = 0.10,
                       size = 3, parse=TRUE)

This pastes the p.value of the Pearson's correlation. I would like to know if there is a way to modify the code to display the Spearman's correlation instead. I know you can do the following code to get the result:

Global.CD8.spearman <- Global.log.CD8.Density %>% 
 group_by(Signature) %>% 
 do(tidy(cor.test(.$Weight, .$Global.log.CD8.Density, method='spearman')))

ggplot(Global.log.CD8.Density, aes(Weight,Global.log.CD8.Density))+
  facet_wrap(~Signature, nrow = 2)+
  geom_smooth(method = "lm", se = F) +
  geom_text (data=Global.CD8.spearman, 
         aes(0.55,0.55,label = ifelse(p.value>0.05,paste( "italic (p)== ", 
                                             signif(p.value, digits = 2)),"italic (p)<0.05")),
                       size = 3, parse=TRUE)

but I was wondering if there is a way to modify stat_fit_glance or any other ggpmisc tool to get that result.

Many thanks

Comments (2)

  1. Pedro J. Aphalo reporter

    This should be a question of finding out what value broom::glance() returns in the case of cor.test().

  2. Pedro J. Aphalo reporter

    This is not a bug. Example added to documentation showing how to achieve the intended annotation.

  3. Log in to comment