'How to label the leaves of a dendrogram in hclust in R using categorical variables from another column in the same dataframe

I would like to change the default labels for the leaves of a dendrogram to match a categorical variable column in a dataframe generated using dplyr group_by and summarise functions. This is a screenshot of the dataframe. dataframe I would like to use the 'm' column variables as the labels for the dendrogram.

This is the code to generate the dendrogram (sfdf_lop is the dataframe)

csfdf_lop <- hclust(dist(sfdf_lop[, -1]), method = "complete")
plot(csfdf_lop)

and the output looks like this: dendrogram

How do I use the variables in the column 'm' to label the leaves, in place of the default numbered leaves?

Edit Below is the result of using the suggested code

tempdf<- as.data.frame(sfdf_lop)
row.names(tempdf)<- tempdf$m
csfdf_lop <- hclust(dist(tempdf[, -1]), method = "complete")
plot(csfdf_lop)

dendrogram



Solution 1:[1]

If you convert your data to data.frame and define the row.names to equal column M.

tempdf<- as.data.frame(sfdf_lop)
row.names(tempdf)<- tempdf$m

csfdf_lop <- hclust(dist(tempdf[, -1]), method = "complete")
plot(csfdf_lop)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dave2e