如何在R中将气泡图叠加在PCA双标图上

5us2dqdw  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(135)

我试图将包含每年环境变量的PCA双标图与每年单位努力渔获量(CPUE)的气泡图重叠。本质上,在我当前的图显示年份的地方,我也会有一个对应于该年CPUE的气泡。
我不知道如何将其编码到我当前的PCA图中,想知道是否有人有任何提示。
附件是我想要的图像,以及我目前作为我的PCA图。What I wantWhat I have
先谢谢你了!
我也试过在谷歌上搜索类似的图,但找不到任何类似的东西。我还问人工智能,如果他们有任何解决方案,这是他们给我的,但它只是产生了多年来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轴。

lxkprmvk

lxkprmvk1#

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

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.
)

字符串


的数据

相关问题