Problems with dendrogram label coloring.

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



# 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)

These two files or representative portions or examples as objects in R will complete the minimum reproducible example and provide better chances of attracting an answer.

Cut and paste dput(distMat) and dput(annotation) is one way to do this.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.