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

88 Views Asked by At

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.

2

There are 2 best solutions below

3
Umar On BEST ANSWER

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

0
Umar On

or this below one , found here 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)
  )

enter image description here