设为首页 加入收藏

TOP

gplots heatmap.2和ggplot2 geom_tile实现数据聚类和热图plot
2017-10-09 14:23:42 】 浏览:5838
Tags:gplots heatmap.2 ggplot2 geom_tile 实现 数据 类和 plot

主要步骤

ggplot2

  • 数据处理成矩阵形式,给行名列名
  • hclust聚类,改变矩阵行列顺序为聚类后的顺序
  • melt数据,处理成ggplot2能够直接处理的数据结构,并加上列名
  • ggplot_tile进行画图

gplots

  • 数据处理成矩阵形式,给行名列名
  • 调制颜色并用heatmap.2画热图(heatmap.2函数内部用hclustfun 进行聚类)

R语言代码

library(ggplot2) 
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)
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");

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇R语言利器之ddply和aggregate 下一篇R语言 数据导入

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目