Label and color leaf dendrograms (phylogeny) in R using the ape package

Following the previous post ( Label and color sheet of the dendrogram in r ) I have the following question.

My questions are similar to the post mentioned, but I wonder if this can be done with the help of a monkey (for example plot(as.phylo(fit), type="fan", labelCol), because it has more types of phylogeny.

Mentioned questions:

  • How to show group codes in sheet label (instead of sample number)?

  • I want to assign a color to each group of code and color the sheet label according to it (it may happen that they will not be in the same treasure, and I can find additional information)?

And an example code:

sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
groupCodes <- c(rep("A",25), rep("B",25), rep("C",25), rep("D",25))

## make unique rownames (equal rownames are not allowed)
rownames(sample) <- make.unique(groupCodes)

colorCodes <- c(A="red", B="green", C="blue", D="yellow")


## perform clustering
distSamples <- dist(sample)
hc <- hclust(distSamples)

## function to set label color
labelCol <- function(x) {
  if (is.leaf(x)) {
    ## fetch label
    label <- attr(x, "label")
    code <- substr(label, 1, 1)
    ## use the following line to reset the label to one letter code
    # attr(x, "label") <- code
    attr(x, "nodePar") <- list(lab.col=colorCodes[code])
  }
  return(x)
}

## apply labelCol on all nodes of the dendrogram
d <- dendrapply(as.dendrogram(hc), labelCol)

plot(d)
+2
source share
2 answers

?"plot.phylo":

library("ape")
plot(as.phylo(hc), tip.color=colorCodes[substr(rownames(sample), 1, 1)], type="fan")

enter image description here

+1

- circlize_dendrogram, : circlize dendextend. :

install.packages("circlize")
devtools::install_github('talgalili/dendextend')

:

# YOUR CODE
sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
groupCodes <- c(rep("A",25), rep("B",25), rep("C",25), rep("D",25))

## make unique rownames (equal rownames are not allowed)
rownames(sample) <- make.unique(groupCodes)

colorCodes <- c(A="red", B="green", C="blue", D="yellow")


## perform clustering
distSamples <- dist(sample)
hc <- hclust(distSamples)

#--------------
# NEW CODE

dend <- as.dendrogram(hc )
library(dendextend)
labels_colors(dend) <- colorCodes 
# plot(dend)
dend <- color_branches(dend, k=4)

# plot the radial plot
par(mar = rep(0,4))
# circlize_dendrogram(dend, dend_track_height = 0.8) 
circlize_dendrogram(dend) 

:

enter image description here

+2

All Articles