如何在 R 中的 PCA Biplot 上叠加气泡图

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

我正在尝试将包含每年环境变量的 PCA 双图与每年单位努力捕获量 (CPUE) 的气泡图重叠。本质上,在我当前的绘图显示年份的地方,我也会有一个与当年的 CPUE 相对应的气泡。

我不确定如何将其编码到我当前的 PCA 图上,并且想知道是否有人有任何提示。

附件是我想要的图像以及我目前拥有的 PCA 图。What I want What I have

提前致谢!

我尝试用谷歌搜索类似的情节,但找不到任何类似的内容。我还询问 AI 他们是否有任何解决方案,这就是他们给我的,但它只是生成了多年来 CPUE 的气泡图。

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)

这不起作用,因为 PCA 图和气泡图没有相同的 x 轴。

r ggplot2 plot pca
1个回答
0
投票

您可以使用factoextra包中的fviz_pca_biplot()。

install.packages("factoextra")
library(factoextra)

res.pca = PCA(yourdata,  scale.unit=TRUE)

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.
)
© www.soinside.com 2019 - 2024. All rights reserved.