• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

R语言24种R语言作图:boxplot箱线图二

武飞扬头像
Bioinfo Guy
帮助1

一、前言

箱线图一般用于可视化基因的表达情况,常化用统计学方法计算组间基因的表达差异情况。以下主要是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

学新通

自定义修改颜色
scale_color_manual() : 使用自定义颜色
scale_color_brewer() : 使用 RColorBrewer 包中的调色板
scale_color_grey() : 使用灰色调色板

#自定义调色板
p<-ggplot(ToothGrowth, 
          aes(x=dose, y=len, color=dose))  
geom_boxplot()
p scale_color_manual(values=c("#999999", "#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

学新通

修改颜色与上面改线条颜色同理,只不过变成了fill
scale_fill_manual() : 使用自定义颜色
scale_fill_brewer() : 使用 RColorBrewer 包中的调色板
scale_fill_grey() : 使用灰色调色板

p<-ggplot(ToothGrowth, 
          aes(x=dose, y=len, color=dose))  
geom_boxplot()
p scale_fill_manual(values=c("#999999", "#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","#8A919799","#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","#8A919799","#D2AF8199"))
print(p2)
dev.off()
学新通

学新通

四、讨论

箱线图最主要的运用场景还是为了展示某个或者某几个变量的分布,如果讨论分组便介入t检验,非参数秩和检验等统计学方法计算其显著性。以上代码都是导入自己的文件可直接运行的,可关注公主号「生信初学者」回复关键词【boxplot】获取代码和示例数据。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggjfba
系列文章
更多 icon
同类精品
更多 icon
继续加载