0
votes

I'm essentially trying to change the labels of my dendrogram, to the corresponding row in the table. For instance, consider the USArrest data set.

Right now the labels on the dendrogram are just the states name, "Alabama" and so on. But looking at the data table, the row for alabama looks like this

        Murder Assault UrbanPop Rape
Alabama   13.2     236       58 21.2

So instead of just "Alabama" as a label, I wish to have this full row: Alabama 13.2 236 58 21.2 and so on for all the other labels of the dendrogram.

My code looks like this:

library("ggplot2")
library("ggdendro")
library(dendextend)
data(USArrests)

dend <- USArrests %>%  scale %>% 
dist %>% hclust %>% as.dendrogram
dend %>% plot
dend %>% set("labels_cex", 0.8) %>% # Change size
plot(main = "Dendrogram") # plot

I tried reducing the dendrogram to only two rbanches to try to make it easier on me. So let's say I'm working with dend <- USArrests[1:2,] %>% scale %>%

I tried something like this:

alabama_label <- USArrests[1,]
alaska_label <- USArrests[2,]
dend %>% set("labels", c(alabama_label, alaska_label))

But that did not work. It only gave me the value from the USArrests[1,1] and USArrests[2,1] as label.

So I'm just wondering, how can i possibly label the dendrogram with all the rows?

1

1 Answers

0
votes

There you go:

# library("ggplot2")
## library("ggdendro") ## this package is not relevant for your question. (and in general it is mostly deprecated compared with the features from dendextend)


library(dendextend)
data(USArrests)

dend <- USArrests %>%  scale %>% 
        dist %>% hclust %>% as.dendrogram %>% 
        set("labels_cex", 0.75) # Change size

USArrests_dend_order <- USArrests[order.dendrogram(dend), ]
new_labels <- paste(rownames(USArrests_dend_order), apply(USArrests_dend_order,1,paste,collapse="_"))

dend2 <- dend
labels(dend2) <- new_labels # the labels assignment function is from dendextend


par(mfrow = c(1,2), mar = c(2,0,0,4))
plot(dend, horiz = T)
par(mar = c(2,0,0,10))
plot(dend2, horiz = T)

The output returns a dendrogram with the correct labels, with the stuff you wanted added:

enter image description here