Search code examples
rggplot2plotpca

How to Overlay Bubble Plot on PCA Biplot in R


I am trying to overlay my PCA biplot that includes environmental variables per year with a bubble plot of catch per unit effort (CPUE) per year. Essentially, where my current plot displays years I would also a bubble corresponding to CPUE of that year.

I am not sure how to code this onto my current PCA plot and was wondering if anyone had any tips.

Attached is an image of what I would like as well as what I have currently have as my PCA plot.What I want What I have

Thanks in advance!

I have tried googling similar plots but can't find anything similar. I also asked AI if they had any solutions and this is what they gave me but it just produced a bubble plot of CPUE over years.

bubble_plot <- ggplot(BayAnchovyannualdata, aes(x = Year, y = AnnualCPUE.y, size = AnnualCPUE.y, fill = AnnualCPUE.y)) +
  geom_point(shape = 21, alpha = 0.7) +
  scale_size_continuous(range = c(2, 10)) +
  scale_fill_gradient(low = "blue", high = "red") +
  labs(title = "CPUE Per Year")

# View the bubble plot
print(bubble_plot)

pc_scores <- as.data.frame(results$x)

# Create a scatterplot with the first two principal components (PC1 and PC2)
pca_scatterplot <- ggplot(pc_scores, aes(x = PC1, y = PC2)) +
  geom_point(shape = 1, size = 3, color = "blue") +
  labs(title = "PCA Results")

# View the PCA scatterplot
print(pca_scatterplot)

# Combine the PCA scatterplot and bubble plot
final_plot <- pca_scatterplot + annotation_custom(ggplotGrob(bubble_plot), xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)

# View the combined plot
print(final_plot)

This didn't work because the PCA plot and this bubble plot do not have the same x axis.


Solution

  • Alternatively, you can use fviz_pca_biplot() from factoextra package.

    install.packages("factoextra")
    install.packages("FactoMineR")  
    library(FactoMineR)  #this is for PCA()
    library(factoextra)  #this is for fviz_pca_biplot()
    
    res.pca = PCA(yourdata,  scale.unit=TRUE) #results from PCA()
    
    fviz_pca_biplot(res.pca, #your pca result
                 col.ind = "cos2", #the color (gradient) for the individuals
                 pointsize = "cos2", #the size of the points...(maybe the cos2 if you want to check the results
                 gradient.cols = c("red", "blue", "green"),  #some 3 colors for the gradient...
                 repel = TRUE # avoid to overwrite text .lol.
    )
    

    enter image description here