带标签的R中的水平树状图

带标签的R中的水平树状图,r,dendrogram,hclust,R,Dendrogram,Hclust,我试图从hclust函数输出中绘制树状图。我希望树状图是水平排列的,而不是默认的,可以通过(例如)获得 要在水平树状图中显示定义的标签,一种解决方案是将数据框的行名称设置为新标签(所有标签都应是唯一的) require(图形) 实验室=粘贴(“sta”,1:50,sep=“”)#新标签 USArrests2使用dendrapply您可以自定义您的dendro colLab <- function(n) { if(is.leaf(n)) { a <- attributes(

我试图从
hclust
函数输出中绘制树状图。我希望树状图是水平排列的,而不是默认的,可以通过(例如)获得


要在水平树状图中显示定义的标签,一种解决方案是将数据框的行名称设置为新标签(所有标签都应是唯一的)

require(图形)
实验室=粘贴(“sta”,1:50,sep=“”)#新标签

USArrests2使用
dendrapply
您可以自定义您的dendro

colLab <- function(n) {
  if(is.leaf(n)) {
    a <- attributes(n)
    attr(n, "label") <- substr(a$label,1,2)             #  change the node label 
    attr(n, "nodePar") <- c(a$nodePar, lab.col = 'red') #   change the node color
  }
  n
}

require(graphics)
hc <- hclust(dist(USArrests), "ave")
clusDendro <- as.dendrogram(hc)
clusDendro <- dendrapply(clusDendro, colLab)
op <- par(mar = par("mar") + c(0,0,0,2))
plot(clusDendro,horiz=T)


colLab谢谢,但我仍然不知道如何将用户指定的标签分配给水平树状图?你给出的例子有内置标签,但我真的想传递我自己的标签…请查看上面的更新。很抱歉,我自己的数据示例很难在网上发布,所以我刚刚制作了一个标签向量,我想在水平树状图上显示。再次感谢@alittleboy更新了我的解决方案。此解决方案仅在标签是唯一的情况下有效。要更改标签,
hc$labels我认为当OP说“您给出的示例具有内置标签”时,他的意思是存储到
hc
中的hclust对象已经具有树叶的“标签”(如文档中所述)。此外,如果您使用的是
stringdistmatrix
而不是
dist
,它会用字符串本身来标记每个字符串。是的,我很感谢您的出色回答,并且我对您的帖子投了更高的票。很抱歉,我只能选择一个最终答案……我想知道您的代码示例中
hc.poi
是什么?
 [1] "Al" "Al" "Ar" "Ar" "Ca" "Co" "Co" "De" "Fl" "Ge" "Ha"
[12] "Id" "Il" "In" "Io" "Ka" "Ke" "Lo" "Ma" "Ma" "Ma" "Mi"
[23] "Mi" "Mi" "Mi" "Mo" "Ne" "Ne" "Ne" "Ne" "Ne" "Ne" "No"
[34] "No" "Oh" "Ok" "Or" "Pe" "Rh" "So" "So" "Te" "Te" "Ut"
[45] "Ve" "Vi" "Wa" "We" "Wi" "Wy"
require(graphics)
labs = paste("sta_",1:50,sep="") #new labels
USArrests2<-USArrests #new data frame (just to keep original unchanged)
rownames(USArrests2)<-labs #set new row names
hc <- hclust(dist(USArrests2), "ave")
par(mar=c(3,1,1,5)) 
plot(as.dendrogram(hc),horiz=T)
labs = paste("sta_",1:50,sep="") #new labels
rownames(USArrests)<-labs #set new row names
hc <- hclust(dist(USArrests), "ave")

library(ggplot2)
library(ggdendro)

#convert cluster object to use with ggplot
dendr <- dendro_data(hc, type="rectangle") 

#your own labels (now rownames) are supplied in geom_text() and label=label
ggplot() + 
  geom_segment(data=segment(dendr), aes(x=x, y=y, xend=xend, yend=yend)) + 
  geom_text(data=label(dendr), aes(x=x, y=y, label=label, hjust=0), 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())
colLab <- function(n) {
  if(is.leaf(n)) {
    a <- attributes(n)
    attr(n, "label") <- substr(a$label,1,2)             #  change the node label 
    attr(n, "nodePar") <- c(a$nodePar, lab.col = 'red') #   change the node color
  }
  n
}

require(graphics)
hc <- hclust(dist(USArrests), "ave")
clusDendro <- as.dendrogram(hc)
clusDendro <- dendrapply(clusDendro, colLab)
op <- par(mar = par("mar") + c(0,0,0,2))
plot(clusDendro,horiz=T)