[对我来说,我使用clusplot
或ggplot
得到相同的图。但是对于使用ggplot
,必须首先在数据上制作一个PCA
,以获得与clustplot
相同的图。也许这是您遇到的问题。
我正在使用K-均值算法。在R
中以分隔变量。我想在我能够管理的ggplot
女巫中绘制结果,但是在ggplot
和cluster::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 ...] >>>
[对我来说,我使用clusplot
或ggplot
得到相同的图。但是对于使用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
或ggplot
得到相同的图。但是对于使用ggplot
,必须首先在数据上制作一个PCA
,以获得与clustplot
相同的图。也许这是您遇到的问题。