ggsignif包错误stat_signif需要以下缺少美学:y

问题描述 投票:3回答:1

这是我数据的发明示例:

x <- c("Control", "Case", "Case", "Case", "Control", "Control", "Control", "Case", "Case", "Case")
y <- c("Dead", "Dead", "Dead", "Alive", "Alive", "Dead", "Dead", "Dead", "Alive", "Dead")

我试图以条形图的形式表示这些数据,然后表明两个实验组(病例和对照组)中活着和死亡患者比例的统计学显着差异。我进行了Pearson的卡方检验,p值为4.674e-06。

这是我的情节代码:

library(ggsignif)

ggplot(data, aes(x = data$x,
             fill = data$y)) + 
geom_bar(aes(y = stat(count/sum(count))), position = position_dodge(0.9)) + 
theme(plot.title = element_text(hjust = 0.5)) +
ylim(c(0, 0.4)) +
labs(x = NULL, y = "Proportion", fill = NULL) +
scale_x_discrete(labels = c("Control", "Case")) +
geom_signif(comparisons = list(c("Control", "Case"), map_signif_level = TRUE))

但后来我得到:

Error: stat_signif requires the following missing aesthetics: y

有谁能告诉我为什么会这样,我该如何解决?

谢谢

r ggplot2 bar-chart p-value
1个回答
1
投票

如错误消息所示,geom_signif需要y美学,而您没有指定任何。

要么将y = stat(count/sum(count))geom_bar移动到你的全球美学,要么将其添加到geom_signif的美学中。

接下来,修复你的美学:使用data$xdata$y而不是xy。此外,你在geom_signif有一个错误:map_signif_level = TRUE需要在comparisons之外。

最后,geom_signif似乎无法在美学中使用计算统计数据。所以你需要预先计算这个统计数据,例如通过dplyr:

data %>%
    group_by(x) %>%
    count(y) %>%
    mutate(Freq = n / sum(n)) %>%
    ggplot() +
    aes(x, Freq, fill = y) +
    geom_col(position = position_dodge(0.9)) +
    geom_signif(
        comparisons = list(c("Control", "Case")),
        map_signif_level = TRUE
    )
© www.soinside.com 2019 - 2024. All rights reserved.