使用ggsignif優(yōu)雅添加顯著性標(biāo)記詳解
引言
該包目前僅適用于利用 ggplot2 進(jìn)行繪制的圖形
我們在利用ggplot2進(jìn)行數(shù)據(jù)可視化的時候經(jīng)常會需要對兩組數(shù)據(jù)進(jìn)行比較,并添加顯著性標(biāo)記,自己學(xué)習(xí)之余,也給大家分享一個好用的添加顯著性標(biāo)記的包:ggsignif。
ggsignif: Significance Brackets for ‘ggplot2’
PART1:安裝
#從cran安裝: install.packages("ggsignif") #從GitHub安裝(最新版): install.packages("remotes") remotes::install_github("const-ae/ggsignif")
PART2:函數(shù)部分參數(shù)詳解
?geom_signif
函數(shù)幫助文檔如下:
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, ... )
在這些里面,我們常用的一些參數(shù)就是這些啦:
· comparisons #指定比較對象,是一個由長度為2的向量組成的列表
· test #指定使用的檢驗(yàn)方法,包括 wilcox.test 和 t.test
· comparisons #指定比較對象,是一個由長度為2的向量組成的列表 · test #指定使用的檢驗(yàn)方法,包括 wilcox.test 和 t.test · annotations #指定注釋的內(nèi)容 · map_signif_level #布爾型變量,如果為TRUE,就用諸如***的形式來展示顯著性差異 · y_position #指定標(biāo)記在y軸方向上的坐標(biāo),是個數(shù)字向量 · xmin, xmax #指定標(biāo)記在x軸方向上的位置,是個數(shù)字向量 · tip_length #指定標(biāo)記小豎線的長度,是個數(shù)字向量 · size #指定標(biāo)記尺寸,即線條的粗線 · textsize #指定標(biāo)記中文字部分的大小 · family #指定標(biāo)記中文字部分的字體 · vjust #指定標(biāo)記部分中的文字與標(biāo)記中的短橫線之間的距離,負(fù)值在橫線上方,正值在下方 · orientation #指定整個圖的方向,一般不會修改,如果想讓圖“躺下”,就設(shè)置成為"y"
PART3:示例——以ggplot2內(nèi)置數(shù)據(jù)集mpg為例
問題描述:
我們想繪制一個箱型圖,橫軸為每輛車的車型,縱坐標(biāo)為hwy,使用mpg數(shù)據(jù)集,其結(jié)構(gòu)如下:
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àn)方法 y_position = c(40, 47), #指定兩個標(biāo)記在y軸上的坐標(biāo),按照前面指定比較對象的順序 size = 1, #指定標(biāo)記中線條的尺寸 textsize = 5, #指定標(biāo)記中文字部分的大小 vjust = -0.3, #指定標(biāo)記中文字部分與橫線之間的距離 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"))
成圖:
丑是丑了點(diǎn),但還是湊合,畢竟針對主題的修改美化沒有花大力氣。需要注意的幾個點(diǎn):
關(guān)于y軸范圍:
y軸的范圍在這里很重要,范圍不合適會直接導(dǎo)致顯著性標(biāo)記溢出圖外,顯示不全,所以可以用 scale_y_*(limits = numeric.vecter) 或者 ylab() 指定范圍,保證好看的前提下盡可能大一點(diǎn)。
關(guān)于短豎線的長度:
短豎線長度指定的數(shù)值向量中數(shù)值的順序應(yīng)該是與你指定的比較對象的出現(xiàn)順序是一致的,這個參數(shù)可以慢慢一個一個修改,多試試就好了。
PART4:來點(diǎn)花的——何必一定是顯著性標(biāo)記?
前面的函數(shù)幫助文檔里有 annotations 參數(shù),讓人不禁想試試(狗頭)。
話不多說,直接開始:
問題描述:我們想把上面的圖中的 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 參數(shù):
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() #只改了這里
出圖:
以上就是使用ggsignif優(yōu)雅添加顯著性標(biāo)記詳解的詳細(xì)內(nèi)容,更多關(guān)于ggsignif添加顯著性標(biāo)記的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語言 用均值替換、回歸插補(bǔ)及多重插補(bǔ)進(jìn)行插補(bǔ)的操作
這篇文章主要介紹了R語言 用均值替換、回歸插補(bǔ)及多重插補(bǔ)進(jìn)行插補(bǔ)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03R語言編程學(xué)習(xí)從Github上安裝包解決網(wǎng)絡(luò)問題
這篇文章主要為大家介紹了R語言編程從Github上安裝包的過程詳解,這樣可以解決很多網(wǎng)絡(luò)問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11R語言讀取柵格數(shù)據(jù)的方法(raster包讀取)
這篇文章主要介紹了R語言讀取柵格數(shù)據(jù)的方法,本文就只是對R語言raster包讀取、處理柵格數(shù)據(jù)加以基本的方法介紹,需要的朋友可以參考下2023-05-05R語言-生成頻數(shù)表和列聯(lián)表crosstable函數(shù)介紹
這篇文章主要介紹了R語言-生成頻數(shù)表和列聯(lián)表crosstable函數(shù)介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言科學(xué)計(jì)算RcppArmadillo簡明手冊
這篇文章主要為大家介紹了R語言科學(xué)計(jì)算RcppArmadillo的簡明手冊,內(nèi)容非常詳細(xì)全面,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11