查看集群对象的名称
View the names of clustered objects
我有一个城市数据样本,我正在为某些参数对它们进行聚类。但是我在视觉上表示它们时遇到了麻烦,首先使用了 clusplot,但我不明白为什么比例会发生变化,因为即使只使用 2 个组件和数据范围从 -1 到 1 的绘图,范围也从 -4 到 4 和 - 2 到 2,如您在示例 1.
中所见
[
所以我使用了 hullplot DBSCAN 包,但该图没有在您的输出中显示城市名称,如 clusplot,请参见 2。有人可以给我建议如何将这些名称添加到图表中吗?
我会尝试为此使用 ggplot2 和 ggrepel 包。我借用代码从 this question.
制作凸包
set.seed(175)
library(ggplot2)
library(ggrepel) # Or first install.packages("ggrepel")
# Make the cluster
mtcars$cluster <- as.factor(kmeans(mtcars, 3)$cluster)
# Get the convex hull for the axes you want to plot
hull_df <- plyr::ddply(mtcars, "cluster", function(dta) {
hull <- chull(dta$mpg, dta$disp)
dta[c(hull, hull[1]), ]
})
ggplot(mtcars, aes(mpg, disp, color = cluster, fill = cluster)) +
geom_point() +
geom_polygon(data = hull_df, alpha = 0.5) +
geom_text_repel(aes(label = row.names(mtcars)))
结果:
以下是如何使用 dbscan 执行此操作的一些示例:
library(dbscan)
set.seed(2)
n <- 400
x <- cbind(
x = runif(4, 0, 1) + rnorm(n, sd=0.1),
y = runif(4, 0, 1) + rnorm(n, sd=0.1),
z = runif(4, 0, 1) + rnorm(n, sd=0.1)
)
cl <- rep(1:4, time = 100)
### show some points (first 10) inside the hulls with text
hullplot(x, cl, main = "True clusters", pch = NA)
points(x[1:10,])
text(x[1:10,], labels = paste("Obs.", 1:10), pos = 3)
### look at dimensions x and z
hullplot(x[, c("x", "z")], cl, main = "True clusters")
### use a PCA projection
hullplot(prcomp(x)$x, cl, main = "True clusters")
您可以查看包 wordcloud 以获得更好的文字排版。参见 here.
我有一个城市数据样本,我正在为某些参数对它们进行聚类。但是我在视觉上表示它们时遇到了麻烦,首先使用了 clusplot,但我不明白为什么比例会发生变化,因为即使只使用 2 个组件和数据范围从 -1 到 1 的绘图,范围也从 -4 到 4 和 - 2 到 2,如您在示例 1.
中所见[
所以我使用了 hullplot DBSCAN 包,但该图没有在您的输出中显示城市名称,如 clusplot,请参见 2。有人可以给我建议如何将这些名称添加到图表中吗?
我会尝试为此使用 ggplot2 和 ggrepel 包。我借用代码从 this question.
制作凸包set.seed(175)
library(ggplot2)
library(ggrepel) # Or first install.packages("ggrepel")
# Make the cluster
mtcars$cluster <- as.factor(kmeans(mtcars, 3)$cluster)
# Get the convex hull for the axes you want to plot
hull_df <- plyr::ddply(mtcars, "cluster", function(dta) {
hull <- chull(dta$mpg, dta$disp)
dta[c(hull, hull[1]), ]
})
ggplot(mtcars, aes(mpg, disp, color = cluster, fill = cluster)) +
geom_point() +
geom_polygon(data = hull_df, alpha = 0.5) +
geom_text_repel(aes(label = row.names(mtcars)))
结果:
以下是如何使用 dbscan 执行此操作的一些示例:
library(dbscan)
set.seed(2)
n <- 400
x <- cbind(
x = runif(4, 0, 1) + rnorm(n, sd=0.1),
y = runif(4, 0, 1) + rnorm(n, sd=0.1),
z = runif(4, 0, 1) + rnorm(n, sd=0.1)
)
cl <- rep(1:4, time = 100)
### show some points (first 10) inside the hulls with text
hullplot(x, cl, main = "True clusters", pch = NA)
points(x[1:10,])
text(x[1:10,], labels = paste("Obs.", 1:10), pos = 3)
### look at dimensions x and z
hullplot(x[, c("x", "z")], cl, main = "True clusters")
### use a PCA projection
hullplot(prcomp(x)$x, cl, main = "True clusters")
您可以查看包 wordcloud 以获得更好的文字排版。参见 here.