在 r 中绘制集群成员
Plot the cluster member in r
我在 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="")
您可以使用 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)
你可以试试这个:
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))
我在 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="")
您可以使用 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)
你可以试试这个:
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))