数据集的子集作为单独的树形图,但在同一图中

时间:2014-06-18 11:55:02

标签: r plot cluster-analysis dendrogram hclust

我知道我可以按如下方式绘制树形图

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))
}

enter image description here

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

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

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

enter image description here enter image description here

1 个答案:

答案 0 :(得分: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)
            }
}

用你的例子给出:

enter image description here

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

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

enter image description here

最后,如果你想在结果图中连接不同分支之间的空格,你可以使用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))

enter image description here