重点
heatmap(),heatmap.2(),pheatmap()绘制热图,数据为矩阵
colorRamPalette生成渐变色,并在图中增加颜色条
heatmap(scale=)对图进行标准化,放大各数据的区别
#热图的绘制颜色的深浅表示基因表达量的大小(数据为数值矩阵)
#heatmap包
example(heatmap)#heatmap包用于绘制热图
getwd()
setwd("C:/Users/用户名/Desktop/")
m <- read.csv("Rdata/heatmap.csv",header = T,row.names = 1)#20个样品的基因表达量数据
m
class(m)
x <- as.matrix(m)#数据类型为矩阵
heatmap(x)#直接显示热图
heatmap(t(x))#将样品名与基因名互换位置
heatmap(x,col=c("green","red"))#默认是暖色调
yanse <- colorRampPalette(c("red","black","green"))(nrow(x))#随机生成渐变色,后边的nrow(x)是设置渐变的个数
heatmap(x,col=yanse)
heatmap(x,col=yanse,ColSideColors = colorRampPalette(c("red","black","green"))(ncol(x)))#设置颜色条,会在热图上显示
heatmap(x,col=yanse,Rowv=NA)#取消按行聚类Colv=NA
#若矩阵数值差别过大,可用scale进行中心化和标准化,让图中区别更明显
heatmap(state.x77)
heatmap(state.x77,scale = "col")#按列进行缩放,使图像更清晰
#heatmap.2包
install.packages("gplots")
library(gplots)
example("heatmap.2")#相比于heatmap多了图例和分割线
m <- read.csv("Rdata/heatmap.csv",header = T,row.names = 1)#20个样品的基因表达量数据
m
class(m)
x <- as.matrix(m)
head(x)
heatmap.2(x)
heatmap.2(x,key=F)#是否使用图例
heatmap.2(x,symkey = F)#使用图例是否对称
heatmap.2(x,symkey = T)
heatmap.2(x,symkey = T,density.info = "none")#图例上的刻度
heatmap.2(x,symkey = T,trace = "none")#是否取消图和图例的线
heatmap.2(x,symkey = T,tracecol="blue")#改变线的颜色
#pheatmap包
install.packages("pheatmap")
library(pheatmap)
getwd()
setwd("C:/Users/用户名/Desktop/")
m <- read.csv("Rdata/heatmap.csv",header = T,row.names = 1)#20个样品的基因表达量数据
m
x <- as.matrix(m)
x
pheatmap(x)
colnames(x)
#将20个数据分成五组
annotation_col <- data.frame(CellType=factor(rep(c("N1","T1","N2","T2"),each=5))) #建立一个数据框
annotation_col
rownames(annotation_col)
rownames(annotation_col) <- colnames(x)#将分好的组名与数据名对应
annotation_col
pheatmap(x,annotation_col =annotation_col )#增加分组信息和分组颜色条,会在图中增加分组的颜色条
pheatmap(x,annotation_col =annotation_col,display_numbers = T,number_format = "0.2f%" )#display_numbers在热图上显示数字,number_format设置有效数字保留两位小数
heatmap()结果如下
heatmap.2结果如下
pheatmap结果如下