双标图 R 中特定点的文本标签

text label for specific point in biplot R

library('factoextra')
data('mtcars')
 pca.cars <- PCA(mtcars)
 gg <- factoextra::fviz_pca_biplot(X = pca.cars, 
                                   # samples
                                   fill.ind = mtcars$vs , col.ind = 'black',
                                   pointshape = 21, pointsize = 1.5,
                                   geom.ind = 'point', repel = T,
                                   geom.var = FALSE )
mtcars$brand <- row.names(mtcars)

在情节 gg 中,我想要在 mtcars$brandValiant 的点上添加文本标签。

我已经尝试过这种方法,它只给了我想要的点。但我想要相同的情节,但在 Valiant

上有一个文本标签
gg$layers[[1]]$data <- dplyr::filter(gg$layers[[1]]$data, name == "Valiant")
gg$layers[[2]]$data <- dplyr::filter(gg$layers[[2]]$data, name == "Valiant")

谢谢!

这可以这样实现。您可以

而不是通过 gg$layers 提取坐标
  1. PCA() 的结果中获取它们。
  2. 将它们放入数据框中
  3. 添加标签
  4. 添加一个 geom_text 图层来标记所需的点
library(factoextra)
library(FactoMineR)
library(dplyr)
library(ggplot2)

pca.cars <- PCA(mtcars, graph = FALSE)
gg <- factoextra::fviz_pca_biplot(X = pca.cars, 
                                  # samples
                                  fill.ind = mtcars$vs , col.ind = 'black',
                                  pointshape = 21, pointsize = 1.5,
                                  geom.ind = 'point', repel = T,
                                  geom.var = FALSE )

# Make df with PC coordinate for each obs
d <- as.data.frame(pca.cars$ind$coord)
d$brand <- row.names(mtcars)

gg +
  geom_text(data = filter(d, brand == "Valiant"), aes(x = Dim.1, y = Dim.2, label = brand), hjust = -.1, vjust =-.1)