ggplot中的Kmean聚类

问题描述 投票:2回答:1

我正在使用K-均值算法。在R中以分隔变量。我想在我能够管理的ggplot女巫中绘制结果,但是在ggplotcluster::clusplot]中结果似乎有所不同

所以我想问我缺少什么:例如,我知道缩放比例不同,但是我想知道Whz在使用clustplot时所有变量都在范围内,而在使用ggplot时不在范围内。

仅仅是因为缩放吗?

所以下面两个结果完全一样吗?

library(cluster)
library(ggfortify)


x <- rbind(matrix(rnorm(2000, sd = 123), ncol = 2),
           matrix(rnorm(2000, mean = 800, sd = 123), ncol = 2))
colnames(x) <- c("x", "y")
x <- data.frame(x)

A <- kmeans(x, centers = 3, nstart = 50, iter.max = 500)
cluster::clusplot(cbind(x$x, x$y), A$cluster, color = T, shade = T)
autoplot(kmeans(x, centers = 3, nstart = 50, iter.max = 500), data = x, frame.type = 'norm')

我正在使用K-均值算法。在R中以分隔变量。我想在我能够管理的ggplot中绘制结果,但是在ggplot和cluster :: clusplot ...] >>>

[对我来说,我使用clusplotggplot得到相同的图。但是对于使用ggplot,必须首先在数据上制作一个PCA,以获得与clustplot相同的图。也许这是您遇到的问题。

在这里,以您的示例,我做了:

x <- rbind(matrix(rnorm(2000, sd = 123), ncol = 2),
           matrix(rnorm(2000, mean = 800, sd = 123), ncol = 2))
colnames(x) <- c("x", "y")
x <- data.frame(x)

A <- kmeans(x, centers = 3, nstart = 50, iter.max = 500)
cluster::clusplot(cbind(x$x, x$y), A$cluster, color = T, shade = T)

pca_x = princomp(x)
x_cluster = data.frame(pca_x$scores,A$cluster)
ggplot(test, aes(x = Comp.1, y = Comp.2, color = as.factor(A.cluster), fill = as.factor(A.cluster))) + geom_point() + 
  stat_ellipse(type = "t",geom = "polygon",alpha = 0.4)

使用clusplot的情节enter image description here

和使用ggplot的那个:enter image description here

希望它可以帮助您找出不同情节的原因

r ggplot2 k-means ggfortify
1个回答
0
投票

[对我来说,我使用clusplotggplot得到相同的图。但是对于使用ggplot,必须首先在数据上制作一个PCA,以获得与clustplot相同的图。也许这是您遇到的问题。

© www.soinside.com 2019 - 2024. All rights reserved.