簡體   English   中英

如何根據定義的組為樹形圖的標簽着色? (在R中)

[英]How to color a dendrogram's labels according to defined groups? (in R)

我在R中有一個24行和10,000列的數字矩陣。 該矩陣的行名基本上是文件名,我從中讀取了對應於24行中每一行的數據。 除此之外,我有一個單獨的因子列表,包含24個entires,指定24個文件所屬的組。 有3組 - 醇類,碳氫化合物和酯類。 它們所屬的名稱和相應的組如下所示:

> MS.mz
[1] "int-354.19" "int-361.35" "int-368.35" "int-396.38" "int-408.41" "int-410.43" "int-422.43"
[8] "int-424.42" "int-436.44" "int-438.46" "int-452.00" "int-480.48" "int-648.64" "int-312.14"
[15] "int-676.68" "int-690.62" "int-704.75" "int-312.29" "int-326.09" "int-326.18" "int-326.31"
[22] "int-340.21" "int-340.32" "int-352.35"

> MS.groups
[1] Alcohol     Alcohol     Alcohol     Alcohol     Hydrocarbon Alcohol     Hydrocarbon Alcohol    
[9] Hydrocarbon Alcohol     Alcohol     Alcohol     Ester       Alcohol     Ester       Ester      
[17] Ester       Alcohol     Alcohol     Alcohol     Alcohol     Alcohol     Alcohol     Hydrocarbon
Levels: Alcohol Ester Hydrocarbon

我想生成一個樹形圖來查看矩陣中的數據是如何聚類的。 所以,我使用了以下命令:

require(vegan)
dist.mat<-vegdist(MS.data.scaled.transposed,method="euclidean")
clust.res<-hclust(dist.mat)
plot(clust.res)

我得到了一個樹狀圖。 現在我想根據它們所屬的組(即酒精,碳氫化合物或酯類)為樹形圖中的文件名着色。 我查看了論壇上發布的不同例子

r中的標簽和顏色葉樹形圖

使用猿包的R中的標簽和顏色葉樹形圖

使用自舉進行群集

,但無法為我的數據實現它。 我不確定如何將row.names與MS.groups相關聯以獲得樹形圖中的彩色名稱。

在使用dendextend生成樹時(如https://nycdatascience.com/wp-content/uploads/2013/09/dendextend-tutorial.pdf中所述 ),我得到以下樹

在此輸入圖像描述

以下是用於生成它的代碼:

require(colorspace)
d_SIMS <- dist(firstpointsample5[,-1])
hc_SIMS <- hclust(d_SIMS)
labels(hc_SIMS)
dend_SIMS <- as.dendrogram(hc_SIMS)
SIMS_groups <- rev(levels(firstpointsample5[, 1]))
dend_SIMS <- color_branches(dend_SIMS, k = 3, groupLabels = SIMS_groups)
is.character(labels(dend_SIMS)) 
plot(dend_SIMS)
labels_colors(dend_SIMS) <- rainbow_hcl(3)[sort_levels_values(as.numeric(firstpointsample5[,1])[order.dendrogram(dend_SIMS)])]
labels(dend_SIMS) <- paste(as.character(firstpointsample5[, 1])[order.dendrogram(dend_SIMS)],"(", labels(dend_SIMS), ")", sep = "")
dend_SIMS <- hang.dendrogram(dend_SIMS, hang_height = 0.1)
dend_SIMS <- assign_values_to_leaves_nodePar(dend_SIMS, 0.5,"lab.cex")
par(mar = c(3, 3, 3, 7))
plot(dend_SIMS, main = "Clustered SIMS dataset\n (the labels give the true m/z groups)",horiz = TRUE, nodePar = list(cex = 0.007))
legend("topleft", legend = SIMS_groups, fill = rainbow_hcl(3))

我懷疑你要找的功能是color_labels還是get_leaves_branches_col 基於cutree的標簽的第一種顏色(如color_branches )和第二種顏色允許您獲取每個葉子的分支的顏色,然后使用它為樹的標簽着色(如果您使用不尋常的方法着色分支(就像使用branches_attr_by_labels )。例如:

# define dendrogram object to play with:
hc <- hclust(dist(USArrests[1:5,]), "ave")
dend <- as.dendrogram(hc)

library(dendextend)
par(mfrow = c(1,2), mar = c(5,2,1,0))
dend <- dend %>%
         color_branches(k = 3) %>%
         set("branches_lwd", c(2,1,2)) %>%
         set("branches_lty", c(1,2,1))

plot(dend)

dend <- color_labels(dend, k = 3)
# The same as:
# labels_colors(dend)  <- get_leaves_branches_col(dend)
plot(dend)

在此輸入圖像描述

無論哪種方式,你應該總是看一下set函數,了解你的樹形圖可以做什么(這節省了記住所有不同函數名稱的麻煩)。

您可以查看本教程,該教程顯示了幾種可視化R組中樹狀圖的解決方案

https://rstudio-pubs-static.s3.amazonaws.com/1876_df0bf890dd54461f98719b461d987c3d.html

但是,我認為最適合您數據的解決方案是由'dendextend'軟件包提供的。 請參閱教程(有關'iris'數據集的示例,與您的問題類似): https//nycdatascience.com/wp-content/uploads/2013/09/dendextend-tutorial.pdf

另見插圖: http//cran.r-project.org/web/packages/dendextend/vignettes/Cluster_Analysis.html

你可以嘗試這個解決方案,只用'MS.groups'和'var'改變'labs',你的'MS.groups'轉換為數字(也許,用as.numeric)。 它來自如何通過R中的附加因子變量為樹形圖的標記着色

## The data
df <- structure(list(labs = c("a1", "a2", "a3", "a4", "a5", "a6", "a7", 
"a8", "b1", "b2", "b3", "b4", "b5", "b6", "b7"), var = c(1L, 1L, 2L,     
1L,2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L), td = c(13.1, 14.5, 16.7, 
12.9, 14.9, 15.6, 13.4, 15.3, 12.8, 14.5, 14.7, 13.1, 14.9, 15.6, 14.6), 
fd = c(2L, 3L, 3L, 1L, 2L, 3L, 2L, 3L, 2L, 4L, 2L, 1L, 4L, 3L, 3L)), 
.Names = c("labs", "var", "td", "fd"), class = "data.frame", row.names = 
c(NA, -15L))

## Subset for clustering
df.nw = df[,3:4]

# Assign the labs column to a vector
labs = df$labs

d = dist(as.matrix(df.nw))                          # find distance matrix 
hc = hclust(d, method="complete")                   # apply hierarchical clustering 

## plot the dendrogram

plot(hc, hang=-0.01, cex=0.6, labels=labs, xlab="") 

## convert hclust to dendrogram 
hcd = as.dendrogram(hc)                             

## plot using dendrogram object
plot(hcd, cex=0.6)                                  

Var = df$var                                        # factor variable for colours
varCol = gsub("1","red",Var)                        # convert numbers to colours
varCol = gsub("2","blue",varCol)

# colour-code dendrogram branches by a factor 

# ... your code
colLab <- function(n) {
  if(is.leaf(n)) {
    a <- attributes(n)
    attr(n, "label") <- labs[a$label]
    attr(n, "nodePar") <- c(a$nodePar, lab.col = varCol[a$label]) 
  }
  n
}

## Coloured plot
plot(dendrapply(hcd, colLab))

暫無
暫無

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

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