2017-05-28 37 views
2

我目前正在構建樹狀圖,我正在使用'dendextend'來調整它的外觀。 我已經能夠做到我想做的一切(標記樹葉並突出顯示我選擇的簇的分支),除了圍繞預定義的簇繪製矩形。使用'dendextend'在樹狀圖中指定標籤周圍繪製矩形

我的數據(可以從這個文件中找到:Barra_IBS_example.matrix)與'pvclust'聚集在一起,所以'pvrect'在正確的位置繪製矩形,但它剪切了標籤(見下圖),所以我想用'rect.dendrogram'重現它,然而,我不知道如何告訴函數使用來自'pvclust'的聚類數據。

dendrogram with pvrect

這是我使用的代碼:

idnames <- dimnames(ibs_mat)[[1]] 
ibs.pv <- pvclust(ibs_mat, nboot=1000) 
ibs.clust <- pvpick(ibs.pv, alpha=0.95) 
names(ibs.clust$clusters) <- paste0("Cluster", 1:length(ibs.clust$clusters)) 
# Choose a colour palette 
pal <- brewer.pal(length(ibs.clust$clusters), "Paired") 
# Transform the list to a dataframe 
ibs_meta <- bind_rows(lapply(names(ibs.clust$clusters), 
     function(l) data.frame(Cluster=l, Sample = ibs.clust$clusters[[l]]))) 
# Add the rest of the non-clustered samples (and assign them as Cluster0), add colour to each cluster 
ibs_table <- ibs_meta %>% 
    rbind(., data.frame(Cluster = "Cluster0", 
         Sample = idnames[!idnames %in% .$Sample])) %>% 
    mutate(Cluster_int=as.numeric(sub("Cluster", "", Cluster))) %>% 
    mutate(Cluster_col=ifelse(Cluster_int==0, "#000000", 
       pal[Cluster_int])) %>% 
    .[match(ibs.pv$hclust$labels[ibs.pv$hclust$order], .$Sample),] 
hcd <- as.dendrogram(ibs.pv) %>% 
    #pvclust_show_signif(ibs.pv, show_type = "lwd", signif_value = c(2, 1),alpha=0.25) %>% 
    set("leaves_pch", ifelse(ibs_table$Cluster_int>0,19,18)) %>% # node point type 
    set("leaves_cex", 1) %>% # node point size 
    set("leaves_col", ibs_table$Cluster_col) %>% #node point color 
    branches_attr_by_labels(ibs_meta$Sample, TF_values = c(2, Inf), attr = c("lwd")) %>% # change branch width 
    # rect.dendrogram(k=12, cluster = ibs_table$Cluster_int, border = 8, lty = 5, lwd = 1.5, 
    #     lower_rect = 0) %>% # add rectangles around clusters 
    plot(main="Barramundi samples IBS based clustering") 
pvrect(ibs.pv, alpha=0.95, lwd=1.5) 

非常感謝,伊

+0

請提供最低重複性的數據,我們可以運行你的代碼,例如粘貼'dput(ibs_mat)'或子集 – Djork

+0

@ R.S的輸出。在問題中添加了由'dput(ibs_mat)'創建的示例文件。 – IsoBar

+1

'idnames'函數來自哪裏,請清除您的工作空間並重新運行您的代碼以確保所有內容都包含在上面的代碼中,包括需要安裝的所有庫。 – Djork

回答

2

OK,這個時間比我希望更多的工作,但我得到了一個解決方案您。

我創建了一個名爲pvrect2的新函數,並將它推送到github上的最新版本dendextend。下面是一個自包含的示例演示瞭解決方案:

devtools::install_github('talgalili/dendextend') 

library(pvclust) 
library(dendextend) 
data(lung) # 916 genes for 73 subjects 
set.seed(13134) 
result <- pvclust(lung[, 1:20], method.dist="cor", method.hclust="average", nboot=10) 

par(mar = c(9,2.5,2,0)) 
dend <- as.dendrogram(result) 
dend %>%  
    pvclust_show_signif(result, signif_value = c(3,.5)) %>% 
    pvclust_show_signif(result, signif_value = c("black", "grey"), show_type = "col") %>% 
    plot(main = "Cluster dendrogram with AU/BP values (%)") 
# pvrect(result, alpha=0.95) 
pvrect2(result, alpha=0.95) 
text(result, alpha=0.95) 

[1]: https://i.stack.imgur.com/G UvdV.png

+1

非常好,非常感謝您提供穩定的解決方案,而不僅僅是一次性的解決方法! 高度讚賞。 – IsoBar

+0

我的榮幸:) 我認爲它應該推廣到使用rect.dendrogram,但我會在另一個時間... –

+0

@TalGalili是否有可能維護從pvclust分支註釋的AU/BP值? – Djork