[英]decrease size of dendogram (or y-axis) ggplot
I have this code for a dendrogram. 我有树状图的这段代码。 How can I decrease the size of dendrogram (or y-axis)? 如何减小树状图(或y轴)的大小?
I am using this code as example. 我使用此代码作为示例。 In my dataset, I have large labels so I do not have space enough to include it. 在我的数据集中,我有大标签,所以我没有足够的空间来包含它。 For that reason, I would like to reduce the space used for y axis, decrease the distance between 0 and 150. Also, when I save the figure as tiff, most of figure is the dendogram and I can not see labels clearly. 因此,我想减少用于y轴的空间,减小0到150之间的距离。而且,当我将图形保存为tiff时,图形的大部分是树状图,我看不清标签。
df <- USArrests # really bad idea to muck up internal datasets
labs <- paste("sta_",1:50,sep="") # new labels
rownames(df) <- labs # set new row names
library(ggplot2)
library(ggdendro)
hc <- hclust(dist(df), "ave") # heirarchal clustering
dendr <- dendro_data(hc, type="rectangle") # convert for ggplot
clust <- cutree(hc,k=2) # find 2 clusters
clust.df <- data.frame(label=names(clust), cluster=factor(clust))
# dendr[["labels"]] has the labels, merge with clust.df based on label column
dendr[["labels"]] <- merge(dendr[["labels"]],clust.df, by="label")
# plot the dendrogram; note use of color=cluster in geom_text(...)
ggplot() +
geom_segment(data=segment(dendr), aes(x=x, y=y, xend=xend, yend=yend)) +
geom_text(data=label(dendr),
aes(x, y, label=label, hjust=0, color=cluster),
size=3) +
coord_flip() +
scale_y_reverse(expand=c(0.2, 0)) +
theme(axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
panel.background=element_rect(fill="white"),
panel.grid=element_blank())
How can I decrease the size of dendogram similar than this heatmap? 与该热图类似,我如何减小树状图的大小?
(source: r-graph-gallery.com ) (来源: r-graph-gallery.com )
Thanks you so much 非常感谢
For flexibility, I recommend putting the dendrogram labels on the x-axis itself, rather than text labels within the plot. 为了灵活起见,我建议将树状图标签放在x轴本身上,而不是在图中添加文本标签。 Otherwise no matter what values you choose for expand
in the y-axis, part of the labels could be cut off for some image sizes / dimensions. 否则,无论您选择在y轴上expand
值是什么,对于某些图像尺寸/尺寸,标签的一部分都可能被剪切掉。
Define colour palette for the dendrogram labels: 为树状图标签定义调色板:
library(dplyr)
label.colour = label(dendr)$cluster %>%
factor(levels = levels(.),
labels = scales::hue_pal()(n_distinct(.))) %>%
as.character()
For the purpose of illustration, make some labels very long: 为了说明的目的,使一些标签很长:
label.values <- forcats::fct_recode(
label(dendr)$label,
sta_45_abcdefghijklmnop = "sta_45",
sta_31_merrychristmas = "sta_31",
sta_6_9876543210 = "sta_6")
Plot: 情节:
p <- ggplot(segment(dendr)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) +
coord_flip() +
scale_x_continuous(breaks = label(dendr)$x,
# I'm using label.values here because I made
# some long labels for illustration. you can
# simply use `labels = label(dendr)$label`
labels = label.values,
position = "top") +
scale_y_reverse(expand = c(0, 0)) +
theme_minimal() +
theme(axis.title = element_blank(),
axis.text.y = element_text(size = rel(0.9),
color = label.colour),
panel.grid = element_blank())
p
# or if you want a color legend for the clusters
p + geom_point(data = label(dendr),
aes(x = x, y = y, color = cluster), alpha = 0) +
scale_color_discrete(name = "Cluster",
guide = guide_legend(override.aes = list(alpha = 1))) +
theme(legend.position = "bottom")
You can do this by adding a size parameter to axis.text.y like so: 您可以通过向axis.text.y添加一个size参数来做到这一点,如下所示:
theme(axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_text(size=12),
axis.title.y=element_blank(),
panel.background=element_rect(fill="white"),
panel.grid=element_blank())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.