df<-USArrests#破坏内部数据集的好主意实验室<-paste("sta _",1:50,sep =")#新标签rownames(df)<-labs#设置新的行名库(ggplot2)图书馆(ggdendro)hc<-hclust(dist(df),"ave")#分层聚类dendr<-dendro_data(hc,type ="rectangle")#转换为ggplotclust<-cutree(hc,k = 2)#找到2个簇clust.df<-data.frame(label = names(clust),cluster = factor(clust))#dendr [["labels"]]具有标签,根据标签列与clust.df合并dendr [["labels"]]<-merge(dendr [["labels"]],clust.df,by ="label")#绘制树状图;注意在geom_text(...)中使用color = clusterggplot()+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),大小= 3)+coord_flip()+scale_y_reverse(expand = c(0.2,0))+主题(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()) 与本热图类似,我如何减小树状图的大小? (来源: I have this code for a dendrogram. How can I decrease the size of dendrogram (or y-axis)?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.df <- USArrests # really bad idea to muck up internal datasetslabs <- paste("sta_",1:50,sep="") # new labelsrownames(df) <- labs # set new row nameslibrary(ggplot2)library(ggdendro)hc <- hclust(dist(df), "ave") # heirarchal clusteringdendr <- dendro_data(hc, type="rectangle") # convert for ggplotclust <- cutree(hc,k=2) # find 2 clustersclust.df <- data.frame(label=names(clust), cluster=factor(clust))# dendr[["labels"]] has the labels, merge with clust.df based on label columndendr[["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)Thanks you so much 解决方案 For flexibility, I recommend putting the dendrogram labels on the x-axis itself, rather than text labels within the plot. 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.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 clustersp + 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") 这篇关于减小树状图(或y轴)ggplot的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-16 03:25