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:
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.
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_means
only 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.