PCA Plot Created Using R for Data_Liu_PCA_plot

gene_x 0 like s 377 view s

Tags: pipeline

PCA_plot.png

PCA_plot.png

To make the circles (ellipses) more focused on the point clouds, we can adjust the level parameter in stat_ellipse() to a smaller value, which will create tighter circles around the clusters. Here's the modified code with more focused circles:

  1. library(ggrepel)
  2. library(dplyr)
  3. library(ggplot2)
  4. library(readxl) # To read Excel files
  5. # Load the data from the Excel file
  6. merged_pca_data <- read_excel("PCA figure.xlsx")
  7. # Prepare the data: select relevant columns and add a 'condition' variable
  8. pca_data <- merged_pca_data %>%
  9. select("Component 1", "Component 2", "Component 3", "Component 4", "Component 5",
  10. "Component 6", "Component 7", "Component 8", "C: Grouping") %>%
  11. rename(
  12. PC1 = "Component 1",
  13. PC2 = "Component 2",
  14. PC3 = "Component 3",
  15. PC4 = "Component 4",
  16. PC5 = "Component 5",
  17. PC6 = "Component 6",
  18. PC7 = "Component 7",
  19. PC8 = "Component 8",
  20. condition = "C: Grouping"
  21. ) %>%
  22. mutate(condition = factor(condition)) # Add 'condition' as a factor for coloring
  23. # Prepare PCA plot data (data to be plotted)
  24. plot_pca_df <- pca_data
  25. # Save PCA plot as PNG
  26. png("PCA_plot.png", width = 1000, height = 600, res = 150)
  27. # Create PCA plot using ggplot with tighter circles around point clouds
  28. ggplot(plot_pca_df, aes(x = PC1, y = PC2, color = condition)) +
  29. geom_point(size = 4, alpha = 0.8) + # Plot points
  30. stat_ellipse(
  31. type = "norm",
  32. level = 0.6, # Reduced from 0.9 to make circles tighter
  33. size = 1,
  34. linetype = 2,
  35. alpha = 0.7 # Added transparency to circles
  36. ) +
  37. labs(
  38. title = "", # PCA Plot
  39. x = "Principal Component 1",
  40. y = "Principal Component 2"
  41. ) +
  42. theme_minimal() +
  43. scale_color_manual(values = c("blue", "red")) + # Customize colors for different conditions
  44. theme(legend.title = element_blank()) # Optional: remove legend title
  45. dev.off() # Close the PNG device and save the file

Key changes:

  • Reduced the level parameter from 0.9 to 0.6 to make the circles tighter around the point clouds

  • Added alpha = 0.7 to make the circles slightly transparent

  • Kept the dashed line style (linetype = 2) for better visibility

If the circles are still too large, you can try:

  • Decreasing the level further (e.g., 0.5 or 0.4)

  • Using type = "t" instead of "norm" for a more robust ellipse estimation

  • Adding segments = 100 to make the ellipses smoother

For perfect circles (if your data is properly scaled), we could also try:

  1. stat_ellipse(type = "euclid", level = 0.6, size = 1, linetype = 2, alpha = 0.7)

like unlike

点赞本文的读者

还没有人对此文章表态


本文有评论

没有评论

看文章,发评论,不要沉默


© 2023 XGenes.com Impressum