将点添加到散点图矩阵

时间:2015-04-23 16:18:45

标签: r plot

我想在我的Scatter绘图矩阵中添加点。

head(pca)
        pc1       pc2         pc3       pc4       
[1,]  0.72859559 -2.2864943 -0.5408501  0.1564730  
[2,]  0.34852943  0.3100891  0.6007349 -0.5985266  
[3,] -0.04605026  0.5067896 -0.2911211 -1.1617171  
[4,] -1.88358617  1.3739440 -0.5655383  0.9518367  
[5,]  0.35528650 -1.7482304 -0.3871520 -0.7837712  

我对我的PCA数据运行了kmeans,我可以使用以下内容绘制散点图矩阵:

k <- kmeans(pca,3)

plot(pca, col=k$clust, pch=16)

接下来我想添加X以通过添加

来显示中心
+ points(k$centers, col=1:3 ,pch="X", cex=3)

但它不起作用。如果我减少到只有2台PC,那么下面的中心只绘制2个:

plot(pca[,c(1,2)], col=k$clust, pch=16) + points(k$centers, col=1:10 ,pch="X", cex=3)

当我输入上面的代码时,请注意返回&#34;数字(0)&#34;。这是什么意思?

  > plot(pca[,c(1,2)], col=k$clust, pch=16) + points(k$centers, col=1:10 ,pch="X", cex=3)
  > numeric(0)

以下内容在某种程度上有所帮助。 Kmeans clustering identifying knowledge in R

1 个答案:

答案 0 :(得分:0)

我不确定你的意思是“它不起作用”。这对我来说很好:

# parameters
rows <- 100
cols <- 4
groups <- 3

# create a matrix of uniforms
pca <- matrix(runif(rows*cols),ncol = cols,byrow=TRUE)
colnames(pca) <- paste0('pc',seq(cols))

# calculate the kmeans
(k <- kmeans(pca,groups))

# plot the data and the means
plot(pca, col=k$clust, pch=15)
points(k$centers, col=seq(groups),pch="X", cex=3)

请注意,当矩阵中的列数(cols)为2时,您只会在二维图中获得voronoi曲面细分。

相关问题