使用不同的定性属性自定义 PCA 中点的颜色和形状

问题描述 投票:0回答:1

我对 R 还很陌生 - 我想根据我已经实现的称为“分数”的定性变量对我的点进行颜色编码,但是,同时我还想根据称为“的不同定性变量”来更改点的形状土地利用”

目前我有:

fviz_pca_biplot(SXR_PCA,
                label="var",
                habillage = SXR_input$Fraction,
                pointshape = 19, 
                col.var = "black",
                addEllipses = TRUE, ellipse.type = "norm",
                legend.title = "Fraction",
                mean.point = FALSE,
                ggtheme = theme_classic()) +
  ggtitle("")+
  scale_color_manual(values=c("#782ED1","#D1782E","#2ED178"))

我已经能够根据“分数”变量改变形状,并且能够将所有点更改为相同的形状,但很难找到任何能让我根据不同变量改变形状的东西

r pca factoextra
1个回答
0
投票

我认为你需要添加自己的

geom_point()
才能更改形状,例如

library(factoextra)
#> Loading required package: ggplot2
#> Welcome! Want to learn more? See two factoextra-related books

data(iris)
res.pca <- prcomp(iris[, -5],  scale = TRUE)

# add a new variable
iris$Landuse <- c(rep(paste("var", 1:5, sep = "_"), each = 30))
head(iris)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Landuse
#> 1          5.1         3.5          1.4         0.2  setosa   var_1
#> 2          4.9         3.0          1.4         0.2  setosa   var_1
#> 3          4.7         3.2          1.3         0.2  setosa   var_1
#> 4          4.6         3.1          1.5         0.2  setosa   var_1
#> 5          5.0         3.6          1.4         0.2  setosa   var_1
#> 6          5.4         3.9          1.7         0.4  setosa   var_1
tail(iris)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species Landuse
#> 145          6.7         3.3          5.7         2.5 virginica   var_5
#> 146          6.7         3.0          5.2         2.3 virginica   var_5
#> 147          6.3         2.5          5.0         1.9 virginica   var_5
#> 148          6.5         3.0          5.2         2.0 virginica   var_5
#> 149          6.2         3.4          5.4         2.3 virginica   var_5
#> 150          5.9         3.0          5.1         1.8 virginica   var_5

fviz_pca_biplot(res.pca,
                label="var",
                habillage = iris$Species,
                pointshape = NA, 
                col.var = "black",
                addEllipses = TRUE, ellipse.type = "norm",
                legend.title = "Fraction",
                mean.point = FALSE,
                ggtheme = theme_classic()) +
  ggtitle("")+
  geom_point(aes(shape = iris$Landuse)) +
  scale_color_manual(values=c("#782ED1","#D1782E","#2ED178")) +
  scale_shape_manual(name = "Landuse", values = c("var_1" = 2, "var_2" = 3,
                                                  "var_3" = 4, "var_4" = 5,
                                                  "var_5" = 6))
#> Warning: Removed 150 rows containing missing values (`geom_point()`).

创建于 2024-02-21,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.