【问题标题】:problems with geom_path indicating the statistically significant difference in bar graphgeom_path 的问题表明条形图中的统计显着差异
【发布时间】:2017-08-10 11:11:37
【问题描述】:

我想制作一个类似于Indicating the statistically significant difference in bar graph 此处的图表的图表

考虑以下示例 库(ggplot2)

# my data
my_data <- data.frame(x = c("No","Yes"), y=c(5,25), lower = c(1,10), upper = c(15,50))

我用一些误差线制作了一个条形图,效果很好。

my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
    geom_bar(stat="identity", fill="grey", width=0.5) +
    geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
    coord_cartesian(ylim = c(0,70))

好的,现在我想添加一些带有 p 值的注释,也可以正常工作。

 my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
    geom_bar(stat="identity", fill="grey", width=0.5) +
    geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
    coord_cartesian(ylim = c(0,70)) +
    annotate("text",x=1.5,y=65,label="p<0.001")

好吧,但现在我想在条形图中添加一条表示统计显着差异的线。

 my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
    geom_bar(stat="identity", fill="grey", width=0.5) +
    geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
    coord_cartesian(ylim = c(0,70)) +
    annotate("text",x=1.5,y=65,label="p<0.001") +
    geom_path(x=c(1,1,2,2),y=c(55,60,60,55))

现在我没有工作。那么geom_path有什么问题呢?我尝试用 x 更改映射。

 my_data <- data.frame(x = c(1,2), y=c(5,25), lower = c(1,10), upper = c(15,50))

 my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
    geom_bar(stat="identity", fill="grey", width=0.5) +
    geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
    coord_cartesian(ylim = c(0,70)) +
    annotate("text",x=1.5,y=65,label="p<0.001") +
    geom_path(x=c(1,1,2,2),y=c(55,60,60,55))

还是不行。我可以做些什么来使 geom_path 工作?

【问题讨论】:

    标签: r ggplot2 mapping geom-bar significance


    【解决方案1】:

    在尝试之后,我想出了以下解决方案。

    my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
      geom_bar(stat="identity", fill="grey", width=0.5) +
      geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
      coord_cartesian(ylim = c(0,70)) +
      annotate("text",x=1.5,y=65,label="p<0.01")
      geom_path(data = data.frame(x=c(1,1,2,2),y=c(58,60,60,58)), aes(x=x,y=y))
    

    有点乱,可能有更好的答案。

    【讨论】:

    • 对于任何类型的多于单个标签或点的注释,我通常使用这种方法。它是最灵活的,可以准确地添加您想要的内容,尤其是使用多面面板。
    【解决方案2】:

    上一篇提出相同问题的帖子可能很有用:Indicating the statistically significant difference in bar graph USING R

    ggsignif 包是另一种选择。 例如,请参阅介绍性小插曲:https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html

    【讨论】:

    • 我知道那个链接,但是在第一个示例中提供的代码在我的情况下不起作用。我不得不为 geom_path 创建一个新的数据框。
    猜你喜欢
    • 1970-01-01
    • 2012-07-16
    • 2013-02-04
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多