R语言 Pheatmap 画非聚类 热图
2019-04-02 本文已影响64人
Soliva
python pandas 加 R Pheatmap 画非聚类热图
最近需求一个需求图![](https://img.haomeiwen.com/i15500891/5d693ad43522e630.png)
上面的annotation部分用作临床注释,下面显示Gene 出现的频次。不多说直接上代码。
#Encoding:utf8
library(pheatmap)
file <- "C:/Users/qy/Desktop/data2.csv"
file2 = "C:/Users/qy/Desktop/data4.csv"
首先导入数据文件
数据文件分为两个部分,我是用python pandas 包处理的数据
因为比较快
data2 是一个用pandas onehot处理过的一组数据
![](https://img.haomeiwen.com/i15500891/b8ef0e53fc857025.png)
data4
![](https://img.haomeiwen.com/i15500891/65c45ede1602f42a.png)
data <- read.table(file,header=T,sep=",",row.names = 1)
annotation_col = read.table(file2,header=T,sep=",",row.names = 1)
read.table吧数据处理成dataframe格式
annotation_col 是顶部的annotation 内容,列名为annotation的标签
Treatment.regimen = c("red","blue","green","#4D4D4D","darkgreen")
Primary.tumour.site = c("#ADFF2F","#7A67EE","#575757","#8E388E")
names(Primary.tumour.site) = c("Rectum","Right side colon","Left side colon","Concurrent left and Right side colon")
names(Treatment.regimen) <- c("FOLFOX","FOLFIRI","FOLFOX+Bevacizumab","FOLFOX+Cetuximab","FOLFIRI+Cetuximab")
Age = c("#EEC591", "#EEAD0E","green","#EE0000")
names(Age)=c("<55","55-65","66-75",">76")
因为标签中含有一些空格等特殊字符,如果要修改颜色必须用names 定义 变量 Primary.tumour.site Treatment.regimen
然后定义annotation的colors
ann_colors = list(Age = Age,
Gender = c(F="#A6A6A6",M="#FFFF00"),
Primary.tumour.site = Primary.tumour.site,
Primary.tumour.resected = c(Y="#DEB887",N="#912CEE"),
#Synchronous.metachronous = c(Synchronous="red",Metachronous="#48D1CC",Early.metachronous="#551A8B"),
No..of.metastatic.sites = c("white","#9AFF9A","#7A67EE","green"),
Treatment.regimen = Treatment.regimen)
最后生成图片
pheatmap(data,annotation_col= annotation_col,cellwidth = 25, cellheight = 35,cluster_row = FALSE,color = c("#F2F2F2", "green", "blue", "cyan", "brown"),annotation_colors = ann_colors,fontsize_row=8,fontsize_col=11,show_colnames=T,legend=T,cluster_col = FALSE,legend_labels = c("NA","0.5-20","20-40","40-60","60-80"),legend_breaks=c(0.4,1.2,2,2.8,3.6),filename = "C:/Users/qy/Desktop/HERT.pdf",angle_col = "45")
图呢不上传了下面贴一张类似的图
![](https://img.haomeiwen.com/i15500891/af66114bfa76253c.png)
这是这幅图的代码
这里主要是修改标签名字
用
Var1 <- c("navy", "darkgreen")
names(Var1) <- c("Exp1", "Exp2")
重定义标签颜色
library(pheatmap)
# Generate some data
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
# original figure
pheatmap(test)
# Add annotation as described above, and change the name of annotation
annotation <- data.frame(Var1 = factor(1:10 %% 2 == 0, labels = c("Exp1", "Exp2")))
rownames(annotation) <- colnames(test) # check out the row names of annotation
pheatmap(test, annotation = annotation)
# change the color of annotation to what you want: (eg: "navy", "darkgreen")
Var1 <- c("navy", "darkgreen")
names(Var1) <- c("Exp1", "Exp2")
anno_colors <- list(Var1 = Var1)
pheatmap(test, annotation = annotation, annotation_colors = anno_colors)