R实战之热点图(HeatMap)

快速实现是搜索帮助文档的首要目的,所以此处涉及实战的文章一概略去传统帮助文档的理论部分,直接上代码加注释!

本文将介绍R语言下利用ggplot2包制作heatmap的代码

-------------------------我是分割线-------------------------------

测试数据:

数据中的空白行、列用以分割heatmap,区别不同的数据处理,如不需要删除即可。

1、制作连续型

 1 #引入包
 2 library(ggplot2)
 3 library(reshape)
 4 library(zoo)
 5 
 6 #载入windows下的字体
 7 library(extrafont)
 8 #loadfonts(device="win")
 9 #查看字体库
10 #fonts()
11 
12 #读取数据
13 mydata <- read.csv("G:\\R\\test\\Book3.csv",sep=",",header=TRUE)
14 mydata
15 
16 #数据转换成 ‘rowname, columnname, value’这样形式的数据
17 mydata.m <- melt(mydata)
18 
19 ##设置图形样式
20 theme_change <- theme(
21   #设置xy轴字体样式、大小等 colour="grey50",
22   axis.text.x=element_text(family = "Arial",size=12, colour="black",angle = 0),
23   axis.text.y=element_text(family = "Arial",size=12, colour="black",face="italic"),
24   axis.title.x=element_text(family = "Arial",angle=0, face="plain", size=14),#font face ("plain", "italic", "bold", "bold.italic")
25   axis.title.y=element_text(family = "Arial",angle=270, face="plain", size=14),
26   axis.line = element_blank(),
27   axis.ticks = element_blank(),
28   #设置图例字体样式大小
29   #legend.text
30   #legend.title
31   #legend.position   #the position of legends ("none", "left", "right", "bottom", "top", or two-element numeric vector)
32   #清空背景及xy轴样式
33   plot.background = element_blank(),
34   panel.grid.minor = element_blank(),
35   panel.grid.major = element_blank(),
36   panel.background = element_blank(),
37   panel.border = element_blank()  
38 )
39 
40 ## 输出图形 #Arial
41 p <- ggplot(data = mydata.m, aes(x = variable, y = Name, fill = value)) +
42   geom_tile()+
43   #geom_tile(color="white", size=0.1) + #color="white", size=0.1设置栅格之间的分割线颜色及大小  
44   theme_classic() + #去掉灰快 
45   theme_change +
46   scale_x_discrete(limits=c("BS","X", "BS1","RS"),labels=c("BS","","BS","RS"))+#x轴排序及标注
47   scale_y_discrete(limits=c("B","C","NA1","Meth1", "Meth2","Meth3","Meth4","NA2","S","G","M"),labels=c("B","C","","Meth1", "Meth2","Meth3","Meth4","","S","G","M"))+ #y轴排序及标注
48   xlab('row name1') +
49   ylab('column name') + #更改行名和列名
50   scale_fill_gradient(na.value='#FFFFFF',low = 'green', high = 'red')+ #修改图例名字以及图中颜色
51   #scale_fill_gradient2('legend name', na.value='#FFFFFF',low = '#8856a7', high = '#636363', mid = '#ece7f2')+ #修改图例名字以及图中颜色
52   geom_text(aes(label=round(value,2)), family="Arial", angle=0, colour = "black",size=5)#显示每个栅格的数值及字体设置 
53 p
54 
55 #legend的设置参见http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/

 输出效果图:

2、分级别显示

 1 #引入包
 2 library(ggplot2)
 3 library(reshape)
 4 library(zoo)
 5 
 6 #载入windows下的字体
 7 library(extrafont)
 8 #loadfonts(device="win")
 9 #查看字体库
10 #fonts()
11 
12 #读取数据
13 mydata <- read.csv("G:\\R\\test\\Book3.csv",sep=",",header=TRUE)
14 mydata
15 
16 #数据转换成 ‘rowname, columnname, value’这样形式的数据
17 mydata.m <- melt(mydata)
18 
19 #将现有的value值划分等级
20 quantile_range <- quantile(mydata.m$value, na.rm = TRUE, probs = seq(0, 1, 0.2))
21 
22 #颜色模板
23 color_palette <- colorRampPalette(c("green", "red"))(length(quantile_range) - 1)
24 
25 #图例标签
26 label_text <- rollapply(round(quantile_range, 2), width = 2, by = 1, FUN = function(i) paste(i, collapse = " : "))
27 
28 #将value值用1,2,3,4...等级代替(用以着色)
29 mod_mat <- matrix(findInterval(mydata.m$value, quantile_range, all.inside = TRUE))
30 
31 
32 ##设置图形样式
33 theme_change <- theme(
34   #设置xy轴字体样式、大小等 colour="grey50",
35   axis.text.x=element_text(family = "Arial",size=12, colour="black",angle = 0),
36   axis.text.y=element_text(family = "Arial",size=12, colour="black",face="italic"),
37   axis.title.x=element_text(family = "Arial",angle=0, face="plain", size=14),#font face ("plain", "italic", "bold", "bold.italic")
38   axis.title.y=element_text(family = "Arial",angle=270, face="plain", size=14),
39   axis.line = element_blank(),
40   axis.ticks = element_blank(),
41   #设置图例字体样式大小
42   #legend.text
43   #legend.title
44   #legend.position   #the position of legends ("none", "left", "right", "bottom", "top", or two-element numeric vector)
45   #清空背景及xy轴样式
46   plot.background = element_blank(),
47   panel.grid.minor = element_blank(),
48   panel.grid.major = element_blank(),
49   panel.background = element_blank(),
50   panel.border = element_blank()  
51 )
52 
53 ## 输出图形 #Arial
54 p <- ggplot(data = mydata.m, aes(mydata.m$variable, mydata.m$Name, fill=factor(melt(mod_mat)$value))) +
55   geom_tile()+
56   #geom_tile(color="white", size=0.1) + #color="white", size=0.1设置栅格之间的分割线颜色及大小  
57   theme_classic() + #去掉灰快 
58   theme_change +
59   scale_x_discrete(limits=c("BS","X", "BS1","RS"),labels=c("BS","","BS","RS"))+#x轴排序及标注
60   scale_y_discrete(limits=c("B","C","NA1","Meth1", "Meth2","Meth3","Meth4","NA2","S","G","M"),labels=c("B","C","","Meth1", "Meth2","Meth3","Meth4","","S","G","M"))+ #y轴排序及标注
61   xlab('row name1') +
62   ylab('column name') + #更改行名和列名
63   scale_fill_manual(values = color_palette, name = "Level", labels = label_text) +
64   #scale_fill_gradient(na.value='#FFFFFF',low = 'green', high = 'red')+ #修改图例名字以及图中颜色
65   #scale_fill_gradient2('legend name', na.value='#FFFFFF',low = '#8856a7', high = '#636363', mid = '#ece7f2')+ #修改图例名字以及图中颜色
66   geom_text(aes(label=round(value,2)), family="Arial", angle=0, colour = "black",size=5)#显示每个栅格的数值及字体设置 
67 p
68 
69 #legend的设置参见http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/

输出效果图:

--------------------------------我是分割线------------------------------------

转载请注明出处!

posted @ 2015-07-22 21:13  marost  阅读(8512)  评论(0编辑  收藏  举报