gpt4 book ai didi

r - biplot R 中特定点的文本标签

转载 作者:行者123 更新时间:2023-12-04 08:42:45 25 4
gpt4 key购买 nike

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我想要一个文本标签,用于 Valiantmtcars$brand .
我已经尝试过这种方法,它只给了我想要的点。但我想要相同的情节,但在 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 提取坐标你可以

  • PCA() 的结果中获取它们.
  • 将它们放入数据框中
  • 添加标签
  • 添加 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)

    关于r - biplot R 中特定点的文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64480833/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com