使用Kmean创建带有标记的点的

时间:2018-09-11 06:16:12

标签: r pca

我想在mtcars datasetenter image description here上绘制下面给出的图。

为此,我尝试给定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) 

代码可以正常工作直到此处并生成两个图,如下所示: plot1 plot2

使用了绘图下方给出的代码,但绘图中存在问题:

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))

plot4

比例尺不正确,如图所示,它是重叠的,pc1的比例尺已移至上方,而pc2和pc3的轮廓均重叠,如何解决这些问题,以便将pc1,pc2和pc3的比例尺打印在正确的地方?

1 个答案:

答案 0 :(得分:1)

也许您想使用参数aspectadjustcex或使用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)))

enter image description here