如何使用fviz_pca_ind为PCA分数图中的点添加黑色边框?

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

我目前正在使用factoextra 包中的fviz_pca_ind 函数在R 中可视化PCA 分数图。但是,我在对图中的点应用黑色边框时遇到了问题。

尽管在函数中指定了

col.ind = "black"
,但我无法在点周围获得黑色边框。

这是一个最小的可重现示例,其中包含我的 R 代码的相关部分:

# Load required libraries
library(factoextra)
library(ggplot2)

# Create sample data
set.seed(123)
df <- data.frame(
  Place = rep(c("A", "B", "C"), each = 10),
  Score1 = rnorm(30),
  Score2 = rnorm(30)
)

My_PCA <- prcomp(df[, c("Score1", "Score2")])

# Plot scores
fviz_pca_ind(My_PCA,
             geom = "point",
             pointsize = rep(5, nrow(df)),
             habillage = df$Place,
             col.ind = "black",
             fill = df$Place,
             palette = c("red", "green", "blue"),
             repel = TRUE
) +
  scale_shape_manual(values = c(21, 24, 22)) +
  labs(title=NULL) +
  theme(legend.position="none")

我已经查看了文档并尝试了各种方法,但似乎都不起作用。我也考虑过使用不同的参数,例如alpha,但他们没有解决问题。任何帮助或建议将不胜感激。

r visualization pca factoextra
2个回答
0
投票

我不知道,如果这能找到你的答案,但我从我这边尝试,我试图提出观点以及边界 我在这里得到的一些链接https://www.biostars.org/p/417894/

# Load required libraries
library(factoextra)
library(ggplot2)

# Create sample data
set.seed(123)
df <- data.frame(
  Place = rep(c("A", "B", "C"), each = 10),
  Score1 = rnorm(30),
  Score2 = rnorm(30)
)

My_PCA <- prcomp(df[, c("Score1", "Score2")])

# Plot scores with black border using ggplot2
my_plot <- fviz_pca_ind(My_PCA,
                        geom = "point",
                        pointsize = rep(5, nrow(df)),
                        habillage = df$Place,
                        fill = df$Place,
                        palette = c("red", "green", "blue"),
                        repel = TRUE,
                        ggtheme = theme_bw()  # Add this line
) +
  scale_shape_manual(values = c(21, 24, 22)) +
  labs(title=NULL) +
  theme(legend.position="none")

# Add a black border around the points using ggplot2 layer
my_plot + geom_point(color = "black", size = 2, alpha = 0.7)+scale_color_manual(values = c("black", "black", "black"))


0
投票

或者下面这个,可以在这里找到https://github.com/kevinblighe/PCAtools/issues/35

# Load required libraries
library(factoextra)
library(ggplot2)

# Create sample data
set.seed(123)
df <- data.frame(
  Place = rep(c("A", "B", "C"), each = 10),
  Score1 = rnorm(30),
  Score2 = rnorm(30)
)

# Create a new 'Group' variable based on 'Place'
df$Group <- ifelse(df$Place == "A", "red",
                   ifelse(df$Place == "B", "green", "blue"))

My_PCA <- prcomp(df[, c("Score1", "Score2")])

# Plot scores with black border using fviz_pca_ind
fviz_pca_ind(My_PCA,
             geom = "point",
             pointsize = rep(3.5, nrow(df)),
             habillage = df$Place,
             fill = df$Group,
             palette = c("red", "green", "blue"),
             repel = TRUE,
             stroke = 1  # Set the border width
) +
  scale_shape_manual(values = c(21, 24, 22)) +
  labs(title=NULL) +
  theme(text = element_text(size = 20),
        axis.text.x = element_text(size = 20),
        axis.text.y = element_text(size = 20),
        legend.key = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        panel.border = element_rect(linetype = "solid", fill = NA)
  )

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