pheatmap的基本使用

pheatmap是R语言中绘制热图的一个非常流行的包,通过设置不同的参数能够画出精美的图。因此,本文章介绍一些pheatmap的一些基础用法,以供参考。

library(pheatmap)
# 加载数据
setwd("D:/Desktop")
gene.exp <- read.table("tpm.txt", header = T, row.names = 1)
gene.expr[gene.expr < 1] <- 0    # 将数据中tpm值小于1的替换为0
gene.expr = gene.expr[apply(gene.expr, 1, function(x) sd(x)!=0),]    # 去除标准差为0的行,会对后续聚类造成影响
# 进行绘图
bk <- c(seq(-2,-0.01,by=0.01),seq(0,2,by=0.01))    # 调整图例的数值范围
pheatmap(test,     # 载入数据
         scale="row",     # 对数据按行进行归一化,避免数值过大或过小对热图的影响
         cluster_rows=TRUE,     # 对行进行聚类
         cluster_cols=TRUE,     # 对列进行聚类
         border_color = "white",     # 调整单元格边框颜色
         show_rownames = FALSE,  # 不显示行名
         color = c(colorRampPalette(colors = c("blue","white"))(length(bk)/2),
                   colorRampPalette(colors = c("white","red"))(length(bk)/2)),    # 调整图例的颜色
         legend_breaks=seq(-2,2,1),     # 设置颜色断点
         breaks=bk,    # 调整图例的数值范围
         treeheight_row = 0,    # 去除行的聚类树,即进行聚类但不显示聚类树
         cutree_cols=2)    # 按列对图分割
image.png

我们也可以按照分组进行对热图中的样品进行排序,同时我们也可以选取对特定的基因进行绘图

library(tidyverse)
allmarker <- read.table("Allmarkers.txt", header = T)
class <- read.table("group.txt", header = T, row.names = 1)
selected_rows <- gene[rownames(gene) %in% allmarker$x,]    # 挑选特定基因出来
sort_idx <- order(match(colnames(selected_rows), rownames(class)))
genenewcol <- selected_rows[, sort_idx]    # 按照group中的样品顺序对基因表达matrix中的列进行排序
pheatmap(genenewcol, scale="row", cluster_rows =F, cluster_cols = F, annotation_col = class, show_rownames = F,
         color = c(colorRampPalette(colors = c("blue","white"))(length(bk)/2),
                   colorRampPalette(colors = c("white","red"))(length(bk)/2)),
         legend_breaks=seq(-3,3,1.5), breaks=bk, fontsize_row = 3))
颜色一样的样品排到一起
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容