R绘图基础(四)热图 heatmap ← 糗世界_2

2015-04-02 17:52 阅读(?)评论(0)

糗世界的服务器关了,检索到有其他网站保存的有,贴过来学习用。
http://www.360doc.com/content/14/1103/10/17553313_422108323.shtml#



################################################

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

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

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

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

使用红绿渐变填充。

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

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

使用绿白渐变填充。

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

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

使用棕白渐变填充。

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

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

使用灰阶填充。

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

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

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

> hc<-hclust(dist(data))
> dd.row<-as.dendrogram(hc)
> row.ord<-order.dendrogram(dd.row) #介绍另一种获得排序的办法
> hc<-hclust(dist(t(data)))
> dd.col<-as.dendrogram(hc)
> col.ord<-order.dendrogram(dd.col)
> data.m<-data[row.ord,col.ord]
> library(ggplot2)
> data.m<-apply(data.m,1,rescale) #rescale是ggplot2当中的一个函数
> library(lattice)
> levelplot(data.m,
+           aspect = "fill",xlab="",ylab="",
+           scales = list(x = list(rot = 90, cex=0.8),y=list(cex=0.5)),
+           colorkey = list(space = "left"),col.regions = heat.colors)
> library(latticeExtra)
> levelplot(data.m,
+           aspect = "fill",xlab="",ylab="",
+           scales = list(x = list(rot = 90, cex=0.5),y=list(cex=0.4)),
+           colorkey = list(space = "left"),col.regions = heat.colors,
+           legend =
+           list(right =
+                list(fun = dendrogramGrob, #dendrogramGrob是latticeExtra中绘制树型图的一个函数
+                     args =
+                     list(x = dd.row, ord = row.ord,
+                          side = "right",
+                          size = 5)),
+                top =
+                list(fun = dendrogramGrob,
+                     args =
+                     list(x = dd.col, 
+                          side = "top",
+                          type = "triangle")))) #使用三角型构图

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


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

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

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

pheatmap最简单地直接出图


pheatmap改变排序算法


pheatmap自定义颜色


pheatmap关闭按行排序


pheatmap关闭图例


pheatmap设定格子的尺寸


pheatmap为样品分组


pheatmap为分组的样品设定颜色


 
表  情:
加载中...
 

请各位遵纪守法并注意语言文明