简体   繁体   English

在R中旋转树状图x轴标签

[英]rotating dendogram x axis label in R

I am giving the example data provided by hclust help: 我给出了hclust帮助提供的示例数据:

mds2 <- -cmdscale(UScitiesD)
hcity.D <- hclust(UScitiesD, "ward.D")
plot(hcity.D,  hang=-1)

When you plot this the site labels are vertical- which is appropriate for this example since they use long names. 当您绘制此图时,站点标签是垂直的 - 这适用于此示例,因为它们使用长名称。 However my data is labelled simply A1 , A2 , etc. and when I plot the graph it looks unnecessary to have vertical labelling. 然而,我的数据仅标记为A1A2等,当我绘制图形时,看起来没有必要进行垂直标注。

I know that for a vertical dendrogram, las=2 , and that srt = 90 can also rotate the y-axis labels in text() , but this doesn't appear to affect the labels in this hclust plot. 我知道对于垂直树状图, las=2srt = 90也可以在text()旋转y轴标签,但这似乎不会影响此hclust图中的标签。

How can I rotate the site labels to be horizontal for this plot? 如何将站点标签旋转为此绘图的水平?

Thanks 谢谢

You can solve this using the following code, just change srt to whatever you want (notice you'll need the dendextend R package): 您可以使用以下代码解决此问题,只需将srt更改为您想要的任何内容(请注意您需要dendextend R包):

mds2 <- -cmdscale(UScitiesD)
hcity.D <- hclust(UScitiesD, "ward.D")
dend <- as.dendrogram(hcity.D)

# install.packages("dendextend")
library(dendextend)
dend_labels <- labels(dend)
labels(dend) <- ""
plot(dend)
text(x = 1:length(dend_labels), labels = dend_labels, srt = 45, adj = c(1,1), xpd = T)

在此输入图像描述

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

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