大家好,欢迎来到IT知识分享网。
目录
一、前言
箱线图一般用于可视化基因的表达情况,常化用统计学方法计算组间基因的表达差异情况。以下主要是boxplot和geom_boxplot
二、初阶画图
2.1 基础语法
boxplot(x, data, notch, varwidth, names, main) #x:向量或公式 #data:是数据帧 #notch:逻辑值。 设置为TRUE以绘制凹口 #varwidth:一个逻辑值。 设置为true以绘制与样本大小成比例的框的宽度 #names:将打印在每个箱线图下的组标签 #main:用于给图表标题
2.2 简单箱线图
#内置数据集 ToothGrowth$dose <- as.factor(ToothGrowth$dose) head(ToothGrowth) p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() p
#翻转横置 p + coord_flip()
#凹形箱线图 ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(notch=TRUE)
#修改离群值、颜色、形状和大小 ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(outlier.colour="red", outlier.shape=8, outlier.size=4)
2.3 带点箱线图
#初始箱线图 p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() #显示每个值 p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
#发散式散点 p + geom_jitter(shape=16, position=position_jitter(0.2)) # 0.2:X方向上的发散程度
2.4 给线条“上色”
简单上色
#根据dose列分三色 p<-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) + geom_boxplot() p
#自定义调色板 p<-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) + geom_boxplot() p+scale_color_manual(values=c("#", "#E69F00", "#56B4E9"))
#使用brewer调色板 p+scale_color_brewer(palette="Dark2")
2.5 填充“上色”
#使用单色 ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(fill='#A4A4A4', color="black")+ theme_classic()
#分组上色 p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_boxplot() p
p<-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) + geom_boxplot() p+scale_fill_manual(values=c("#", "#E69F00", "#56B4E9"))
#使用brewer调色板 p+scale_fill_brewer(palette="Dark2")
#灰色调色板几乎不使用,不过代码如下大家可以自己运行 #使用黑白色线条 p<-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) + geom_boxplot() p + scale_color_grey() + theme_classic() #使用黑白色填充 p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_boxplot() p + scale_fill_grey() + theme_classic()
2.6 修改图形顺序
p + scale_x_discrete(limits=c("2", "0.5", "1"))
三、进阶画图
3.1 排序箱线图
library(plyr) library(ggpubr) #读取文件 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") #排序 med=ddply(rt,"Type",summarise,med=median(expression)) rt$Type=factor(rt$Type, levels=med[order(med[,"med"],decreasing = T),"Type"]) #绘制 col=rainbow(length(levels(factor(rt$Type)))) pdf(file=outFile, width=10, height=6) #输出图片文件 p=ggboxplot(rt, x="Type", y="expression", color = "Type", palette = col, ylab=y, xlab=x, #add = "jitter", #绘制每个样品的散点 legend = "right") p+rotate_x_text(45) #倾斜角度 dev.off() print(p+rotate_x_text(45))
3.2 差异箱线图
#读取文件 rt=read.table(inputFile,sep="\t",header=T,check.names=F) x=colnames(rt)[2] y=colnames(rt)[3] colnames(rt)=c("id","Group","Expression") #设置分组 group=levels(factor(rt$Group)) rt$Group=factor(rt$Group, levels=group) comp=combn(group,2) my_comparisons=list() for(i in 1:ncol(comp)){
my_comparisons[[i]]<-comp[,i]} #绘制boxplot pdf(file=outFile,width=5,height=4.5) boxplot=ggboxplot(rt, x="Group", y="Expression", color="Group", xlab=x, ylab="TP53 Expression", legend.title=x, palette = c(colornormal,colortumor), add = "jitter")+ stat_compare_means(comparisons = my_comparisons,method = "wilcox.test") #默认是wilcox.test,可换成t.test,kruskal.test,anova,下面同理 print(boxplot) dev.off()
3.3 多基因差异箱线图
#读取文件 rt=read.table(inputFile,sep="\t",header=T,check.names=F,row.names=1) x=colnames(rt)[1] colnames(rt)[1]="Type" #整理表格 data=melt(rt,id.vars=c("Type")) colnames(data)=c("Type","Gene","Expression") #绘制 pdf(file=outFile, width=6, height=5) p=ggboxplot(data, x="Gene", y="Expression", color = "Type", ylab="Gene expression", xlab="", legend.title=x, palette = c(colornormal,colortumor), width=0.6, add = "none") p=p+rotate_x_text(60) #标显著星号 p1=p+stat_compare_means(aes(group=Type), method="wilcox.test", symnum.args=list(cutpoints = c(0, 0.001, 0.01, 0.05, 1), symbols = c("*", "", "*", " ")), label = "p.signif") print(p1) dev.off()
3.4 多组差异箱线图
#读取文件 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]} #绘制 pdf(file=outFile, width=5.5, height=5) boxplot=ggboxplot(rt, x="Type", y="Expression", color="Type", xlab=x, ylab=y, legend.title=x, add = "jitter")+ scale_colour_manual(values = c("#FED43999","#709AE199","#8A","#D2AF8199"))+ stat_compare_means(comparisons = my_comparisons) print(boxplot) dev.off()
3.5 多基因多组差异箱线图
#读取文件 rt=read.table(inputFile, header=T,sep="\t",check.names=F,row.names=1) x=colnames(rt)[1] colnames(rt)[1]="Type" #差异分析 geneSig=c("") for(gene in colnames(rt)[2:ncol(rt)]){
rt1=rt[,c(gene,"Type")] colnames(rt1)=c("expression","Type") p=1 if(length(levels(factor(rt1$Type)))>2){
test=kruskal.test(expression ~ Type, data = rt1) p=test$p.value }else{
test=wilcox.test(expression ~ Type, data = rt1) p=test$p.value } Sig=ifelse(p<0.001,"*",ifelse(p<0.01,"",ifelse(p<0.05,"*",""))) geneSig=c(geneSig,Sig) } colnames(rt)=paste0(colnames(rt),geneSig) #整理表格 data=melt(rt,id.vars=c("Type")) colnames(data)=c("Type","Gene","Expression") #绘制 pdf(file=outFile, width=9, height=5) p1=ggplot(data,aes(x=Type,y=Expression,fill=Type))+ guides(fill=guide_legend(title=x))+ labs(x = x, y = "Gene expression")+ geom_boxplot()+ facet_wrap(~Gene,nrow =1)+ #theme_bw()+ theme(axis.text.x = element_text(angle = 45, hjust = 1)) p2=p1+scale_fill_manual(values=c("#FED43999","#709AE199","#8A","#D2AF8199")) print(p2) dev.off()
四、讨论
箱线图最主要的运用场景还是为了展示某个或者某几个变量的分布,如果讨论分组便介入t检验,非参数秩和检验等统计学方法计算其显著性。以上代码都是导入自己的文件可直接运行的,可关注公主号「生信初学者」回复关键词【boxplot】获取代码和示例数据。
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/126719.html