1

我知道我可以如下绘制树状图

library(cluster)
d <- mtcars
d[,8:11] <- lapply(d[,8:11], as.factor)

gdist <- daisy(d, metric = c("gower"), stand = FALSE)
dendro <- hclust(gdist, method = "average")
plot(as.dendrogram(dendro))

但是,我确定了一些组(例如,通过迭代分类方法),作为最后一列给出d

G <- c(1,2,3,3,4,4,5,5,5,5,1,2,1,1,2,4,1,3,4,5,1,7,4,3,3,2,1,1,1,3,5,6)
d$Group <- G

head(d)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb  Group
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4     1
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4     2
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1     3
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1     3
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2     4
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1     4

我正在尝试将所有树状图以相同的比例绘制在同一个图上。还需要绘制只有一个成员的组。(第 6 组和第 7 组)

我能够为数据子集绘制单独的树状图,除非组中的成员数量只有一个。但我认为这不是正确的方法。

layout(matrix(1:9, 3,3,byrow=TRUE))

gdist <- as.matrix(gdist)

N <- max(G)
for (i in 1:N){
  rc_tokeep <- row.names(subset(d, G==i))
  dis <- as.dist(gdist[rc_tokeep, rc_tokeep])
  dend <- hclust(dis, method = "average")
  plot(as.dendrogram(dend))
}

在此处输入图像描述

循环为最后两组给出此错误。(6 和 7) 只有一个成员。

Error in hclust(dis, method = "average") : 
  must have n >= 2 objects to cluster

基本上我不想重现这些类型的情节。此处还绘制了具有单个成员的集群。

在此处输入图像描述 在此处输入图像描述

4

1 回答 1

4

如果你想模仿最后几张图,你可以这样做:

N <- max(G)
layout(matrix(c(0,1:N,0),nc=1))

gdist <- as.matrix(gdist)

for (i in 1:N){
    par(mar=c(0,3,0,7))
    rc_tokeep <- row.names(subset(d, G==i))
    if(length(rc_tokeep)>2){ #The idea is to catch the groups with one single element to plot them differently
        dis <- as.dist(gdist[rc_tokeep, rc_tokeep])
        dend <- hclust(dis, method = "average")
        plot(as.dendrogram(dend),horiz=TRUE,
                 xlim=c(.8,0),axes=FALSE) # giving the same xlim will scale all of them, here i used 0.8 to fit your data but you can change it to whatever
        }else{
            plot(NA,xlim=c(.8,0),ylim=c(0,1),axes=F,ann=F)
            segments(0,.5,.1,.5) #I don't know how you intend to compute the length of the branch in a group of 1 element, you might want to change that
            text(0,.5, pos=4,rc_tokeep,xpd=TRUE)
            }
}

通过您的示例,它给出了:

在此处输入图像描述

如果要添加比例,可以在所有图表中添加网格并在最后一个中添加比例:

N <- max(G)
layout(matrix(c(0,1:N,0),nc=1))

gdist <- as.matrix(gdist)

for (i in 1:N){
    par(mar=c(0,3,0,7))
    rc_tokeep <- row.names(subset(d, G==i))
    if(length(rc_tokeep)>2){
        dis <- as.dist(gdist[rc_tokeep, rc_tokeep])
        dend <- hclust(dis, method = "average")
        plot(as.dendrogram(dend),horiz=TRUE,xlim=c(.8,0),xaxt="n",yaxt="n")
        abline(v=seq(0,.8,.1),lty=3) #Here the grid
        }else{
            plot(NA,xlim=c(.8,0),ylim=c(0,1),axes=F,ann=F)
            segments(0,.5,.1,.5)
            text(0,.5, pos=4,rc_tokeep,xpd=TRUE)
            abline(v=seq(0,.8,.1),lty=3) #Here the grid
            }
    }
axis(1,at=seq(0,.8,.1)) #Here the axis

在此处输入图像描述

最后,如果您想平衡结果图中不同分支之间的空间,您可以使用它table(d$Group)来获取每个组的成员数并将其用作每个子图的高度:

N <- max(G)

layout(matrix(c(0,1:7,0),nc=1), height=c(3,table(d$Group),3)) #Plus the height of the empty spaces.

gdist <- as.matrix(gdist)

for (i in 1:N){
    par(mar=c(0,3,0,7))
    rc_tokeep <- row.names(subset(d, G==i))
    if(length(rc_tokeep)>2){
        dis <- as.dist(gdist[rc_tokeep, rc_tokeep])
        dend <- hclust(dis, method = "average")
        plot(as.dendrogram(dend),horiz=TRUE,xlim=c(.8,0),xaxt="n",yaxt="n")
        abline(v=seq(0,.8,.1),lty=3)
        }else{
            plot(NA,xlim=c(.8,0),ylim=c(0,1),axes=F,ann=F)
            segments(0,.5,.1,.5)
            text(0,.5, pos=4,rc_tokeep,xpd=TRUE)
            abline(v=seq(0,.8,.1),lty=3)
            }
    }
axis(1,at=seq(0,.8,.1))

在此处输入图像描述

于 2014-06-19T10:11:00.097 回答