我正在对我的数据进行主成分分析 (PCA)。 这是脚本的最后一部分,我将在其中构建 PCA 双图:
biplot <- fviz_pca_biplot(pca_result,
# Individuals
geom.ind = "point",
fill.ind = Data$Type, col.ind = "black",
pointshape = 21, pointsize = 2,
palette = c("#04BF8A","#D7D7D9"),
addEllipses = TRUE,
# Variables
col.var = "black",
legend.title = "Type",
repel = TRUE,
xlim = c(-5,14),
ylim = c(-3,8),
) +
xlab("PC1 (74.5%)") + ylab("PC2 (16.6%)")
biplot + theme_bw()
我不明白为什么椭圆的边框颜色与调色板中指定的颜色不同。
现在,我想更改椭圆的边框颜色(如调色板中指定的那样),或者直接删除边框并仅保留椭圆的填充。
你能告诉我该怎么做吗?
非常感谢您的帮助!
我尝试在 addEllipse = TRUE 字符串之后立即应用 ellipse.border.remove = TRUE,但出现错误:
ggpubr :: ggscatter中的错误(数据= df,x =“x”,y =“y”,颜色=颜色,: 形式参数“ellipse.border.remove”与多个实际参数匹配
我怀疑这是 fviz_pca_biplot() 函数的限制,但潜在的解决方法是“手动”指定颜色,例如
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)
# palette works for fill but not for ellipses color
fviz_pca_biplot(res.pca,
# Individuals
geom.ind = "point",
fill.ind = iris$Species,
col.ind = "black",
pointshape = 21, pointsize = 2,
palette = c("#04BF8A","#D7D7D9", "red"),
addEllipses = TRUE,
# Variables
col.var = "black",
legend.title = "Type",
repel = TRUE,
xlim = c(-5,14),
ylim = c(-3,8),
) +
xlab("PC1 (74.5%)") +
ylab("PC2 (16.6%)") +
theme_bw()
#> Coordinate system already present. Adding new coordinate system, which will
#> replace the existing one.
# fix ellipses color
fviz_pca_biplot(res.pca,
# Individuals
geom.ind = "point",
fill.ind = iris$Species,
col.ind = "black",
pointshape = 21, pointsize = 2,
palette = c("#04BF8A","#D7D7D9", "red"),
addEllipses = TRUE,
# Variables
col.var = "black",
legend.title = "Type",
repel = TRUE,
xlim = c(-5,14),
ylim = c(-3,8),
) +
xlab("PC1 (74.5%)") +
ylab("PC2 (16.6%)") +
theme_bw() +
scale_color_manual(values = c("setosa" = "#04BF8A",
"versicolor" = "#D7D7D9",
"virginica" = "red"))
#> Coordinate system already present. Adding new coordinate system, which will
#> replace the existing one.
创建于 2024-02-21,使用 reprex v2.1.0
这适用于您的“真实”数据吗?