JohnJ
JohnJ

Reputation: 145

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

Upvotes: 0

Views: 1037

Answers (1)

Dave2e
Dave2e

Reputation: 24079

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

Upvotes: 1

Related Questions