在ggplot条形图和框形图上放上星号-表示显着性水平(p值)

通常将星标放在条形图或箱形图上以显示一组或两组之间的显着性水平(p值),以下是几个示例:

http://img2.mukewang.com/5db2a6ac000143c202780488.jpghttp://img4.mukewang.com/5db2a6ad0001d75203700281.jpghttp://img2.mukewang.com/5db2a6ae0001d47402470270.jpg

星级数由p值定义,例如p值<0.001的可以放置3颗星,p值<0.01的可以放置2颗星,依此类推(尽管这从一篇文章变为另一篇文章)。

我的问题是:如何生成相似的图表?一种基于显着性水平自动放置星星的方法非常受欢迎。


白衣染霜花
浏览 6975回答 3
3回答

宝慕林4294392

我知道这是一个古老的问题,Jens Tierling的答案已经为该问题提供了一种解决方案。但是我最近创建了一个ggplot-extension,它简化了添加重要性栏的整个过程:ggsignif不必单调地将geom_line和添加geom_text到绘图中,而只需添加一个图层geom_signif:library(ggplot2)library(ggsignif)ggplot(iris, aes(x=Species, y=Sepal.Length)) +&nbsp;&nbsp; geom_boxplot() +&nbsp; geom_signif(comparisons = list(c("versicolor", "virginica")),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map_signif_level=TRUE)要创建类似于Jens Tierling所示的高级绘图,可以执行以下操作:dat <- data.frame(Group = c("S1", "S1", "S2", "S2"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sub&nbsp; &nbsp;= c("A", "B", "A", "B"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = c(3,5,7,8))&nbsp;&nbsp;ggplot(dat, aes(Group, Value)) +&nbsp; geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) +&nbsp; geom_signif(stat="identity",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data=data.frame(x=c(0.875, 1.875), xend=c(1.125, 2.125),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y=c(5.8, 8.5), annotation=c("**", "NS")),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aes(x=x,xend=xend, y=y, yend=y, annotation=annotation)) +&nbsp; geom_signif(comparisons=list(c("S1", "S2")), annotations="***",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y_position = 9.3, tip_length = 0, vjust=0.4) +&nbsp; 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",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color = "dose", palette = "jco")+&nbsp;&nbsp; stat_compare_means(comparisons = my_comparisons, label.y = c(29, 35, 40))+&nbsp; stat_compare_means(label.y = 45)
打开App,查看更多内容
随时随地看视频慕课网APP