【问题标题】:ggsignif package error stat_signif requires the following missing aesthetics: yggsignif 包错误 stat_signif 需要以下缺失的美学:y
【发布时间】:2019-07-15 09:15:53
【问题描述】:

这是我的数据的一个发明示例:

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

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

谢谢

【问题讨论】:

  • 您是否尝试在ggplot() 中指定y 而不仅仅是在geom_bar() 中?除了在aes() 中使用裸列名称而不是data$x 等。
  • 嗯。除了 Markus 的评论,geom_signif 似乎无法与 geom_bar 一起使用。

标签: r ggplot2 bar-chart p-value


【解决方案1】:

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

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

接下来,修复你的美学:使用xy,而不是data$xdata$y。此外,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
    )

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多