【问题标题】:Plot the cluster member in r在 r 中绘制集群成员
【发布时间】:2017-06-24 05:26:31
【问题描述】:

我在 R 中使用 DTW 包。 我终于完成了层次聚类。 但我想像下图一样单独绘制时间序列集群。

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="")

我该怎么做? 我的数据在http://blogattach.naver.com/e772fb415a6c6ddafd1370417f96e494346a9725/20170207_141_blogfile/khm2963_1486442387926_THgZRt_csv/confirm.csv?type=attachment

【问题讨论】:

  • 您希望数据中有多少个集群?你也可以把你的数据放在这里吗?链接是中文的,看不懂。
  • 哦,我不知道怎么把数据放到这个帖子里。我将说明人们如何获取数据,或者您会给我您的电子邮件地址吗??
  • 您的时间序列沿列排列,但您正在对行进行聚类。您不需要对列进行聚类吗?
  • 哦,是的,我需要对列进行聚类。当我检查结果时,我觉得有些奇怪。

标签: r plot time-series data-mining hierarchical-clustering


【解决方案1】:

你可以试试这个:

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

【讨论】:

  • 真的谢谢~我有图了!!!你是最好的女人??还是家伙?? ~~我还有一个问题。我将第二行代码 rownames(sc) &lt;- id 更改为 rownames(sc) &lt;- sc$STDR_YM_CD 。好吗??当我使用第一个代码时,我收到了错误消息
  • 谢谢~~我希望很多人看到这个答案,以便分享这个伟大的知识
  • 数据看起来如何,你能把数据放在首位吗?
【解决方案2】:

您可以使用cutree 对数据点进行聚类,并在聚类上使用facet_wrap(来自包ggplot2)来绘制它们。由于我无法获取您的数据,因此我提供了一个来自公开数据的示例。

narrest <- USArrests

# Clustering
hc <- hclust(dist(narrest), "ave")
plot(hc)

# Cut the tree to required number of clusters, here 3
narrest$clusters <- cutree(hc, k = 3)

# use facet_wrap from ggplot to one variable Murder 
d <- ggplot(narrest, aes(y=Murder, x=1:nrow(narrest))) + geom_line()
d + facet_wrap(~ clusters)
print(d)

【讨论】:

  • 嗨~真的谢谢~但我改变了我的文件链接。那你能给我演示一下吗?我认为我的数据有问题..哈哈我不知道 y 值在哪里。
猜你喜欢
  • 2016-11-23
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 2014-06-24
  • 2018-02-24
相关资源
最近更新 更多