使用标记有点的 Kmean 创建 3D pca
Create a 3D pca using Kmean with points labelled
我想在 mtcars dataset. 上制作下面给出的情节。
为此,我尝试了 here 给出的代码,如下所示:
require(rgl)
require(SciViews)
require(plotrix)
library(corrplot)
require(ggplot2)
require(reshape)
require("gridExtra")
cars.pca <- pcomp(~mpg+cyl+disp+hp+drat+wt+qsec, data = mtcars)#, subset = -(8:14))
mtcars_pca = cbind(cbind(mtcars, cars.pca$scores), car = rownames(mtcars))
plot(cars.pca, which = "correlations")
plot(cars.pca, which = "scores", cex = 0.8)
代码在此处运行良好,并生成两个图,如下所示:
使用下面给出的代码制作了情节,但情节存在问题:
k <- kmeans(mtcars, 5, nstart=25, iter.max=1000)
new = cbind(mtcars_pca,cluster = k$cluster)
with(new,plot3d(PC1,PC2,PC3, col=k$cluster, size=2, type='s'))
car= = rownames(mtcars)
with(new,text3d(PC1,PC2,PC3,car))
比例不对,如图所示,它是重叠的,pc1 的比例已移到上方,同样 pc2 和 pc3 重叠,如何消除这些问题,以便 pc1、pc2 和 pc3 的比例得到印在正确的地方?
您可能想尝试使用参数 aspect
、adjust
、cex
或使用 jitter
来查找最小化文本重叠的值:
with(new,plot3d(PC1,PC2,PC3, col=k$cluster, size=2, type='s', aspect=c(3,1,3)))
car= rownames(mtcars)
with(new,text3d(jitter(PC1),jitter(PC2),jitter(PC3),car,cex=0.7, adjust=c(0.5,0.9)))
我想在 mtcars dataset.
为此,我尝试了 here 给出的代码,如下所示:
require(rgl)
require(SciViews)
require(plotrix)
library(corrplot)
require(ggplot2)
require(reshape)
require("gridExtra")
cars.pca <- pcomp(~mpg+cyl+disp+hp+drat+wt+qsec, data = mtcars)#, subset = -(8:14))
mtcars_pca = cbind(cbind(mtcars, cars.pca$scores), car = rownames(mtcars))
plot(cars.pca, which = "correlations")
plot(cars.pca, which = "scores", cex = 0.8)
代码在此处运行良好,并生成两个图,如下所示:
使用下面给出的代码制作了情节,但情节存在问题:
k <- kmeans(mtcars, 5, nstart=25, iter.max=1000)
new = cbind(mtcars_pca,cluster = k$cluster)
with(new,plot3d(PC1,PC2,PC3, col=k$cluster, size=2, type='s'))
car= = rownames(mtcars)
with(new,text3d(PC1,PC2,PC3,car))
比例不对,如图所示,它是重叠的,pc1 的比例已移到上方,同样 pc2 和 pc3 重叠,如何消除这些问题,以便 pc1、pc2 和 pc3 的比例得到印在正确的地方?
您可能想尝试使用参数 aspect
、adjust
、cex
或使用 jitter
来查找最小化文本重叠的值:
with(new,plot3d(PC1,PC2,PC3, col=k$cluster, size=2, type='s', aspect=c(3,1,3)))
car= rownames(mtcars)
with(new,text3d(jitter(PC1),jitter(PC2),jitter(PC3),car,cex=0.7, adjust=c(0.5,0.9)))