美文网首页
使用R语言绘制热图

使用R语言绘制热图

作者: 路人里的路人 | 来源:发表于2023-08-26 15:46 被阅读0次

加载所需数据

gene_exp <- read.table("D:/bioinfotools/R/R/R-4.3.1/R-4.3.1/wkdir/rna-seq/genes.TMM.EXPR.matrix",
                       header = T, row.names = 1)
#加载表达矩阵
sample_info <- read.delim("D:/share/R_data/data/rnaseq-apple/sample_info.txt", header = T,
                          row.names=1)
#加载样本信息表
library(readr)
library(tidyverse)
gene_info <- read_delim("D:/share/R_data/data/rnaseq-apple/query_seqs.fa.emapper.annotations", 
                                    delim = "\t", escape_double = FALSE, 
                                    col_names = FALSE, comment = "#", trim_ws = TRUE) %>%
  select(Gene_Id = X1,
         Gene_Symbol = X6,
         GO = X7,
         Ko = X9,
         Pathway = X10,
         COG = X21,
         Gene_Name = X22,)
#加载基因信息表

前期数据处理

library(tidyverse)
top_de_exp <- dplyr::slice(de_result, 1:20) %>%
#筛选de_result中差异表达最高的前20个基因(但这不是绝对的,也可以结合火山图中的基因进行筛选),通过管道符传递到下一个函数
  select(-c(2:11)) %>%
#通过select()函数将de_result中的第2至11列进行删除,因为heatmap只组之间的差异信息。然后通过管道符传递到下一个函数
  column_to_rownames(var = 'id')
#将id这一列转化成行名

1 pheatmap基础绘图

library(pheatmap)
pheatmap(top_de_exp)

1.1 取对数优化基础绘图

library(pheatmap)
pheatmap(log10(top_de_exp + 1))
#如果直接对top_de_exp取对数,则会出现报错,因为有的数值为0,所以在top_de_exp基础上+1即可

1.2 标准化优化基础绘图

library(pheatmap)
pheatmap(top_de_exp,
         scale = 'row')
#标准化就是把数据的变化程度归一到一个统一的范围内,中心化就是将平均值都挪到0

2 进阶绘图

cols <- list(
  stage = c(S1 = '#FFA07A', S2 = '#FFB6C1', S3 = '#FFEBCD', S4 = '#CD853F')
)
#list()函数是一个封装函数,可以把不同的数据类型打包到一个向量。如果还需要指定其他图例的颜色,在后面直接添加即可
library(pheatmap)
pheatmap(log10(top_de_exp + 1),
         #cluster_rows = F,
         #不对行名进行聚类
         #cluster_cols = F,
         #不对列名进行聚类
         #show_colnames = F,
         #将列名进行忽略
          angle_col = 45,
         #将行名的倾斜程度设置为45°
         annotation_col = select(sample_info, stage),
         #能够实现的原因在于sample_info中的行名与top_de_exp中的列名能够一一对应
         annotation_colors = cols,
         #对不同阶段的样本上不同的颜色,颜色是在前面用list()函数指定的
         cutree_rows = 3,
         cutree_cols = 2,
         #将行/列分开为指定的行/列数,需要几行/列就为几
         color = colorRampPalette(c("green", "white", "red"))(100)
         )

相关文章

网友评论

      本文标题:使用R语言绘制热图

      本文链接:https://www.haomeiwen.com/subject/zkfimdtx.html