R语言 Pheatmap 画非聚类 热图

python pandas 加 R Pheatmap 画非聚类热图

最近需求一个需求图
f9cb530a4fb553c2f42fd8f157cd451.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处理过的一组数据


image.png

data4


image.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")

图呢不上传了下面贴一张类似的图


Rplot.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)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Python教程 Python 教程Python 简介Python 环境搭建Python 中文编码Python 基...
    茶茶点阅读 4,705评论 0 5
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    小迈克阅读 8,133评论 1 3
  • 常用库: fuzzywuzzy,字符串模糊匹配。 esmre,正则表达式的加速器。 Chardet字符编码探测器,...
    AlastairYuan阅读 6,758评论 0 43
  • 数据分析入门 数据分析入门常见问题汇总 Python初学者们 - Anaconda入门使用指南 Python学习利...
    amoyyean阅读 11,253评论 7 61
  • 秦墨从梦中惊醒,翻身下床。穿好衣服后就直奔以前读书的初中。好不容易和门卫说好了就进去看看当年读书时的教室,到...
    贺微来阅读 1,222评论 0 0