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

すべてのデンドログラムを同じスケールで同じプロットにまとめてプロットしようとしています。メンバーが 1 人だけのグループもプロットする必要があります。(グループ 6 および 7)

グループ内のメンバーの数が 1 つだけの場合を除いて、データのサブセットに対して個々のデンドログラムをプロットできます。しかし、私はこれが正しいアプローチだとは思いません。

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

ここに画像の説明を入力

ループは、最後の 2 つのグループに対してこのエラーを出しています。(6 と 7) メンバーが 1 つしかありません。

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

基本的に、この種のプロットを再現したくありません。メンバーが 1 つのクラスターもここにプロットされます。

ここに画像の説明を入力 ここに画像の説明を入力

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 に答える