R绘图基础(10)热图 heatmap

我们在分析了差异表达数据之后,经常要生成一种直观图--热图(heatmap)。这一节就以基因芯片数据为例,示例生成高品质的热图。

比如

R绘图基础(10)热图 heatmap-图片1

钢蓝渐白配色的热图

首先还是从最简单的heatmap开始。

  1. > library(ggplot2)
  2. > library(ALL) #可以使用biocLite("ALL")安装该数据包
  3. > data("ALL")
  4. > library(limma)
  5. > eset<-ALL[,ALL$mol.biol %in% c("BCR/ABL","ALL1/AF4")]
  6. > f<-factor(as.character(eset$mol.biol))
  7. > design<-model.matrix(~f)
  8. > fit<-eBayes(lmFit(eset,design)) #对基因芯片数据进行分析,得到差异表达的数据
  9. > selected <- p.adjust(fit$p.value[, 2]) <0.001
  10. > esetSel <- eset[selected,] #选择其中一部分绘制热图
  11. > dim(esetSel) #从这尺度上看,数目并不多,但也不少。如果基因数过多,可以分两次做图。
  12. Features Samples
  13. 84 47
  14. > library(hgu95av2.db)
  15. > data<-exprs(esetSel)
  16. > probes<-rownames(data)
  17. > symbol<-mget(probes,hgu95av2SYMBOL,ifnotfound=NA)
  18. > symbol<-do.call(rbind,symbol)
  19. > symbol[is.na(symbol[,1]),1]<-rownames(symbol)[is.na(symbol[,1])]
  20. > rownames(data)<-symbol[probes,1] #给每行以基因名替换探针名命名,在绘制热图时直接显示基因名。
  21. > heatmap(data,cexRow=0.5)

R绘图基础(10)热图 heatmap-图片2

使用heatmap函数默认颜色生成的热图

这个图有三个部分,样品分枝树图和基因分枝树图,以及热图本身。之所以对样品进行聚类分析排序,是因为这次的样品本身并没有分组。如果有分组的话,那么可以关闭对样品的聚类分析。对基因进行聚类分析排序,主要是为了色块好看,其实可以选择不排序,或者使用GO聚类分析排序。上面的这种热图,方便简单,效果非常不错。

接下来我们假设样品是分好组的,那么我们想用不同的颜色来把样品组标记出来,那么我们可以使用ColSideColors参数来实现。同时,我们希望变更热图的渐变填充色,可以使用col参数来实现。

  1. > color.map <- function(mol.biol) { if (mol.biol=="ALL1/AF4") "#FF0000" else "#0000FF" }
  2. > patientcolors <- unlist(lapply(esetSel$mol.bio, color.map))
  3. > heatmap(data, col=topo.colors(100), ColSideColors=patientcolors, cexRow=0.5)

R绘图基础(10)热图 heatmap-图片3

使用heatmap函数top.colors填充生成的热图

在heatmap函数中,样品分组只能有一种,如果样品分组有多次分组怎么办?heatmap.plus就是来解决这个问题的。它们的参数都一致,除了ColSideColors和RowSideColors。heatmap使用是一维数组,而heatmap.plus使用的是字符矩阵来设置这两个参数。

  1. > library(heatmap.plus)
  2. > hc<-hclust(dist(t(data)))
  3. > dd.col<-as.dendrogram(hc)
  4. > groups <- cutree(hc,k=5)
  5. > color.map <- function(mol.biol) { if (mol.biol=="ALL1/AF4") 1 else 2 }
  6. > patientcolors <- unlist(lapply(esetSel$mol.bio, color.map))
  7. > col.patientcol<-rbind(groups,patientcolors)
  8. > mode(col.patientcol)<-"character"
  9. > heatmap.plus(data,ColSideColors=t(col.patientcol),cexRow=0.5)

R绘图基础(10)热图 heatmap-图片4

使用heatmap.plus绘制热图

这样绘图的不足是没有热图色key值。gplots中的heatmap.2为我们解决了这个问题。而且它带来了更多的预设填充色。下面就是几个例子。

  1. > library("gplots")
  2. > heatmap.2(data, col=redgreen(75), scale="row", ColSideColors=patientcolors,
  3. + key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)

R绘图基础(10)热图 heatmap-图片5

使用heatmap.2函数,readgreen渐变色填充生成的热图

  1. > heatmap.2(data, col=heat.colors(100), scale="row", ColSideColors=patientcolors,
  2. + key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
  3. > heatmap.2(data, col=terrain.colors(100), scale="row", ColSideColors=patientcolors,
  4. + key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
  5. > heatmap.2(data, col=cm.colors(100), scale="row", ColSideColors=patientcolors,
  6. + key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
  7. > heatmap.2(data, col=redblue(100), scale="row", ColSideColors=patientcolors,
  8. + key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
  9. > heatmap.2(data, col=colorpanel(100,low="white",high="steelblue"), scale="row", ColSideColors=patientcolors,
  10. + key=TRUE, keysize=1, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)

R绘图基础(10)热图 heatmap-图片6

使用heatmap.2函数,heat.colors渐变色填充生成的热图

R绘图基础(10)热图 heatmap-图片7

使用heatmap.2函数,terrain.colors渐变色填充生成的热图

R绘图基础(10)热图 heatmap-图片8

使用heatmap.2函数,cm.colors渐变色填充生成的热图

R绘图基础(10)热图 heatmap-图片9

使用heatmap.2函数,redblue渐变色填充生成的热图

R绘图基础(10)热图 heatmap-图片10

使用heatmap.2函数,colorpanel渐变色填充生成的热图

然而,以上的heatmap以及heatmap.2虽然方便简单,效果也很不错,可以使用colorpanel方便的设置渐变填充色,但是它的布局没有办法改变,生成的效果图显得有点呆板,不简洁。为此这里介绍如何使用ggplot2当中的geom_tile来为基因芯片绘制理想的热图。

  1. > library(ggplot2)
  2. > hc<-hclust(dist(data))
  3. > rowInd<-hc$order
  4. > hc<-hclust(dist(t(data)))
  5. > colInd<-hc$order
  6. > data.m<-data[rowInd,colInd] #聚类分析的作用是为了色块集中,显示效果好。如果本身就对样品有分组,基因有排序,就可以跳过这一步。
  7. > data.m<-apply(data.m,1,rescale) #以行为基准对数据进行变换,使每一行都变成[0,1]之间的数字。变换的方法可以是scale,rescale等等,按照自己的需要来变换。
  8. > data.m<-t(data.m) #变换以后转置了。
  9. > coln<-colnames(data.m)
  10. > rown<-rownames(data.m) #保存样品及基因名称。因为geom_tile会对它们按坐标重排,所以需要使用数字把它们的序列固定下来。
  11. > colnames(data.m)<-1:ncol(data.m)
  12. > rownames(data.m)<-1:nrow(data.m)
  13. > data.m<-melt(data.m) #转换数据成适合geom_tile使用的形式
  14. > head(data.m)
  15. X1 X2 value
  16. 1 1 1 0.1898007
  17. 2 2 1 0.6627467
  18. 3 3 1 0.5417057
  19. 4 4 1 0.4877054
  20. 5 5 1 0.5096474
  21. 6 6 1 0.2626248
  22. > base_size<-12 #设置默认字体大小,依照样品或者基因的多少而微变。
  23. > (p <- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value), #设定横坐标为以前的列,纵坐标为以前的行,填充色为转换后的数据
  24. + colour = "white") + scale_fill_gradient(low = "white", #设定渐变色的低值为白色,变值为钢蓝色。
  25. + high = "steelblue"))
  26. > p + theme_grey(base_size = base_size) + labs(x = "", #设置xlabel及ylabel为空
  27. + y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) + #设置x坐标扩展部分为0,刻度为之前的样品名
  28. + scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts( #设置y坐标扩展部分为0,刻度为之前的基因名
  29. + axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size * #设置坐标字体为基准的0.8倍,贴近坐标对节,x坐标旋转90度,色彩为中灰
  30. + 0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
  31. + size = base_size * 0.8, hjust=1, colour="grey50"))

R绘图基础(10)热图 heatmap-图片11

使用ggplot2中geom_tile函数,钢蓝渐白配色的热图

也可以很轻松的实现传统渐变填充色,红黄渐变。

  1. > (p <- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
  2. + colour = "white") + scale_fill_gradient(low = "yellow",
  3. + high = "red"))
  4. > p + theme_grey(base_size = base_size) + labs(x = "",
  5. + y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
  6. + scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
  7. + axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
  8. + 0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
  9. + size = base_size * 0.8, hjust=1, colour="grey50"))

R绘图基础(10)热图 heatmap-图片12

使用ggplot2中geom_tile函数,红黄渐变填充的热图

使用红绿渐变填充。

  1. > (p <- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
  2. + colour = "white") + scale_fill_gradient(low = "green",
  3. + high = "red"))
  4. > p + theme_grey(base_size = base_size) + labs(x = "",
  5. + y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
  6. + scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
  7. + axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
  8. + 0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
  9. + size = base_size * 0.8, hjust=1, colour="grey50"))

R绘图基础(10)热图 heatmap-图片13

使用ggplot2中geom_tile函数,红绿渐变填充的热图

使用绿白渐变填充。

  1. > (p <- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
  2. + colour = "white") + scale_fill_gradient(low = "seagreen",
  3. + high = "white"))
  4. > p + theme_grey(base_size = base_size) + labs(x = "",
  5. + y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
  6. + scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
  7. + axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
  8. + 0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
  9. + size = base_size * 0.8, hjust=1, colour="grey50"))

R绘图基础(10)热图 heatmap-图片14

使用ggplot2中geom_tile函数,绿白渐变填充的热图

使用棕白渐变填充。

  1. > (p <- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
  2. + colour = "white") + scale_fill_gradient(low = "white",
  3. + high = "sienna4"))
  4. > p + theme_grey(base_size = base_size) + labs(x = "",
  5. + y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
  6. + scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
  7. + axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
  8. + 0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
  9. + size = base_size * 0.8, hjust=1, colour="grey50"))

R绘图基础(10)热图 heatmap-图片15

使用ggplot2中geom_tile函数,棕白渐变填充的热图

使用灰阶填充。

  1. > (p <- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
  2. + colour = "white") + scale_fill_gradient(low = "black",
  3. + high = "gray85"))
  4. > p + theme_grey(base_size = base_size) + labs(x = "",
  5. + y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
  6. + scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
  7. + axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
  8. + 0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
  9. + size = base_size * 0.8, hjust=1, colour="grey50"))

R绘图基础(10)热图 heatmap-图片16

使用ggplot2中geom_tile函数,灰色渐变填充的热图

除了ggplot2,还有lattice也是不错的选择。我只使用一种填充色,生成两个图,以作示例。

  1. > hc<-hclust(dist(data))
  2. > dd.row<-as.dendrogram(hc)
  3. > row.ord<-order.dendrogram(dd.row) #介绍另一种获得排序的办法
  4. > hc<-hclust(dist(t(data)))
  5. > dd.col<-as.dendrogram(hc)
  6. > col.ord<-order.dendrogram(dd.col)
  7. > data.m<-data[row.ord,col.ord]
  8. > library(ggplot2)
  9. > data.m<-apply(data.m,1,rescale) #rescale是ggplot2当中的一个函数
  10. > library(lattice)
  11. > levelplot(data.m,
  12. + aspect = "fill",xlab="",ylab="",
  13. + scales = list(x = list(rot = 90, cex=0.8),y=list(cex=0.5)),
  14. + colorkey = list(space = "left"),col.regions = heat.colors)
  15. > library(latticeExtra)
  16. > levelplot(data.m,
  17. + aspect = "fill",xlab="",ylab="",
  18. + scales = list(x = list(rot = 90, cex=0.5),y=list(cex=0.4)),
  19. + colorkey = list(space = "left"),col.regions = heat.colors,
  20. + legend =
  21. + list(right =
  22. + list(fun = dendrogramGrob, #dendrogramGrob是latticeExtra中绘制树型图的一个函数
  23. + args =
  24. + list(x = dd.row, ord = row.ord,
  25. + side = "right",
  26. + size = 5)),
  27. + top =
  28. + list(fun = dendrogramGrob,
  29. + args =
  30. + list(x = dd.col,
  31. + side = "top",
  32. + type = "triangle")))) #使用三角型构图

R绘图基础(10)热图 heatmap-图片17

使用lattice中的levelplot函数,heat.colors填充绘制热图

 

R绘图基础(10)热图 heatmap-图片18

使用lattice中的levelplot函数,heat.colors填充,dendrogramGrob绘树型,绘制热图

可是可是,绘制一个漂亮的热图这么难么?参数如此之多,设置如此复杂,色彩还需要自己指定。有没有简单到发指的函数呢?有!那就是pheatmap,全称pretty heatmaps.

  1. > library(pheatmap)
  2. > pheatmap(data,fontsize=9, fontsize_row=6) #最简单地直接出图
  3. > pheatmap(data, scale = "row", clustering_distance_row = "correlation", fontsize=9, fontsize_row=6) #改变排序算法
  4. > pheatmap(data, color = colorRampPalette(c("navy", "white", "firebrick3"))(50), fontsize=9, fontsize_row=6) #自定义颜色
  5. > pheatmap(data, cluster_row=FALSE, fontsize=9, fontsize_row=6) #关闭按行排序
  6. > pheatmap(data, legend = FALSE, fontsize=9, fontsize_row=6) #关闭图例
  7. > pheatmap(data, cellwidth = 6, cellheight = 5, fontsize=9, fontsize_row=6) #设定格子的尺寸
  8. > color.map <- function(mol.biol) { if (mol.biol=="ALL1/AF4") 1 else 2 }
  9. > patientcolors <- unlist(lapply(esetSel$mol.bio, color.map))
  10. > hc<-hclust(dist(t(data)))
  11. > dd.col<-as.dendrogram(hc)
  12. > groups <- cutree(hc,k=7)
  13. > annotation<-data.frame(Var1=factor(patientcolors,labels=c("class1","class2")),Var2=groups)
  14. > pheatmap(data, annotation=annotation, fontsize=9, fontsize_row=6) #为样品分组
  15. > Var1 = c("navy", "skyblue")
  16. > Var2 = c("snow", "steelblue")
  17. > names(Var1) = c("class1", "class2")
  18. > ann_colors = list(Var1 = Var1, Var2 = Var2)
  19. > pheatmap(data, annotation=annotation, annotation_colors = ann_colors, fontsize=9, fontsize_row=6) #为分组的样品设定颜色

R绘图基础(10)热图 heatmap-图片19

pheatmap最简单地直接出图

 

R绘图基础(10)热图 heatmap-图片20

pheatmap改变排序算法

 

R绘图基础(10)热图 heatmap-图片21

pheatmap自定义颜色

 

R绘图基础(10)热图 heatmap-图片22

pheatmap关闭按行排序

 

R绘图基础(10)热图 heatmap-图片23

pheatmap关闭图例

 

R绘图基础(10)热图 heatmap-图片24

pheatmap设定格子的尺寸

 

R绘图基础(10)热图 heatmap-图片25

pheatmap为样品分组

 

R绘图基础(10)热图 heatmap-图片26

pheatmap为分组的样品设定颜色

评论  4  访客  4
    • yjf-007 1

      学习了,感谢

      • pl15797898032 0

        很不错

        • 0

          博主,您好,请问ALL是什么数据包啊

          • zhaohuan 0

            为什么plob注册一直提醒验证数字错误呢

          发表评论

          匿名网友

          拖动滑块以完成验证
          加载失败