这是我数据的发明示例:
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
有谁能告诉我为什么会这样,我该如何解决?
谢谢
如错误消息所示,geom_signif
需要y
美学,而您没有指定任何。
要么将y = stat(count/sum(count))
从geom_bar
移动到你的全球美学,要么将其添加到geom_signif
的美学中。
接下来,修复你的美学:使用data$x
和data$y
而不是x
和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
)