0

My problem

I would like to add results of a mcnemar.test (or another test that compares dependent proportions) to a Sankey Diagram using ggplot2 and ggalluvial

My test data:

df <- data.frame(timepoint=rep(0:2, each=10),response=c("A","A","A","A","A","A","A","A","B","B","A","A","A","A","A","A","A","B","B","B","A","B","B","B","B","B","A","A","B","B"),variable=rep(c("var1","var2"),each=5, 3), subject=rep(1:5,6))
df$timepoint <- factor(df$timepoint, level=c(1,0,2), labels=c("method_A","baseline","method_B"))

df %>% add_count(timepoint,variable,response) %>% add_count(timepoint,variable) %>% mutate(freq=n/nn*100) %>% mutate(total=1) -> df

My plot:

ggplot(df,
       aes(x = timepoint, stratum = response, alluvium = subject,
           y = total,
           fill = response, label = paste(freq,"%") )) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  facet_grid(.~variable)

gives me:

Sankey Diagram

What I try to get:

Now I would like to add brackets showing the results of a mcnemar.test (for example p-values) that compares the dependent proportions between baseline and methods A and B.

What I try to get

I thought about something like stat_compare_means from ggpubr (http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/76-add-p-values-and-significance-levels-to-ggplots/). However, stat_compare_meansonly covers methods to compare means, how could I compare proportions? Since my real data set is quite large, I try to find a way to add the test results automatically to the ggplot.

captcoma
  • 1,768
  • 13
  • 29
  • 1
    Check out `ggsignif` although the `mcnemar.test` is difficult as it needs a 2x2 matrix as input https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html – G_T Aug 17 '18 at 03:11
  • thank you for your hint. as I understood it correctly, I need to wrap the mcnemar.test in a new function. (https://github.com/const-ae/ggsignif/issues/37). However, I fail to write a function that gives me a list with p-values as it would be needed (https://cran.r-project.org/web/packages/ggsignif/ggsignif.pdf) – captcoma Aug 17 '18 at 07:48

0 Answers0