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

基于phyloseq对象绘制CCA图时的异常问题求助

海龟微生物组CCA绘图异常:饮食类型被错误显示为物种

我正在基于包含饮食、气候等附加信息的海龟大型微生物组数据集绘制CCA(典范对应分析)图,目的是探究这些因素对海龟体内微生物种群的影响。但当前遇到异常:绘图错误地将饮食类型(食肉、杂食)当作物种进行显示,已检查原始数据,未发现数据错误。

当前生成的绘图:
当前CCA绘图

原始代码

constraining_variables_formula <- ~ Diet + Climate

cca_result <- ordinate(
  physeq = turtles_prop,
  method = "CCA",
  formula = constraining_variables_formula
)

sample_scores <- vegan::scores(cca_result, display = "sites")

bp_scores <- vegan::scores(cca_result, display = "bp")
bp_df <- data.frame(labels = rownames(bp_scores), bp_scores)

sample_data_df$Diet <- trimws(as.character(sample_data_df$Diet))
sample_data_df$Species <- trimws(as.character(sample_data_df$Species))

sample_df_plot <- data.frame(
  CCA1 = sample_scores[, "CCA1"],
  CCA2 = sample_scores[, "CCA2"],
  Diet = as.factor(sample_data(turtles_prop)$Diet),
  Species = as.factor(sample_data(turtles_prop)$Species)
)


p_final <- ggplot(data = sample_df, aes(x = CCA1, y = CCA2)) +
  geom_point(aes(color = Diet)) +
  geom_jitter(
    aes(shape = Diet, color = Species),
    size = 3,           # Adjust the point size
    width = 0.05,       # Adjust the amount of horizontal jitter
    height = 0.05       # Adjust the amount of vertical jitter
  ) +
  geom_segment(
    data = bp_df,
    aes(x = 0, y = 0, xend = CCA1, yend = CCA2),
    arrow = arrow(length = unit(0.02, "npc")),
    color = "black",
    linewidth = 0.5,
    inherit.aes = FALSE # Prevents inheriting aesthetics from the main plot
  ) +
  geom_text_repel(
    data = bp_df,
    aes(x = CCA1, y = CCA2, label = labels),
    color = "black",
    size = 3.5,
    box.padding = 0.5,
    inherit.aes = FALSE # Prevents inheriting aesthetics
  ) +
  labs(
      x = paste("CCA1 (", format(summary(cca_result)$cont$importance[2, "CCA1"] * 100, digits = 3), "%)", sep = ""),
      y = paste("CCA2 (", format(summary(cca_result)$cont$importance[2, "CCA2"] * 100, digits = 3), "%)", sep = ""),
      color = "Species", # Title for the color legend
      shape = "Diet"     # Title for the shape legend
    ) +
      theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "gray")

print(p_final)

问题定位与修复

核心问题

  1. 数据集名称不匹配:创建了预处理后的sample_df_plot,但绘图时调用了未定义的sample_df,导致ggplot使用了错误的数据源,引发美学映射混乱。
  2. 图层冗余与冲突:同时使用geom_point和geom_jitter重复绘制点,且geom_point中color=Diet的映射与后续labs的color="Species"标题矛盾,导致图例显示错误。

修复后的代码

constraining_variables_formula <- ~ Diet + Climate

cca_result <- ordinate(
  physeq = turtles_prop,
  method = "CCA",
  formula = constraining_variables_formula
)

sample_scores <- vegan::scores(cca_result, display = "sites")

bp_scores <- vegan::scores(cca_result, display = "bp")
bp_df <- data.frame(labels = rownames(bp_scores), bp_scores)

# 统一处理样本数据,避免重复调用sample_data
sample_data_df <- sample_data(turtles_prop)
sample_data_df$Diet <- trimws(as.character(sample_data_df$Diet))
sample_data_df$Species <- trimws(as.character(sample_data_df$Species))

sample_df_plot <- data.frame(
  CCA1 = sample_scores[, "CCA1"],
  CCA2 = sample_scores[, "CCA2"],
  Diet = as.factor(sample_data_df$Diet),
  Species = as.factor(sample_data_df$Species)
)

p_final <- ggplot(data = sample_df_plot, aes(x = CCA1, y = CCA2)) +
  # 保留geom_jitter,统一映射:颜色对应物种,形状对应饮食
  geom_jitter(
    aes(shape = Diet, color = Species),
    size = 3,
    width = 0.05,
    height = 0.05
  ) +
  geom_segment(
    data = bp_df,
    aes(x = 0, y = 0, xend = CCA1, yend = CCA2),
    arrow = arrow(length = unit(0.02, "npc")),
    color = "black",
    linewidth = 0.5,
    inherit.aes = FALSE
  ) +
  geom_text_repel(
    data = bp_df,
    aes(x = CCA1, y = CCA2, label = labels),
    color = "black",
    size = 3.5,
    box.padding = 0.5,
    inherit.aes = FALSE
  ) +
  labs(
    x = paste("CCA1 (", format(summary(cca_result)$cont$importance[2, "CCA1"] * 100, digits = 3), "%)", sep = ""),
    y = paste("CCA2 (", format(summary(cca_result)$cont$importance[2, "CCA2"] * 100, digits = 3), "%)", sep = ""),
    color = "物种",
    shape = "饮食类型"
  ) +
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "gray")

print(p_final)

关键修复点

  • 修正数据集引用错误:将绘图数据源改为正确的sample_df_plot
  • 移除冗余的geom_point图层,避免点重叠和映射冲突
  • 统一美学映射逻辑:color对应物种、shape对应饮食类型,与图例标题完全匹配
  • 统一处理样本数据,避免多次调用sample_data()导致的潜在不一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 04:03:18