Search code examples
rplottime-seriesdata-mininghierarchical-clustering

Plot the cluster member in r


I use DTW package in R. and I finally finished hierarchical clustering. but I wanna plot time-series cluster separately like below picture.

enter image description here

sc <- read.table("D:/handling data/confirm.csv", header=T, sep="," )
rownames(sc) <- sc$STDR_YM_CD
sc$STDR_YM_CD <- NULL
col_n <- colnames(sc)

hc <- hclust(dist(sc), method="average")
plot(hc,  main="")

How can I do it?? My data in http://blogattach.naver.com/e772fb415a6c6ddafd1370417f96e494346a9725/20170207_141_blogfile/khm2963_1486442387926_THgZRt_csv/confirm.csv?type=attachment


Solution

  • You can try this:

    sc <- read.table("confirm.csv", header=T, sep="," )
    rownames(sc) <- sc$STDR_YM_CD 
    sc$STDR_YM_CD <- NULL
    col_n <- colnames(sc)
    
    sc <- t(sc) # make sure your rows represent the time series data
    id <- rownames(sc)
    head(sc)
    
    hc <- hclust(dist(sc), method="average")
    plot(hc,  main="")
    
    n <- 20
    sc <- cbind.data.frame(id=id, sc, cluster=cutree(hc, k = n))
    
    library(dplyr)
    library(tidyr)
    library(ggplot2)
    sc %>% gather(variable, value, -id, -cluster) %>%
    ggplot(aes(variable, value, group=id, color=id)) + geom_line() + 
      facet_wrap(~cluster, scales = 'free') + guides(color=FALSE) + 
      theme(axis.text.x = element_text(angle=90, vjust = 0.5))
    

    enter image description here