You need to enable JavaScript to run this app. R中使用ggplot/ggupset从列表列绘图触发stat_count计算错误
优惠活动
大模型
产品
解决方案
定价
更多

R中使用ggplot/ggupset从列表列绘图触发stat_count计算错误

问题描述

尝试基于列表列创建ggplot2可视化,计划使用ggupset包绘制集合交集结果。严格参照ggupset官方示例的流程构建列表列并准备绘图,但在基于列表列调用geom_bar()时出现以下警告:

Warning message: Computation failed in stat_count():non-numeric argument to mathematical function

同时也无法复现ggupset官方基于tidy_pathway_member的示例效果。

可复现代码与运行现象

加载依赖包

library(dplyr)
library(tidyr)
library(ggplot2)
library(ggupset)

简单测试用例

构造基础测试数据:

df <-
  dplyr::tribble(
    ~id,   ~color,
    "a",    "red", 
    "b",   "blue", 
    "b", "purple",
    "c", "purple",
    "d",    "red",
    "d",   "blue"
  )

df_list_col <-
  df %>% 
  group_by(id) %>% 
  summarise(colors = list(color))
#> `summarise()` ungrouping output (override with `.groups` argument)

对该列表列数据进行ggplot绘图时触发报错:

  • 预期输出:x轴为各类别交集的条形图,对应类别包括red、blue,purple、purple、red,blue
  • 实际运行警告:
df_list_col %>% 
  ggplot(aes(x = colors)) +
  geom_bar()
#> Warning: Computation failed in `stat_count()`:
#> non-numeric argument to mathematical function

该代码运行输出如下图:
简单测试用例运行结果

官方示例复现测试

尝试复现ggupset官方文档中基于tidy_pathway_member的示例效果,对应代码如下:

tidy_pathway_member <- 
  ggupset::gene_pathway_membership %>%
  as_tibble(rownames = "Pathway") %>%
  tidyr::gather(Gene, Member, -Pathway) %>%
  filter(Member) %>%
  select(- Member)

tidy_pathway_member_list_col <-
  tidy_pathway_member %>% 
  group_by(Gene) %>%
  summarize(Pathways = list(Pathway)) 
#> `summarise()` ungrouping output (override with `.groups` argument)

对整理好的列表列数据绘图时触发完全相同的报错:

tidy_pathway_member_list_col %>% 
  ggplot(aes(x = Pathways)) +
  geom_bar()
#> Warning: Computation failed in `stat_count()`:
#> non-numeric argument to mathematical function

该代码运行输出如下图:
官方示例复现运行结果

注:测试过程中注释掉了后续的ggupset::scale_x_upset()调用,排除该函数对绘图流程的干扰。示例代码基于reprex包 v0.3.0 于2021-01-05生成。


内容的提问来源于stack exchange,提问作者maia-sh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.10 23:24:03