Good day, I am new to RStudio and I wanted to create a dendrogram with different label color. However, I managed to add the color to the plot but it is not according to the grouping. After the plot was created, the order of the taxa was different based on distance matrix. But the coloring is based on the order of the annotation file (e.g. the first two in the dendrogram were colored as red but they are not the good group). Hope to get some help from you guys. Thanks.
Provided is the annotation file format (tab-delimited) and the code I've been trying to make the plot. FYI, I used a distance matrix to plot the dendrogram.
Annotation file format
library(dendextend) # Read the distance matrix table distMat <- read.table("distMat.txt", header = FALSE, row.names = 1) # Read the annotation file annotation <- read.table("annotation.txt", header = TRUE) # To create dendrogram dm <- as.dist(distMat, diag = TRUE) hc <- hclust(dm, method = "ward.D") dend <- as.dendrogram(hc) # Label the color according to the annotation groups labels_colors(dend) <- annotation_colors[annotation$Groups] # Define a vector for the annotation groups with color annotation_colors <- c("Bad"= "blue", "Good" = "red") # Plot the dendogram png("hc.png", width = 60, height = 20, units = "cm", res = 300) plot(dend) dev.off()