简体   繁体   English

增加ggplot2树形图中叶子和标签之间的空间

[英]Increase space between leaves and labels in ggplot2 dendrogram

I need to plot clustering dendrogram using ggplot2 . 我需要使用ggplot2绘制聚类树形图。 To create dendrogram object I use as.ggdend() function from the dendextend package. 要创建树形图对象,我使用dendextend包中的as.ggdend()函数。 However, the space between leaves and labels is very small (see the figure below). 但是,树叶和标签之间的空间非常小(请参见下图)。 Any idea how to increase it? 知道如何增加它吗?

在此输入图像描述

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)

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)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM