簡體   English   中英

如何只聚集R heatmap.2中的列?

[英]How to cluster only the column in R heatmap.2?

我有一個數據,我想繪制一個熱圖,只有列的樹狀圖聚類。 我怎樣才能做到這一點?

數據只包含一行但包含多列。 請注意,我確實希望列上的集群而不是將其轉換為行集群。

這是我的代碼,但沒有用。

library(gplots)
library(RColorBrewer)
dat.all <- structure(list(Probes = structure(1L, .Label = "1419598_at", class = "factor"), 
    XXX_LV_06.ip = 0.985, XXX_SP_06.ip = 0.932, XXX_LN_06.id = 2.115, 
    XXX_LV_06.id = 1.753, XXX_SP_06.id = 2.668, ZZZ_KD_06.ip = 10.079, 
    ZZZ_LG_06.ip = 2.323, ZZZ_LV_06.ip = 2.119, ZZZ_SP_06.ip = 4.157, 
    ZZZ_LN_06.id = 1.371, ZZZ_LV_06.id = 1.825, ZZZ_SP_06.id = 1.457, 
    ZZZ_KD_24.ip = 0L, ZZZ_LG_24.ip = 1.049, ZZZ_LV_24.ip = 1.372, 
    ZZZ_SP_24.ip = 1.83, AAA_LN_06.id = 1.991, AAA_LV_06.ip = 2.555, 
    AAA_SP_06.ip = 4.209, AAA_LV_06.id = 1.375, AAA_SP_06.id = 0.75, 
    GGG_LV_06.ip = 5.938, GGG_SP_06.ip = 8.326, GGG_LN_06.id = 1.982, 
    GGG_LV_06.id = 0.779, GGG_SP_06.id = 1.383, KKK_LN_06.id = 2.006, 
    KKK_LV_06.ip = 1.253, KKK_SP_06.ip = 1.774, X333_LV_06.id = 1.792, 
    X333_SP_06.id = 1.408, EEE_LV_06.in = 0.881, EEE_SP_06.in = 1.374, 
    DDD_LN_06.id = 2.052, DDD_LV_06.id = 1.363, DDD_SP_06.id = 1.678), .Names = c("Probes", 
"XXX_LV_06.ip", "XXX_SP_06.ip", "XXX_LN_06.id", "XXX_LV_06.id", 
"XXX_SP_06.id", "ZZZ_KD_06.ip", "ZZZ_LG_06.ip", "ZZZ_LV_06.ip", 
"ZZZ_SP_06.ip", "ZZZ_LN_06.id", "ZZZ_LV_06.id", "ZZZ_SP_06.id", 
"ZZZ_KD_24.ip", "ZZZ_LG_24.ip", "ZZZ_LV_24.ip", "ZZZ_SP_24.ip", 
"AAA_LN_06.id", "AAA_LV_06.ip", "AAA_SP_06.ip", "AAA_LV_06.id", 
"AAA_SP_06.id", "GGG_LV_06.ip", "GGG_SP_06.ip", "GGG_LN_06.id", 
"GGG_LV_06.id", "GGG_SP_06.id", "KKK_LN_06.id", "KKK_LV_06.ip", 
"KKK_SP_06.ip", "X333_LV_06.id", "X333_SP_06.id", "EEE_LV_06.in", 
"EEE_SP_06.in", "DDD_LN_06.id", "DDD_LV_06.id", "DDD_SP_06.id"
), row.names = 1L, class = "data.frame")



# Clustering and distance function
hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) dist(x,method="maximum")


height <- 3; 

outdir <- "./";

# Define output file name
heatout <-paste(outdir,base,"myplot.pdf",sep="");

# require(RColorBrewer)
col1 <- colorRampPalette(brewer.pal(12, "Set3"));
col2 <- colorRampPalette(brewer.pal(9, "Set1"));


cl.col <- hclustfunc(distfunc(t(dat.all)))


# extract cluster assignments; i.e. k=8 (rows) k=5 (columns)
gr.col <- cutree(cl.col, h=3)
gr.col.nofclust <- length(unique(as.vector(gr.col)));
clust.col.height <- col2(gr.col.nofclust);
hmcols <- rev(redgreen(2750));

pdf(file=heatout,width=50,height=25);
heatmap.2(as.matrix(dat.all),
                scale='row',
                trace='none',
                Rowv=FALSE,
                col=hmcols,
                symbreak=T,
                hclustfun=hclustfunc,
                distfun=distfunc,
                keysize=0.1,
                margins=c(10,200),
                lwid=c(1,4), lhei=c(0.7,3),
                ColSideColors=clust.col.height[gr.col])
dev.off();

圖像看起來像這樣: 在此輸入圖像描述

你明確需要使用heatmap.2()函數嗎? 如果沒有,那么我建議你考慮包pheatmap中的函數pheatmap() ,因為它允許你完成你所追求的壯舉,相當簡單的體操。

首先,我將擺脫數據集中的第一列。 但是,為了保留信息,我將Affymetrix ID作為行名稱放在數據框中:

rownames(dat.all)<-dat.all[,1]
dat.all<-dat.all[,-1]

之后,您可以運行其余代碼,直到實際繪制熱圖。 在那個階段,你求助於pheatmap() 它與heatmap.2()工作方式非常相似,但參數的名稱不同。 以下命令可以讓您完成剩下的工作或接近它:

require(pheatmap)
pheatmap(dat.all, cluster_rows=FALSE, color=hmcols, scale="row",
annotation.colors=clust.col.height[gr.col], annotation=t(dat.all),
clustering_distance_cols=distfunc(t(dat.all)))

名稱中帶有注釋的參數會添加列側顏色。 如果要使用自己的距離函數,可以使用參數clustering_distance_cols將其輸出指定為輸入到pheatmap()輸出。 有關詳細信息,請參閱pheatmap包的幫助。 另外,請參閱下面的示例圖。

pheatmap情節

繞過'每個維度必須是兩個或更多'約束的一點點,你可以將單行rbind到自身,沿着

heatmap.2(rbind(as.numeric(dat.all[,-1]),as.numeric(dat.all[,-1])),...

雖然您可能需要手動調整標簽。 我把第一列從dat.all上取下來(使用[,-1]因為當我復制它時,affymetrix id會妨礙你 - 你可能不需要在真實版本中這樣做嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM