比较ggplot中的依赖比例

时间:2018-08-16 23:35:11

标签: r ggplot2 ggpubr

我的问题

我想使用mcnemar.testggplot2ggalluvial (或另一个比较依存比例的测试)的结果添加到Sankey图中。 >

我的测试数据:

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

我的情节:

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)

给我:

Sankey Diagram

我尝试获得的内容:

现在,我想添加括号,以显示mcnemar.test(例如p值)的结果,该结果比较基线与方法A和B之间的依存比例。

What I try to get

我想到了类似stat_compare_meanshttp://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/76-add-p-values-and-significance-levels-to-ggplots/)中的ggpubr。但是,stat_compare_means仅涵盖比较均值的方法,我该如何比较比例?由于我的真实数据集很大,因此我尝试找到一种将测试结果自动添加到ggplot中的方法。

0 个答案:

没有答案