Andrej
Andrej

Reputation: 3839

Increase space between leaves and labels in ggplot2 dendrogram

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)

Upvotes: 4

Views: 2030

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24252

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

Upvotes: 0

Related Questions