R语言pheatmap包绘制热图:数据处理、分组信息添加及结果保存

1. 数据准备

args <- commandArgs(TRUE)
arg1 <- args[1]  #'/home/lzhang/script/metebolism_heatmap/qv.go.txt'
# arg2 <- as.numeric(args[2]) 

df<-read.table(arg1,header = T,sep = '\t')
# df2<-read.table('/home/lzhang/script/metebolism_heatmap/sample_group.txt',header = T,sep = '\t')
# df<-read.table('/home/lzhang/script/metebolism_heatmap/simple4_dre-metb.csv',header = T,sep = '\t')
# df<-read.table('/home/lzhang/script/metebolism_heatmap/qv.go.txt',header = T,sep = '\t')

#热图数据准备:提取数据的3~8列;
dt<-df[,5:16]
# dt<-df[,3:8]

#分组信息数据框生成;
annotation<-data.frame(Type=as.vector(df[,1]))
groups<-data.frame(Type=as.vector(df2[,1]))
groups_trans<-t(groups,)
groups_trans<-t(as.matrix(groups[-1]))

#数据框的行名获取;
rowname<-as.vector(df[,3])
colname<-as.vector(df2[,2])

#用原表的第二列数据替换两个数据框的行名;
row.names(dt)<-rowname
row.names(annotation)<-rowname
names(groups_trans)<-colname

#初始效果尝试,不对数据做聚类和归一化;
p<-pheatmap(dt,cluster_rows=F,cluster_cols=F)
p

#显示数据;
#fontsize_number设置数字大小,number_color设置数据颜色;
#主要对横轴标签的方向,字体的大小做调整;
p1<-pheatmap(dt,cluster_rows=F,cluster_cols=F,
              display_numbers=T,number_format='%.3f',
              border='white',
              fontsize_number=7,
              fontsize_col = 8,
              fontsize_row = 8,
              angle_col = 90)
p1

2. 保存结果

png(file=paste0(arg1,'.png'),width=800,height=600)
p1
dev.off()

3. 添加注释信息

#注释信息添加;
p2<-pheatmap(dt,cluster_rows=F,cluster_cols=F,
              display_numbers=T,number_format='%.3f',
              border='white',
              fontsize_number=7,
              fontsize_col = 8,
              fontsize_row = 8,
              angle_col = 90,
              annotation_col=annotation,
              annotation_colors=c('Type'='white'),
              annotation_legend=T)
p2

#保存结果;
png(file=paste0(arg1,'.with_annotation.png'),width=800,height=600)
p2
dev.off()

4. 对数据进行归一化和聚类处理

#对数据进行归一化和聚类处理;
p3<-pheatmap(dt,cluster_rows=T,cluster_cols=T,
              display_numbers=T,number_format='%.3f',
              border='white',
              fontsize_number=7,
              fontsize_col = 8,
              fontsize_row = 8,
              angle_col = 90,
              annotation_col=annotation,
              annotation_colors=c('Type'='white'),
              annotation_legend=T)
p3

#保存结果;
png(file=paste0(arg1,'.with_annotation_clustered.png'),width=800,height=600)
p3
dev.off()

总结

本文档介绍了使用R语言的pheatmap包绘制热图的详细步骤,包括数据准备、分组信息添加、归一化、聚类处理以及结果保存。通过实际案例演示如何利用pheatmap包生成带注释信息的热图,并对数据进行归一化和聚类处理。希望这篇文章能够帮助您更好地理解和使用pheatmap包,并为您的研究工作提供帮助。

R语言pheatmap包绘制热图:数据处理、分组信息添加及结果保存

原文地址: https://www.cveoy.top/t/topic/na4R 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录