簡體   English   中英

使用 Ape 包根據 R 中定義的組更改樹狀圖“Type = Fan”上的提示標簽

[英]Changing the Tip Labels on a Dendrogram "Type = Fan" Based on a Defined Group in R Using the Ape Package

問題:

我有一個名為Sub_Whistle_Count的數據框(見下文) 我正在嘗試使用hclust()進行分層分析,但我無法自定義正確的標簽名稱Whistle_Type_Sub (海豚產生的 28 種不同的哨子子類型)以出現在樹狀圖中,只有數字標識符(見下文)

 #Data frame structure

'data.frame':   62 obs. of  3 variables:
 $ Country         : Factor w/ 3 levels "Italy","Turkey",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Whistle_Type_Sub: Factor w/ 28 levels "A","AA","AA1",..: 1 24 25 11 2 18 7 5 9 13 ...
 $ N      

     : int  25 64 31 5 4 5 3 10 2 1 ...

我正在嘗試使用行Whistle_Type_SubApe package創建的樹狀圖( type fan )中的標簽,並按Country -N進行聚類。 在這種情況下,我嘗試使用RColorBrewer包和Dark2調色板為使用plot(as.phylo(), type = "fan") plot的系統發育類型圖的邊緣(線)合並colour

#Desired labels

mylabels<- c("A", "E", "EA", "BE", "AA","D", "B", "AD", "BC", "CA", "AA1", "DD1", "ED", "DC", "C", "AC", "ADC", "DE", 
             "EA",  "A", "E", "DE", "F", "BE", "D", "EE1", "B", "CA", "DB",  "BB", "AA1", "ED", "AD",  "DD1", "AA", 
             "A","ED", "E","DD", "DD1","CDC", "C", "AC", "D",  "F", "EE1","BCB", "DC", "ADC", "DE",  "CA", "AA",  "BE",  
             "CBC", "B", "EA",  "AA1", "AD",  "BB",  "CD", "CB",  "DB")

我還嘗試為edges (lines)branchesleaf添加顏色,以突出顯示國家( Country )之間不同的哨子亞型集群。 我想根據它們所在的組(即土耳其、黑山或意大利)為樹狀圖中的“Whistle_Type_Sub”名稱着色,並且當樹狀圖中的分支與給定組相關聯時,邊緣與終端尖端顏色相匹配。

使用tip.color命令根據一組組(Whistle Sub Type and Country)tips (labels)添加顏色似乎不是一個大問題,盡管我不是 100% 確定我已經完成了這部分正確。

但是,當我嘗試為樹狀圖自定義標簽Whistle_Type_Sub時,我無法解決問題,因為命令show.tip.label從字面上需要一個true and false響應(請參閱下面我想要的輸出)。

有人能伸出援手嗎?

任何幫助總是受到贊賞

I tried:

#Attempt 1
#Create labels 
labels <- def(Sub_Whistle_Count$tip.label, Sub_Whistle_Count$Whistle_Type_Sub)
(mylabels<-brewer.pal(3, "Dark2")[labels])
character(0)

#Attempt 2
labels <- def(dend$tip.label, Sub_Whistle_Count$Whistle_Type_Sub)
Warning messages:
1: In get(results[[i]], packages[[i]]) :
  restarting interrupted promise evaluation
2: In get(results[[i]], packages[[i]]) :
  internal error -3 in R_decompress1

R-代碼:

library(data.table)
library(cluster) #agnes function
library(usedist) #change label names
library(ape) #create fan data frame
library('dendextend')
library(RColorBrewer) #Customise the colour palette
library(phytools)

#DENDROGRAM 
Cluster.Country.Dist<-dist(cbind(Sub_Whistle_Count[1, 3], 2*(as.numeric(Sub_Whistle_Count$Whistle_Type_Sub)-2)))
Cluster.Country.Dist

Cluster.Country.hcl<-hclust(Cluster.Country.Dist)
Cluster.Country.hcl

dend = as.dendrogram(Cluster.Country.hcl) 
dend 
#Connecting label color with the country label
geo <- factor(c("Montenegro", "Turkey", "Italy"))
(mycol<-brewer.pal(3, "Dark2")[geo])

#Select the colour of the branch
X <- brewer.pal(3, "Dark2")

#Open a new graphics window
dev.new()

#Plot the dednrogram
plot(as.phylo(dend), type="fan", cex=0.9, label.offset = 0.8,
     edge.color = sample(X, length(Sub_Whistle_Count$Country)/2, replace = TRUE),
     edge.width = sample(2:3, length(Sub_Whistle_Count$Country)/2, replace = TRUE),
     show.tip.label = sample(2:3, length(Sub_Whistle_Count$Whistle_Type_Sub)/2, replace = TRUE), 
     show.node.label = TRUE, 
     tip.color=mycol, lwd=1)

R 代碼的輸出

在此處輸入圖像描述

期望的輸出

在此處輸入圖像描述

數據

structure(list(Country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Italy", "Turkey", 
"Montenegro"), class = "factor"), Whistle_Type_Sub = structure(c(1L, 
24L, 25L, 11L, 2L, 18L, 7L, 5L, 9L, 13L, 3L, 22L, 26L, 20L, 12L, 
4L, 6L, 23L, 25L, 1L, 24L, 23L, 28L, 11L, 18L, 27L, 7L, 13L, 
19L, 8L, 3L, 26L, 5L, 22L, 2L, 1L, 26L, 24L, 21L, 22L, 17L, 12L, 
4L, 18L, 28L, 27L, 10L, 20L, 6L, 23L, 13L, 2L, 11L, 15L, 7L, 
25L, 3L, 5L, 8L, 16L, 14L, 19L), .Label = c("A", "AA", "AA1", 
"AC", "AD", "ADC", "B", "BB", "BC", "BCB", "BE", "C", "CA", "CB", 
"CBC", "CD", "CDC", "D", "DB", "DC", "DD", "DD1", "DE", "E", 
"EA", "ED", "EE1", "F"), class = "factor"), N = c(25L, 64L, 31L, 
5L, 4L, 5L, 3L, 10L, 2L, 1L, 2L, 3L, 2L, 1L, 1L, 3L, 2L, 4L, 
26L, 54L, 20L, 10L, 18L, 7L, 7L, 10L, 2L, 3L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 23L, 1L, 13L, 10L, 5L, 4L, 8L, 9L, 9L, 20L, 1L, 1L, 
9L, 1L, 9L, 2L, 6L, 3L, 1L, 10L, 9L, 2L, 3L, 1L, 2L, 2L, 3L)), row.names = c(NA, 
-62L), class = "data.frame")

代碼

library(data.table)
library(cluster) #agnes function
library(usedist) #change label names
library(ape) #create fan data frame
library('dendextend')
library(RColorBrewer) #Customise the colour palette
library(phytools)

#Create a data frame object
Sub_Whistle_Count<-as.data.frame(Yeo.Whistle.Count_Reorder)
Sub_Whistle_Count

#Check the structure of Yeo.Whistle.Count
str(Sub_Whistle_Count)

#DENDROGRAM 
Cluster.Country.Dist<-dist(cbind(Sub_Whistle_Count[1:2], 2*(as.numeric(Sub_Whistle_Count$Whistle_Type_Sub)-2)))
Cluster.Country.Dist

##tip labels
mylabels<- c("A", "E", "EA", "BE", "AA","D", "B", "AD", "BC", "CA", "AA1", "DD1", "ED", "DC", "C", "AC", "ADC", "DE", 
             "EA",  "A", "E", "DE", "F", "BE", "D", "EE1", "B", "CA", "DB",  "BB", "AA1", "ED", "AD",  "DD1", "AA", 
             "A","ED", "E","DD", "DD1","CDC", "C", "AC", "D",  "F", "EE1","BCB", "DC", "ADC", "DE",  "CA", "AA",  "BE",  
             "CBC", "B", "EA",  "AA1", "AD",  "BB",  "CD", "CB",  "DB")


#Change the rows and column label names in the dist object
NewLabels<-dist_setNames(Cluster.Country.Dist, mylabels[1:62])
NewLabels

#Hierarchical Clustering algorithm on the dataset using hclust() 
Cluster.Country.hcl<-hclust(NewLabels)
Cluster.Country.hcl

#Create a dendrogram object
dend = as.dendrogram(Cluster.Country.hcl) 
dend 

#Connecting label color with the country label
geo <- factor(c("Montenegro", "Turkey", "Italy"))
(mycol<-brewer.pal(3, "Dark2")[geo])

#Select the color of the branch
X <- brewer.pal(3, "Dark2")

#Open a new graphics window
dev.new()

#Plot the dendrogram
plot(as.phylo(dend), type="fan", cex=0.9, label.offset = 0.8,
     edge.color = sample(X, length(Sub_Whistle_Count$Country)/2, replace = TRUE),
     edge.width = sample(1:2, length(Sub_Whistle_Count$Country)/2, replace = TRUE),
     show.tip.label = TRUE, 
     tip.color=mycol, lwd=1)

圖表

在此處輸入圖像描述

暫無
暫無

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

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