該包目前僅適用于利用 ggplot2 進行繪制的圖形
我們在利用ggplot2進行數據可視化的時候經常會需要對兩組數據進行比較,并添加顯著性標記,自己學習之余,也給大家分享一個好用的添加顯著性標記的包:ggsignif。
-
ggsignif: Significance Brackets for ‘ggplot2’
PART1:安裝
#從cran安裝:
install.packages("ggsignif")
#從GitHub安裝(最新版):
install.packages("remotes")
remotes::install_github("const-ae/ggsignif")
PART2:函數部分參數詳解
?geom_signif
函數幫助文檔如下:
geom_signif(
mapping = NULL,
data = NULL,
stat = "signif",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
comparisons = NULL,
test = "wilcox.test",
test.args = NULL,
annotations = NULL,
map_signif_level = FALSE,
y_position = NULL,
xmin = NULL,
xmax = NULL,
margin_top = 0.05,
step_increase = 0,
extend_line = 0,
tip_length = 0.03,
size = 0.5,
textsize = 3.88,
family = "",
vjust = 0,
parse = FALSE,
manual = FALSE,
orientation = NA,
...
)
在這些里面,我們常用的一些參數就是這些啦:
· comparisons #指定比較對象,是一個由長度為2的向量組成的列表
· test #指定使用的檢驗方法,包括 wilcox.test 和 t.test
· annotations #指定注釋的內容
· map_signif_level #布爾型變量,如果為TRUE,就用諸如***的形式來展示顯著性差異
· y_position #指定標記在y軸方向上的坐標,是個數字向量
· xmin, xmax #指定標記在x軸方向上的位置,是個數字向量
· tip_length #指定標記小豎線的長度,是個數字向量
· size #指定標記尺寸,即線條的粗線
· textsize #指定標記中文字部分的大小
· family #指定標記中文字部分的字體
· vjust #指定標記部分中的文字與標記中的短橫線之間的距離,負值在橫線上方,正值在下方
· orientation #指定整個圖的方向,一般不會修改,如果想讓圖“躺下”,就設置成為"y"
PART3:示例——以ggplot2內置數據集mpg為例
問題描述:
我們想繪制一個箱型圖,橫軸為每輛車的車型,縱坐標為hwy,使用mpg數據集,其結構如下:
library(ggplot2)
head(mpg)
# A tibble: 6 x 11
manufacturer model displ year cyl trans drv cty hwy fl class
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
3 audi a4 2 2008 4 manual(m6) f 20 31 p compact
4 audi a4 2 2008 4 auto(av) f 21 30 p compact
5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
開始繪制:
library(ggsignif)
ggplot(data = mpg, aes(x = class, y = hwy)) +
geom_boxplot(aes(group = class, fill = class), size = 1) + #指定箱子線條寬度為1
geom_signif(comparisons = list(c("midsize", "minivan"), c("compact", "midsize")), #指定比較對象
test = "t.test", #指定檢驗方法
y_position = c(40, 47), #指定兩個標記在y軸上的坐標,按照前面指定比較對象的順序
size = 1, #指定標記中線條的尺寸
textsize = 5, #指定標記中文字部分的大小
vjust = -0.3, #指定標記中文字部分與橫線之間的距離
tip_length = c(0.2, 0.45, 0.05, 0.05), #指定短豎線的長度
map_signif_level = T) +
scale_y_continuous(limits = c(10, 55)) +
theme(legend.position = "none",
panel.background = element_rect(fill = NA),
panel.border = element_rect(fill = NA, colour = "black", size = 1),
axis.title = element_text(size = 15, face = "bold"),
axis.text.x = element_text(size = 12, hjust = 1, angle = 45, color = "black"),
axis.title.y = element_text(size = 12, color = "black"))
成圖:
丑是丑了點,但還是湊合,畢竟針對主題的修改美化沒有花大力氣。需要注意的幾個點:
- 關于y軸范圍:
y軸的范圍在這里很重要,范圍不合適會直接導致顯著性標記溢出圖外,顯示不全,所以可以用 scale_y_*(limits = numeric.vecter) 或者 ylab() 指定范圍,保證好看的前提下盡可能大一點。 - 關于短豎線的長度:
短豎線長度指定的數值向量中數值的順序應該是與你指定的比較對象的出現順序是一致的,這個參數可以慢慢一個一個修改,多試試就好了。
PART4:來點花的——何必一定是顯著性標記?
前面的函數幫助文檔里有 annotations 參數,讓人不禁想試試(狗頭)。
話不多說,直接開始:
問題描述:我們想把上面的圖中的 NS. 和 *** 分別替換成為字母 A 和 B。
一樣的代碼:
ggplot(data = mpg, aes(x = class, y = hwy)) +
geom_boxplot(aes(group = class, fill = class), size =1) +
geom_signif(comparisons = list(c("midsize", "minivan"), c("compact", "midsize")),
annotations = c("B", "A"), #差別在這兒
y_position = c(40, 47),
size = 1,
textsize = 5,
vjust = -0.3,
tip_length = c(0.2, 0.45, 0.05, 0.05)) +
scale_y_continuous(limits = c(10, 55)) +
theme(legend.position = "none",
panel.background = element_rect(fill = NA),
panel.border = element_rect(fill = NA, colour = "black", size = 1),
axis.title = element_text(size = 15, face = "bold"),
axis.text.x = element_text(size = 12, hjust = 1, angle = 45, color = "black"),
axis.title.y = element_text(size = 12, color = "black"))
出圖:
PART5:讓圖“躺”下來?
- 第一種方法:使用 orientation 參數:
ggplot(data = mpg, aes(x = hwy, y = class)) + #這里改了
geom_boxplot(aes(group = class, fill = class), size =1, orientation = "y") +
geom_signif(comparisons = list(c("midsize", "minivan"), c("compact", "midsize")),
test = "t.test",
y_position = c(40, 47),
size = 1,
textsize = 5,
vjust = -0.3,
tip_length = c(0.2, 0.45, 0.05, 0.05),
map_signif_level = T,
orientation = "y") + #這里改了
scale_x_continuous(limits = c(10, 55)) + #這里改了
theme(legend.position = "none",
panel.background = element_rect(fill = NA),
panel.border = element_rect(fill = NA, colour = "black", size = 1),
axis.title = element_text(size = 15, face = "bold"),
axis.text.x = element_text(size = 12, hjust = 1, angle = 45, color = "black"),
axis.title.y = element_text(size = 12, color = "black"))
- 第二種方法:使用 coord_flip() :
ggplot(data = mpg, aes(x = class, y = hwy)) +
geom_boxplot(aes(group = class, fill = class), size =1) +
geom_signif(comparisons = list(c("midsize", "minivan"), c("compact", "midsize")),
test = "t.test",
y_position = c(40, 47),
size = 1,
textsize = 5,
vjust = -0.3,
tip_length = c(0.2, 0.45, 0.05, 0.05),
map_signif_level = T) +
scale_y_continuous(limits = c(10, 55)) +
theme(legend.position = "none",
panel.background = element_rect(fill = NA),
panel.border = element_rect(fill = NA, colour = "black", size = 1),
axis.title = element_text(size = 15, face = "bold"),
axis.text.x = element_text(size = 12, hjust = 1, angle = 45, color = "black"),
axis.title.y = element_text(size = 12, color = "black")) +
coord_flip() #只改了這里
出圖:
最后附上GitHub鏈接,有什么問題就去逛逛和留言吧~
https://github.com/const-ae/ggsignif