宝慕林4294392
我知道这是一个古老的问题,Jens Tierling的答案已经为该问题提供了一种解决方案。但是我最近创建了一个ggplot-extension,它简化了添加重要性栏的整个过程:ggsignif不必单调地将geom_line和添加geom_text到绘图中,而只需添加一个图层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)要创建类似于Jens Tierling所示的高级绘图,可以执行以下操作:dat <- data.frame(Group = c("S1", "S1", "S2", "S2"), Sub = c("A", "B", "A", "B"), Value = c(3,5,7,8)) ggplot(dat, aes(Group, Value)) + geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) + geom_signif(stat="identity", data=data.frame(x=c(0.875, 1.875), xend=c(1.125, 2.125), y=c(5.8, 8.5), annotation=c("**", "NS")), aes(x=x,xend=xend, y=y, yend=y, annotation=annotation)) + geom_signif(comparisons=list(c("S1", "S2")), annotations="***", y_position = 9.3, tip_length = 0, vjust=0.4) + scale_fill_manual(values = c("grey80", "grey20"))该软件包的完整文档可在CRAN获得。
富国沪深
ggsignif软件包的扩展也称为ggpubr,在进行多组比较时功能更强大。它建立在ggsignif的基础上,还可以处理方差分析和kruskal-wallis以及针对全局均值的成对比较。例:library(ggpubr)my_comparisons = list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco")+ stat_compare_means(comparisons = my_comparisons, label.y = c(29, 35, 40))+ stat_compare_means(label.y = 45)