ggsignif软件包错误stat

ggsignif软件包错误stat

本文介绍了ggsignif软件包错误stat_signif需要以下缺失的美感:y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据的虚构示例:

This is an invented example of my data:

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.

I'm trying to represent this data in the form of a bar plot and then indicate a statistically significant difference in the proportion of alive and dead patients between the two experimental groups (cases and controls). I performed a Pearson's chi square test and the p-value is 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

任何人都可以告诉我为什么会发生这种情况以及如何解决吗?

Could anyone please tell me why this is happening and how can I solve it?

谢谢

推荐答案

如错误消息所示, geom_signif 需要 y 美观,您未指定任何.

As indicated by the error message, geom_signif requires a y aesthetic, and you didn’t specify any.

y = stat(count/sum(count)) geom_bar 移至您的整体美感,或将其添加到 geom_signif '的美学.

Either move y = stat(count/sum(count)) from geom_bar to your global aesthetics, or add it to geom_signif’s aesthetics.

接下来,解决您的审美问题:使用 x y data $ x data $ y >.此外,您在 geom_signif 中出错: map_signif_level = TRUE 必须在比较外部.

Next, fix your aesthetics: instead of data$x and data$y, use x and y. Furthmore, you have an error in geom_signif: map_signif_level = TRUE needs to be outside the comparisons.

最后, geom_signif 似乎无法使用美学计算的统计数据.因此,您需要预先计算此统计信息,例如通过dplyr:

Finally, geom_signif doesn’t seem to be able to work with computed statistics in aesthetics. So you need to compute this statistic beforehand, e.g. via 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
    )

这篇关于ggsignif软件包错误stat_signif需要以下缺失的美感:y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:21