1

Hope you are all doing good.

I use a dendogram:

d <-
  dist(Correlation_Test)
hc <-
  hclust(d, "ave")

plot(hc) 

It works fine and shows at the End the dendogram. Dendogram has different labels. Some of them with the prefix A some with B. I want now to colorize the labels and perhaps branches, based on the label, if its A or B. I found dendextend package. But I didnt manage to automatically colorize the labels based on their prefix.

Can someone help me :)

Example data of Table which is used for Correlation

A Data 1     A Data 1    B Data 1 
0.1666667    0.5         0.6666667
0.6666667    0.6666667   0.6666667
0.5          0.5         0.6666667
Gregor O
  • 9
  • 2

1 Answers1

1

Please find an example with fviz_dend function from factoextra package.

Basic dendrogram

### Initiating data
data(USArrests)

### Initiating reproducing example: distances for dendrogram
dd <- dist(scale(USArrests), method = "euclidean")
hc <- hclust(dd, method="ave")

### Basic dendrogram
plot(hc)

enter image description here

Option where you color labels

### Option where you can either color branches or labels or both (latest option chosen)
# Libraries import
library(dendextend)
library(factoextra)

# Dendrogram with only branches and labels colored
fviz_dend(hc, k=3,
          cex=0.5,
          k_colors=c("#00ffae", "#00c1ff", "#f000ff"),
          color_labels_by_k=TRUE)

enter image description here

Upgraded option

# Dendrogram with both branches and labels and cluster colored
fviz_dend(hc, k=3,
          cex=0.5,
          k_colors=c("#00ffae", "#00c1ff", "#f000ff"),
          color_labels_by_k=TRUE,
          rect=TRUE,
          rect_border=c("#00ffae", "#00c1ff", "#f000ff"),
          rect_fill=TRUE)

enter image description here

Yacine Hajji
  • 1,124
  • 1
  • 3
  • 20