Hellow,大家好呀!今天帶來《R語言50繪圖》系列第八期~
今天就和大家分享一下:boxplot()展示基因在多組間的差異~
使用ggboxplot()函數來展示基因在多組間的差異。
在做圖時:
一般需要根據及以表達結果提取三列信息:
① Type: 樣本類型
用作——展示在我們的X軸
用作——展示在我們的顏色填充分組
② Expression:Gene的表達量
用作——展示在我們的Y軸
例子:根據基因在多組間的差異表達結果,使用箱線圖進行展示。
library(reshape2)library(ggpubr)inputFile="input.txt" outFile="barplot.pdf"
rt=read.table(inputFile,sep="\t",header=T,check.names=F)x=colnames(rt)[2]y=colnames(rt)[3]colnames(rt)=c("id","Type","Expression")group=levels(factor(rt$Type))rt$Type=factor(rt$Type, levels=group)comp=combn(group,2)my_comparisons=list()for(i in 1:ncol(comp)){my_comparisons[[i]]<-comp[,i]}圖. 比較組設立
注意:這裡設置完比較組後,默認是的兩組間的t-test.
同時:使用ggpubr包添加組間比較的P-value,主要利用兩個函數:
A)compare_means():
可以進行一組或多組間的比較
可以通過??compare_means()查看函數用法哦~
compare_means(formula, data, method = 「wilcox.test」, paired = FALSE, group.by = NULL, ref.group = NULL, …)B)stat_compare_mean():
可以自動添加P-value或顯著性標記
可以通過??stat_compare_mean()查看函數用法哦~
stat_compare_means(mapping = NULL, comparisons = NULL hide.ns = FALSE, label = NULL, label.x = NULL, label.y = NULL, …)boxplot=ggboxplot(rt, x="Type", y="Expression", color="Type", xlab=x, ylab=y, legend.title=x, add = "jitter")+ stat_compare_means(comparisons = my_comparisons)
pdf(file=outFile, width=5.5, height=5)print(boxplot)dev.off()boxplot=ggboxplot(rt, x="Type", y="Expression", color="Type", xlab=x, ylab=y, legend.title=x, add = "jitter")+ stat_compare_means(comparisons = my_comparisons)
pdf(file=outFile, width=5.5, height=5)print(boxplot)dev.off()圖. 繪製箱線圖展示基因在多組間的差異結果帶顯著性差異
今天的分享就到這裡啦~
另外,本公眾號建立了R語言學習分享群,現在長按下方二維碼,即可加入群聊~