rvisualizationpcafactoextra

How to add black border to points in PCA score plot using fviz_pca_ind?


I am currently working on visualizing a PCA score plot in R using the fviz_pca_ind function from the factoextra package. However, I am facing an issue in applying a black border to the points in the plot.

Despite specifying col.ind = "black" in the function, I am unable to get a black border around the points.

Here is a minimal reproducible example with the relevant portion of my R code:

# 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")

I have reviewed the documentation and tried various approaches, but none seem to work. I have also considered using different parameters such as alpha, but they did not resolve the problem. Any help or suggestions would be greatly appreciated.


Solution

  • I dont know, if this finds your answer, but i trie from my side, I tried to put point aswell as boarder some link i got here 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"))
    

    enter image description here