3 回答

TA貢獻2021條經驗 獲得超8個贊
我知道這是一個古老的問題,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獲得。

TA貢獻1790條經驗 獲得超9個贊
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)
- 3 回答
- 0 關注
- 7163 瀏覽
添加回答
舉報