Showing variable labels under the segments of dendrogram with ggdendro
Asked Answered
R

1

3

My question is related to Andrie's answer to my earlier question. My question is whether is this possible to display the variable labels and car label under the corresponding segments of the dendrogram?

library(ggplot2)
library(ggdendro)
data(mtcars)
x <- as.matrix(scale(mtcars))
dd.row <- as.dendrogram(hclust(dist(t(x))))
ddata_x <- dendro_data(dd.row)
p2 <- ggplot(segment(ddata_x)) +
geom_segment(aes(x=x0, y=y0, xend=x1, yend=y1))
print(p2)

enter image description here

Redmond answered 26/8, 2011 at 6:55 Comment(2)
If you simplify your code to a minimal example I shall have a look. For example, if your question is how to add labels at the bottom to a dendrogram, then simplify the code to show a single dendrogram.Xuthus
@Xuthus : I've updated the question with minimal code. ThanksRedmond
X
7

Make sure you have version 0.0-7 of ggdendro and then use the convenience function ggdendrogram:

library(ggplot2)
library(ggdendro)
ggdendrogram(dd.row)

enter image description here

If you want full control over how the labels are displayed, you can extract and manipulate these from ddata_x using either:

ddata_x$labels
label(ddata_x)

To add to your plot:

p2 + geom_text(data=label(ddata_x), aes(label=text, x=x, y=0))

You can find more information in the vignette, vignette("ggdendro")

Xuthus answered 27/8, 2011 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.