绘制热图

首先准备数据:


 

gene_id id

TCGA-2J-AAB101A-11R-A41B-07

TCGA-2J-AAB4-01A-12R-A41B-07 TCGA-2J-AAB6-01A-11R-A41B-07 TCGA-2J-AAB8-01A-12R-A41B-07 TCGA-2J-AAB9-01A-11R-A41B-07 TCGA-2J-AABA-01A-21R-A41B-07
APLP2 334 20091.2 23546.3 18321.6 14173.4 18687 18471.9
OLR1 4973 397.561 925.084 766.082 585.664 755.627 177.861
PLXDC1 57125 797.507 600.088 628.012 744.536 849.946 421.425
SEMA3C 10512 965.366 797.98 5651.46 1521.03 2662.38 3890.4

 

用R绘制热图:首先要安装pheatmap包

install.packages("pheatmap")

 然后根据表达量绘制,横轴作为样本(分为两组),纵轴作为基因。

1.导入数据

data <- read.table("gene.txt",header = T)
data_2 <- data[-1,-c(1,2)] #只保留表达值
data_2<-apply(data_2,2, as.numeric) #将其转换为数值型,有时候你的数据可能是经过不同格式转换过来的,需要注意
as.numeric(data_2)
data_3 <- matrix(as.numeric(data_2),nrow = nrow(data_2)) #制作矩阵
rownames(data_3)<-data[-1,]$gene_id  #添加行名基因
colnames(data_3)<-colnames(data_2) #添加列名样本
data_3[data_3 <= 0] = 0.0001
data_3[1:4,1:4]

2. log数据

logData = apply(data_3, 2, log2)

3. 用preprocessCore包做标准化,此步骤在你不需要的时候可以省略

library("preprocessCore")
logData = normalize.quantiles(logData)
rownames(logData) = rownames(data_3)
colnames(logData) = colnames(data_3)
boxplot(logData)

 4. 热图,用heatmap

group<-factor(c(rep('group1',22),rep('group2',22))) #分组
patientcolors = c("green","red")[group] #将两组分别标记为红色和绿色
##logData[logData<= "-lnf"] = -3 
heatmap(logData, col = colorRampPalette ( rev ( c ( "red" , "white" , "green" ) ) ) ( 102 ),Colv=NA, ColSideColors=patientcolors, cexRow=0.5)
#
#

 

 

5. 热图,用pheatmap

library(pheatmap)
annotation = data.frame(Class=group)
rownames(annotation) = colnames(logData) 
pheatmap( logData, annotation=annotation, color = colorRampPalette(c("darkgreen", "gray99", "red"))(500), cellwidth = 10, cellheight = 3,scale="row", cluster_rows = TRUE,
          cluster_cols = FALSE, show_rownames=T, show_colnames=T, fontsize=8, fontsize_col=6,fontsize_row=3, border_color= NA,filename = "test.pdf") 
#
#

 

如果有人要测试,可以用我的数据:

链接: https://pan.baidu.com/s/1bpkoDnp 密码: waxm

具体的修改,大家可以参见其参数设置。

https://cran.r-project.org/web/packages/pheatmap/pheatmap.pdf

posted @ 2017-01-25 13:43  qinqinyang  阅读(1926)  评论(0编辑  收藏  举报