You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言的PCA双标图上叠加CPUE气泡图?

在PCA双标图上叠加年度CPUE气泡图的实现方案

问题需求

需要在基于年度环境变量生成的PCA双标图中,将每个年份对应的单位捕捞努力量(CPUE)以气泡形式叠加到对应年份的PCA得分点位置,气泡的大小、颜色与CPUE数值关联。

现有方案的问题

此前尝试的代码错误地将两个独立坐标轴的图表(CPUE年份气泡图、PCA散点图)通过annotation_custom强行叠加,完全不符合需求——两个图表的坐标轴维度完全不同,无法实现"在PCA点位置叠加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")

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

pca_scatterplot <- ggplot(pc_scores, aes(x = PC1, y = PC2)) +
  geom_point(shape = 1, size = 3, color = "blue") +
  labs(title = "PCA Results")

final_plot <- pca_scatterplot + annotation_custom(ggplotGrob(bubble_plot), xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
print(final_plot)

正确实现思路与代码

核心是将PCA得分数据与CPUE数据按年份关联,让每个PCA坐标点都绑定对应年份的CPUE值,再在同一个ggplot画布中绘制气泡。

完整代码示例

# 1. 准备环境变量数据并执行PCA(补充基础流程,适配你的数据结构)
# 假设BayAnchovyannualdata包含Year列、环境变量列、AnnualCPUE.y列
env_columns <- c("环境变量1", "环境变量2", "环境变量3") # 替换为实际环境变量列名
env_data <- BayAnchovyannualdata[, env_columns]
# 执行标准化PCA
pca_results <- prcomp(env_data, scale. = TRUE)

# 2. 提取PCA得分并关联年份、CPUE数据
pc_scores <- as.data.frame(pca_results$x[, 1:2]) # 仅保留PC1和PC2
pc_scores$Year <- BayAnchovyannualdata$Year
pc_scores$CPUE <- BayAnchovyannualdata$AnnualCPUE.y

# 3. 绘制带CPUE气泡的PCA双标图
pca_cpue_biplot <- ggplot(pc_scores, aes(x = PC1, y = PC2)) +
  # 绘制环境变量载荷箭头(双标图核心元素)
  geom_segment(
    data = as.data.frame(pca_results$rotation * max(pc_scores[,1:2])),
    aes(x = 0, y = 0, xend = PC1, yend = PC2),
    arrow = arrow(length = unit(0.2, "cm")),
    color = "gray50", linewidth = 0.8
  ) +
  # 标注环境变量名称
  geom_text(
    data = as.data.frame(pca_results$rotation * max(pc_scores[,1:2])),
    aes(label = rownames(pca_results$rotation)),
    color = "gray50", hjust = 1.2, size = 3.5
  ) +
  # 绘制CPUE气泡:size和fill绑定CPUE值
  geom_point(
    aes(size = CPUE, fill = CPUE),
    shape = 21, alpha = 0.7, color = "black"
  ) +
  # 可选:标注年份,放在气泡旁
  geom_text(aes(label = Year), hjust = -0.3, vjust = 0.3, size = 3) +
  # 设置刻度与标签
  scale_size_continuous(name = "年度CPUE", range = c(2, 10)) +
  scale_fill_gradient(name = "年度CPUE", low = "blue", high = "red") +
  labs(
    title = "环境变量PCA双标图(叠加年度CPUE气泡)",
    x = paste0("PC1 (", round(pca_results$sdev[1]^2/sum(pca_results$sdev^2)*100, 1), "%)"),
    y = paste0("PC2 (", round(pca_results$sdev[2]^2/sum(pca_results$sdev^2)*100, 1), "%)")
  ) +
  theme_bw() +
  theme(legend.position = "right")

# 输出图表
print(pca_cpue_biplot)

关键说明

  • 数据关联:确保PCA得分的每一行(对应一个年份)都绑定了该年份的CPUE值,这是实现叠加的核心前提。如果CPUE数据在单独的表中,使用merge(pc_scores, cpue_table, by = "Year")完成关联。
  • 双标图元素:代码中包含了环境变量的载荷箭头与标签,还原了双标图的完整信息;如果不需要可以删除这部分代码。
  • 气泡样式:使用shape=21实现带黑色边框的填充气泡,alpha设置透明度避免气泡遮挡,range参数控制气泡大小的范围。

内容的提问来源于stack exchange,提问作者mp101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 09:43:28