【问题标题】:R Biplot with clusters as colors以簇为颜色的 R Biplot
【发布时间】:2015-06-17 12:25:34
【问题描述】:

我在 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"))

如何删除右边的图例列?

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

PS:

即使是在 3-D 中也很容易获得好的双标图:

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)

【问题讨论】:

  • 我无法安装factoextra 包进行检查,但您可以在ggtitle("") 之后添加+ guides(shape=FALSE) 吗?
  • 几乎!我需要摆脱两个传说,而不仅仅是一个。
  • 我更新了我的评论作为答案,你能确认它有效吗?

标签: r ggplot2 pca factoextra


【解决方案1】:

我认为你应该试试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"))

这是我得到的:

【讨论】:

  • 完美运行!我以为我已经尝试过那个了。谢谢!
【解决方案2】:

这也应该有效: 在ggtitle("")之后添加+ guides(shape=FALSE, color=FALSE)

library(factoextra)
fviz_pca_biplot(pca, label="var", habillage=as.factor(kc$cluster)) +
  labs(color=NULL) + 
  ggtitle("") + guides(shape=FALSE, color=FALSE)
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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2020-06-04
    • 2021-10-22
    相关资源
    最近更新 更多