I want to plot an interactive dendrogram in R where the leaves (labels) are colored according to some rule, and the colours are preserved in an interactive plotly chart.
To produce the dendrogram, I use the following code
library(plotly)
library(ggdendro)
library(dendextend)
bigcars <-mtcars[mtcars$cyl == 6 | mtcars$cyl == 8,] # we will use 6 or 8 cylinder cars only
hc <- hclust(dist(bigcars)) # cluster the data
# set colours by number of cylinders
bigcars$colours <- ifelse(bigcars$cyl == 6, "green", "red")
# create and colour a dendrogram
dend <- as.dendrogram(hc) # create the dendrogram
label_colours <- bigcars[labels(dend), "colours"] # create vector of colours for dendrogram leaves
dend <- set(dend,"labels_col", label_colours) # add color attribute to dendrogram leaves
Plot the dendrogram:
# 1. base plot of dendrogram
plot(dend)
# 2. plotly plot of dendrogram
plot_dendro(dend, height = 600, xmin =-200 ) %>%
hide_legend() %>%
highlight(persistent = FALSE, dynamic = F,
on = "plotly_hover", color = NULL)
The base plot of the dendrogram shows the coloured labels correctly. However, the plotly plot shows the labels dimmed in black; hovering over the plot undikms the labels, but the colors are still black. Setting the color argument to a vector of colours and/or setting dynamic to T doesn’t help either.
How can I create an interactive dendrogram with different coloured labels? Thanks for any help.