4

I need to plot clustering dendrogram using ggplot2. To create dendrogram object I use as.ggdend() function from the dendextend package. However, the space between leaves and labels is very small (see the figure below). Any idea how to increase it?

enter image description here

The code to reproduce the example is pasted below.

library(ggplot2)
library(dendextend)

## Sample 20 instances of iris dataset
data(iris)
df   <- iris[sample(150, 20), -5]
labs <- paste("Longname_", 1:20, sep = "")
rownames(df) <- labs

## Create dendrogram object
dend <- df %>% dist %>%
  hclust %>% as.dendrogram %>%
  set("labels_cex", 1)
ggd1 <- as.ggdend(dend)

## Plot dendrogram
ggplot(ggd1, horiz = TRUE)
Andrej
  • 3,719
  • 11
  • 44
  • 73
  • 2
    As per the [documentation](https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html) of `dendextend` package, there is no implementation of changing the space between the leaves. Please refer to this answer [here](https://stackoverflow.com/questions/26965390/larger-font-and-spacing-between-leaves-in-r-dendrogram) by Tal Galili. I think your best solution here could be `dend <- df %>% dist %>% hclust %>% as.dendrogram %>% set("labels_cex", 1)%>% # hang the leaves (at some height) hang.dendrogram(hang_height = .7)` – mnm Aug 23 '17 at 13:12

1 Answers1

1

In my opinion, working on the width and height of your plot can be a simple and valuable solution for your problem.

library(ggplot2)
library(dendextend)
data(iris)
df   <- iris[sample(150, 20), -5]

## Add blanks before "Longname_"
labs <- paste("  Longname_", 1:20, sep = "")
rownames(df) <- labs

dend <- df %>% dist %>%
  hclust %>% as.dendrogram %>%
  set("labels_cex", 1)
ggd1 <- as.ggdend(dend)

## Resize width and height plotting area
x11(width=10, height=5)
ggplot(ggd1, horiz = TRUE)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58