SCI常用的箱圖繪制
最近水群,有同學(xué)看文獻(xiàn)中,遇見了一些常用的箱圖,并對(duì)箱圖上添加T檢驗(yàn)標(biāo)簽很感興趣。

tOlUZF.png
簡介
可以仔細(xì)看一下這圖,很多學(xué)生信的作者喜歡用這種箱圖加點(diǎn)圖的方式來展示自己選擇的差hub基因在腫瘤組織中和正常組織中表達(dá)量的差異。因此小編在這里嘗試用一下ggplot去畫一下此類圖形,并標(biāo)注t檢驗(yàn)的p值。
代碼示例
以iris數(shù)據(jù)集為例
#先查看一下iris數(shù)據(jù)的結(jié)構(gòu)
head(iris)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
#2 4.9 3.0 1.4 0.2 setosa
#3 4.7 3.2 1.3 0.2 setosa
#4 4.6 3.1 1.5 0.2 setosa
#5 5.0 3.6 1.4 0.2 setosa
#6 5.4 3.9 1.7 0.4 setosa
#最后一列Species是因子,table一下看看
table(iris$Species)
#setosa versicolor virginica
50 50 50
#選其中倆個(gè)(setosa,versicolor)進(jìn)行畫圖
test=iris[1:100,]
用ggplot畫箱圖
這里用的是ggplot2進(jìn)行畫箱圖
#test是加載數(shù)據(jù),用Species里面的分類做x軸,先嘗試用Sepal.Length的數(shù)據(jù)做Y軸,畫箱圖用geom_boxplot()
ggplot(test, aes(x=Species, y=Sepal.Length)) +
geom_boxplot()
一個(gè)簡陋的箱圖就出來了

tOlJMV.png
因?yàn)橄鋱D的背景不好看,就嘗試讓箱圖根據(jù)分類采用不同顏色,讓圖形更形象
#改變邊框顏色
ggplot(test, aes(x=Species, y=Sepal.Length)) +
geom_boxplot(aes(color=Species))

tOl8x0.png
在箱體里填充顏色
ggplot(test, aes(x=Species, y=Sepal.Length)) +
geom_boxplot(aes(fill=Species))

tOl32q.png
這樣箱圖就畫完了,為了方便保存,給箱圖賦值P
p=ggplot(test, aes(x=Species, y=Sepal.Length)) +
geom_boxplot(aes(fill=Species))
在箱圖基礎(chǔ)上加上散點(diǎn)
利用Sepal.Length里的數(shù)值在箱圖上打上散點(diǎn)
p + geom_jitter(position=position_jitter(0.3))

tOl1Gn.png
這時(shí)候已經(jīng)完成接近百分之八十了,如果有同學(xué)想改變一下散點(diǎn)的type,直接根據(jù)shape改就行。
p + geom_jitter(aes(shape=Species), position=position_jitter(0.3))

tOlYrT.png
還剩最后一步,在這基礎(chǔ)上添加t檢驗(yàn)的P值。還是先將目前畫的圖保存一下賦值為p1.
p1=p + geom_jitter(aes(shape=Species), position=position_jitter(0.3))
畫T檢驗(yàn)的P值
要用到ggpubr并構(gòu)建一個(gè)比較的list進(jìn)行t檢驗(yàn)。
library("ggpubr")
class <- list(c("setosa","versicolor"))
p1 + stat_compare_means(comparisons=class,method="t.test",label="p.signif")
這樣圖就畫出來啦
圖中的ns表示P值沒統(tǒng)計(jì)學(xué)意義,一顆星表示0.01<P值≤0.05,倆顆星表示0.001<P值≤0.01,三顆星表示0.0001<P值≤0.001。
