2017-08-10 55 views
0

我想提出类似这里Indicating the statistically significant difference in bar graph问题geom_path表示条形图的统计显著差异

这些图的图形考虑下面的例子 库(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") 

enter image description here

好了,但现在我想添加一行表示条形图的统计显著差异。

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工作?

回答

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

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

+0

对于任何类型的注释的多于一个标签或点,我通常使用这种方法。这是最灵活的方式,在你想要的地方添加你想要的东西,尤其是面板。 – Brian