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

ggpicrust2包pathway_errorbar函数报错:legend.text.align未定义

解决ggpicrust2的pathway_errorbar函数报错及数据显示问题

报错信息

Error in `plot_theme()`:
! The `legend.text.align` theme element is not defined in the element
  hierarchy.
Run `rlang::last_trace()` to see where the error occurred.
Backtrace:
     ▆
  1. ├─base (local) `<fn>`(x)
  2. └─patchwork:::print.patchwork(x)
  3.   └─patchwork:::build_patchwork(plot, plot$layout$guides %||% "auto")
  4.     └─base::lapply(x$plots, plot_table, guides = guides)
  5.       ├─patchwork (local) FUN(X[[i]], ...)
  6.       └─patchwork:::plot_table.ggplot(X[[i]], ...)
  7.         └─ggplot2::ggplotGrob(x)
  8.           ├─ggplot2::ggplot_gtable(ggplot_build(x))
  9.           └─ggplot2:::ggplot_gtable.ggplot_built(ggplot_build(x))
 10.             └─ggplot2:::plot_theme(plot)
 11.               └─ggplot2:::validate_theme(theme)
 12.                 └─base::mapply(...)
 13.                   └─ggplot2 (local) `<fn>`(...)
 14.                     └─cli::cli_abort(...)
 15.                       └─rlang::abort(...)

使用代码

ggpicrust2::pathway_errorbar(abundance = kegg_abundance,
                             daa_results_df = daa_annotated_sub_method_results_df,
                             Group = metadata$treatment,
                             ko_to_kegg = TRUE,
                             p_values_threshold = 0.05,
                             order = "pathway_class",
                             select = test[1:10],
                             p_value_bar = TRUE,
                             colors = NULL,
                             x_lab = "pathway_name")

排查情况

  • 使用函数文档提供的示例数据集仍出现相同报错,推测为ggplot2与ggprism的兼容性问题
  • 手动移除函数中所有ggprism相关代码后,图表可生成,但差异丰度数据无法显示,怀疑差异丰度计算结果存在问题

解决建议

1. 修复版本兼容性问题

legend.text.align是ggplot2 3.4.0版本新增的主题元素,旧版ggprism未适配该参数,可通过以下两种方式解决:

  • 升级ggprism:执行install.packages("ggprism")安装最新版,新版本已适配ggplot2的新增主题元素
  • 降级ggplot2:执行packageVersion("ggplot2")查看当前版本,若为3.4.0+,可降级到3.3.6版本:
    packageurl <- "https://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_3.3.6.tar.gz"
    install.packages(packageurl, repos=NULL, type="source")
    

2. 手动修改pathway_errorbar函数

若不想调整包版本,可复制原函数代码并移除legend.text.align相关设置:

  1. 执行ggpicrust2::pathway_errorbar查看函数源码,复制到本地
  2. 找到包含theme_prism()的代码段,移除其中的legend.text.align = 0参数
  3. 运行修改后的本地函数

3. 验证差异丰度计算结果

先确认差异丰度数据是否正确:

  • 查看daa_results_df的结构:str(daa_results_df),确认包含p_value、log2FoldChange等关键列
  • 统计显著差异通路数量:sum(daa_results_df$p_value < 0.05, na.rm = TRUE),若为0则说明无显著差异通路,需检查差异分析方法参数
  • 检查select = test[1:10]中的test对象是否包含有效通路名称,可通过intersect(test, daa_results_df$pathway_name)确认交集

4. 手动绘制替代图表

如果函数问题无法快速解决,可手动用ggplot2构建可视化:

# 1. 计算每个通路的分组均值和标准差
library(dplyr)
abundance_summary <- kegg_abundance %>%
  tibble::rownames_to_column("pathway_name") %>%
  tidyr::pivot_longer(-pathway_name, names_to = "sample", values_to = "abundance") %>%
  left_join(metadata %>% tibble::rownames_to_column("sample"), by = "sample") %>%
  group_by(pathway_name, treatment) %>%
  summarise(mean_abundance = mean(abundance),
            sd_abundance = sd(abundance)) %>%
  ungroup()

# 2. 筛选显著差异通路并合并数据
sig_pathways <- daa_results_df %>%
  filter(p_value < 0.05) %>%
  select(pathway_name, p_value, log2FoldChange)

plot_data <- abundance_summary %>%
  inner_join(sig_pathways, by = "pathway_name") %>%
  slice(1:10) # 取前10个通路

# 3. 绘制误差棒图
library(ggplot2)
ggplot(plot_data, aes(x = pathway_name, y = mean_abundance, fill = treatment)) +
  geom_col(position = position_dodge(width = 0.8), width = 0.7) +
  geom_errorbar(aes(ymin = mean_abundance - sd_abundance, ymax = mean_abundance + sd_abundance),
                position = position_dodge(width = 0.8), width = 0.2) +
  geom_text(aes(label = paste0("p=", round(p_value, 3))), 
            position = position_dodge(width = 0.8), vjust = -0.5) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 03:14:58