0

我想制作一个类似于此处的图表的图表指示条形图中的统计显着差异

考虑以下示例库(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 工作?

4

2 回答 2

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))

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

于 2017-08-10T11:20:39.857 回答
0

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

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

于 2017-08-10T11:27:41.330 回答