R Biplot以簇为颜色

alb*_*rto 4 r ggplot2 pca

我在PCA转换后进行聚类,我想在PCA空间的前两个或三个维度中可视化聚类的结果,以及从原始轴到投影的PCA的贡献.

我使用的factoextra是使用ggplot 的库,它工作正常,但我想关掉传说:

我的代码:

# Load iris dataset
data(iris)

# PCA
pca <- prcomp(iris[,-5], scale=TRUE)
df.pca <- pca$x

# Cluster over the three first PCA dimensions
kc <- kmeans(df.pca[,1:3], 5)

# 2-D biplot (how to get rid of legend?)
# install.packages("devtools")
# library("devtools")
# install_github("kassambara/factoextra")
library(factoextra)
fviz_pca_biplot(pca, label="var", habillage=as.factor(kc$cluster)) +
  labs(color=NULL) + ggtitle("") +
  theme(text = element_text(size = 15),
      panel.background = element_blank(), 
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      axis.line = element_line(colour = "black"),
      legend.key = element_rect(fill = "white"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何删除右边的图例列?

不使用任何库的等效双标图也是一个受欢迎的解决方案.

PS:

即使在三维中也很难得到一个好的双标图:

library(rgl)
text3d(pca$x[,1:3],texts=rep("*",dim(pca$x)[1]), col=kc$cluster) # points
text3d(1*pca$rotation[,1:3], texts=rownames(pca$rotation), col="red") # arrows title
coords <- NULL
for (i in 1:nrow(pca$rotation)) {
  coords <- rbind(coords, rbind(c(0,0,0),1*pca$rotation[i,1:3]))
}
lines3d(coords, col="blue", lwd=1)
Run Code Online (Sandbox Code Playgroud)

RHe*_*tel 5

我想你应该试试theme(legend.position="none").

 library(factoextra)
 plot(fviz_pca_biplot(pca, label="var", 
  habillage=as.factor(kc$cluster)) + ggtitle("") +
  theme(text = element_text(size = 15), 
      panel.background = element_blank(), 
      panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(), 
      axis.line = element_line(colour = "black"),
      legend.position="none"))
Run Code Online (Sandbox Code Playgroud)

这就是我得到的: 在此输入图像描述