2013-02-19 26 views
16

这是最初在这里提出的一个问题的重复:Indicating the statistically significant difference in bar graph但要求R代替python。指示条形图中的统计显着性差异使用R

我的问题很简单。如果可能的话,我想在R中产生条形图,并指示不同条形之间的显着差异,例如,如果可能,使用ggplot2。产生这样的东西。我搜索了一下,但是找不到另一个问题完全一样的问题。

bar plot showing error bars and sig. diff marking beween bars

+0

那么只有相邻的列? A-C,B-D或A-D没有p值? – 2013-02-19 13:02:32

+0

令人讨厌的(不是你的错),我的本地防火墙中的东西正在杀死你的图像。你能发布一个链接吗? – 2013-02-19 13:19:34

+0

延迟道歉。图片在这里:http://i.stack.imgur.com/3I6El.jpg – 2013-02-19 17:28:00

回答

15

您可以使用geom_path()annotate()得到类似的结果。对于这个例子,你必须自己决定合适的位置。在geom_path()提供了四个数字来获得连接线的小滴答。

df<-data.frame(group=c("A","B","C","D"),numb=c(12,24,36,48)) 
g<-ggplot(df,aes(group,numb))+geom_bar(stat="identity") 
g+geom_path(x=c(1,1,2,2),y=c(25,26,26,25))+ 
    geom_path(x=c(2,2,3,3),y=c(37,38,38,37))+ 
    geom_path(x=c(3,3,4,4),y=c(49,50,50,49))+ 
    annotate("text",x=1.5,y=27,label="p=0.012")+ 
    annotate("text",x=2.5,y=39,label="p<0.0001")+ 
    annotate("text",x=3.5,y=51,label="p<0.0001") 

enter image description here

+0

谢谢 - 好吧,这有点烦琐,但这是我想要的!我想有没有现成的,一般(自动!)的方式来做到这一点 – 2013-02-19 17:30:43

+0

OOF,任何方式来控制使用faceting时的geom_path? – 2013-02-20 14:15:00

+0

@JimBo然后你将需要一个geom_path()值的数据框,并且在这个数据框中你还必须包含与将用于分面相同名称和级别的列。 – 2013-02-20 14:30:15

3

我从上面使用所提出的方法,但我发现用于制备比geom_path函数行注释功能更容易。只需使用“段”而不是“文本”。你必须按细分市场细分,并为每条线段定义开始和结束的x和y值。

例如制作3行段:

annotate("segment", x=c(1,1,2),xend=c(1,2,2), y= c(125,130,130), yend=c(130,130,125)) 
11

我知道这是一个老问题,并通过Didzis Elferts答案已经为这个问题的解决方案。但我最近建立了一个ggplot扩展,简化了添加的意义棒的全过程:ggsignif

代替繁琐加入geom_pathannotate你的阴谋,你只需要添加一个单层geom_signif

library(ggplot2) 
library(ggsignif) 

ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
    geom_boxplot() + 
    geom_signif(comparisons = list(c("versicolor", "virginica")), 
       map_signif_level=TRUE) 

Boxplot with significance bar

该软件包的完整文档可在CRAN获取。

相关问题