ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

Rでクラスター分析~少年サッカー データ分析~

Rでクラスター分析

ggplot で、樹形図を描いてみる。

setwd("C:/data")
d <- read.table("stats_mean.txt", header=T)
d_dist_euclidean <- dist(d, method="Euclidean"  )
d_clust_ward     <- hclust(d_dist_euclidean, "ward.D")
d_dendr          <- dendro_data(d_clust_ward, type="rectangle")

g <- ggplot()
g <- g + geom_segment(
    data=segment(d_dendr), 
    aes(
        x=x, 
        y=y, 
        xend=xend, 
        yend=yend
    )
)
g <- g + geom_text(
    data=label(d_dendr), 
    aes(
        x, 
        y, 
        label=label, 
        hjust=0, 
        color=cluster
    ), 
    size=5
)
g <- g + coord_flip()
g <- g + scale_y_reverse()
g <- g + annotate("rect", xmin=0.5,  xmax=4.4,  ymin=-5, ymax=7, alpha=0.1, color="green", fill="green")
g <- g + annotate("rect", xmin=4.6,  xmax=10.4, ymin=-5, ymax=7, alpha=0.1, color="blue",  fill="blue")
g <- g + annotate("rect", xmin=10.6, xmax=17.4, ymin=-5, ymax=7, alpha=0.1, color="red",   fill="red")

rect.hclust に相当する便利機能を見つけれなかったので、annotate で無理やりやった。後悔はしていない。
f:id:fornext1119:20180511125836p:plain