R 将stat_signif与自定义测试ggplot2一起使用

R 将stat_signif与自定义测试ggplot2一起使用,r,ggplot2,geom-bar,chi-squared,R,Ggplot2,Geom Bar,Chi Squared,我的问题与这里发布的问题非常相似,只是我有多个组,所以我需要在每组之间进行后续的卡方比较,而不是代表整体卡方检验。据我所知,stat_signif没有post-hoc卡方的参数,但它接受自定义公式,只要它有一个名为p-value的条目列表 我正在使用不推荐的fifer包中的代码进行后期卡方检验,我还编辑了公式,因此输出有一列标记为p.value(请参阅本文底部编辑的公式) 下面是我的一个数据示例(组成) 如果我在没有指定测试的情况下使用相同的语法,它会工作,但会自动执行wilcox测试,我会得到

我的问题与这里发布的问题非常相似,只是我有多个组,所以我需要在每组之间进行后续的卡方比较,而不是代表整体卡方检验。据我所知,stat_signif没有post-hoc卡方的参数,但它接受自定义公式,只要它有一个名为p-value的条目列表

我正在使用不推荐的fifer包中的代码进行后期卡方检验,我还编辑了公式,因此输出有一列标记为p.value(请参阅本文底部编辑的公式)

下面是我的一个数据示例(组成)

如果我在没有指定测试的情况下使用相同的语法,它会工作,但会自动执行wilcox测试,我会得到以下错误:

Warning message:
In wilcox.test.default(c(4, 1), c(1, 4)) :
  cannot compute exact p-value with ties
chisq_列表%
计数(二进制_1)%>%
变异(频率=n)%>%
ggplot()+
aes(条件、频率、填充=二进制_1)+
几何坐标(position=“stack”)+
几何符号(
比较=列表(c(“a”、“b”)、c(“a”、“c”)、c(“b”、“c”),
test=“chisq_列表”,
映射符号级别=真
) +
刻度填充手册(数值=c(“#FDAE61“,”#9E0142“),name=“Binary_1”)
如何保存事后测试中的p值,以便将其显示在图形上

Warning message:
Computation failed in `stat_signif()`:
'c(4, 2)' is not a function, character or symbol
Warning message:
In wilcox.test.default(c(4, 1), c(1, 4)) :
  cannot compute exact p-value with ties
gg_1.2 <- gg_melt %>%
  group_by(condition) %>%
  count(binary_1) %>%
  mutate(Freq = n) %>%
  ggplot() +
  aes(condition, Freq, fill = binary_1) +
  geom_col(position = "stack") +
  geom_signif(
    comparisons = list(c("a", "b"), c("a", "c"), c("b", "c")),
    map_signif_level = TRUE
  ) +
  scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Binary_1")

gg_1.2
chisq.post.hoc <- function(tbl,test=c("fisher.test"), popsInRows=TRUE,control=c("fdr","BH","BY","bonferroni","holm","hochberg","hommel"),digits=4, ...) {
  #### extract correction method
  control <- match.arg(control)
  
  #### extract which test (fisher or chi square) 
  test = match.fun(test)
  
  #### test rows or columns
  if (!popsInRows) tbl <- t(tbl)
  popsNames <- rownames(tbl)
  
  #### come up with all possible comparisons
  prs <- combn(1:nrow(tbl),2)
  
  #### preallocate  
  tests <- ncol(prs)
  pvals <- numeric(tests)
  lbls <- character(tests)
  for (i in 1:tests) {
    pvals[i] <- test(tbl[prs[,i],], ...)$p.value
    lbls[i] <- paste(popsNames[prs[,i]],collapse=" vs. ")
  }
  adj.pvals <- p.adjust(pvals,method=control)
  cat("Adjusted p-values used the",control,"method.\n\n")
  data.frame(comparison=lbls,raw.p=round(pvals,digits),p.value=round(adj.pvals,digits))
}
chisq_matrix <- as.matrix(table(df$condition, df$binary_1))
chisq.test(chisq_matrix)

chisq.post.hoc(chisq_matrix, test='chisq.test')
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'chisq_list' of mode 'function' was not found

chisq_list <- chisq.post.hoc(chisq_matrix, test='chisq.test')

gg_1.3 <- gg_melt %>%
  group_by(condition) %>%
  count(binary_1) %>%
  mutate(Freq = n) %>%
  ggplot() +
  aes(condition, Freq, fill = binary_1) +
  geom_col(position = "stack") +
  geom_signif(
    comparisons = list(c("a", "b"), c("a", "c"), c("b", "c")),
    test = "chisq_list",
    map_signif_level = TRUE
  ) +
  scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Binary_1")