主要步骤
pheatmap
- 数据处理成矩阵形式,给行名列名
- 用pheatmap画热图(pheatmap函数内部用hclustfun 进行聚类)
ggplot2
- 数据处理成矩阵形式,给行名列名
- hclust聚类,改变矩阵行列顺序为聚类后的顺序
- melt数据,处理成ggplot2能够直接处理的数据结构,并加上列名
- ggplot_tile进行画图
gplots
- 数据处理成矩阵形式,给行名列名
- 调制颜色并用heatmap.2画热图(heatmap.2函数内部用hclustfun 进行聚类)
R语言代码
library(pheatmap)
library(data.table)
CN_DT <- fread("/home/ywliao/project/Gengyan/ONCOCNV_result/ONCOCNV_all_result.txt",sep="\t")
dt <- CN_DT[cfDNATime=="cfDNA1"]
wdt <- dcast(dt,Gene~Sample,value.var = "CN",fun.aggregate = mean)
data <- as.matrix(wdt[,2:length(wdt),with=F]) #数据矩阵
rownames(data) <- unlist(wdt[,1])
pheatmap(data)
library(ggplot2)
library(data.table)
CN_DT <- fread("/home/ywliao/project/Gengyan/ONCOCNV_result/ONCOCNV_all_result.txt",sep="\t")
dt <- CN_DT[cfDNATime=="cfDNA1"]
wdt <- dcast(dt,Gene~Sample,value.var = "CN",fun.aggregate = mean)
data <- as.matrix(wdt[,2:length(wdt),with=F]) #数据矩阵
rownames(data) <- unlist(wdt[,1])
hc<-hclust(dist(data),method = "average") #对行进行聚类
rowInd<-hc$order #将聚类后行的顺序存为rowInd
hc<-hclust(dist(t(data)),method = "average") #对矩阵进行转置,对原本的列进行聚类
colInd<-hc$order #将聚类后列的顺序存为colInd
data<-data[rowInd,colInd] #将数据按照聚类结果重排行和列
dp=melt(data) #对数据进行融合,适应ggplot的数据结构,以进行热图的绘制
colnames(dp) <- c("Gene","Sample","Value")
p <- ggplot(dp, aes(Sample,Gene)) + geom_tile(aes(fill = as.factor(Value)))+theme(axis.text.x=element_text(angle = 90))+ guides(fill = guide_legend(title = "Copy Number")) + scale_fill_brewer(palette = 3)
p
library(gplots)
library(data.table)
CN_DT <- fread("/home/ywliao/project/Gengyan/ONCOCNV_result/ONCOCNV_all_result.txt",sep="\t")
dt <- CN_DT[cfDNATime=="cfDNA1"]
wdt <- dcast(dt,Gene~Sample,value.var = "CN",fun.aggregate = mean)
dp <- as.matrix(wdt[,2:length(wdt),with=F]) #数据矩阵
labrow <- unlist(wdt[,1,with=F]) #行名
colorsChoice<- colorRampPalette(c("green","black","red")) #调制颜色
heatmap.2(dp,labRow = labrow,col=colorsChoice(5),breaks = c(1,1.5,2,2.5,3,4),density.info="histogram",
hclustfun = function(c)hclust(c,method="average"),keysize = 1.5, cexRow=0.5,trace = "none");