从列表列创建 ggplot/ggupset 时出错

Error creating ggplot/ggupset from list-column

我正在尝试从列表列创建一个 ggplot。首先,我创建列表列,然后创建 ggplot。预期的 ggplot 是一个 ggupset 来显示集合交叉点。我按照 ggupset 示例创建列表列和绘图。但是,当我尝试从列表列创建 geom_bar() 时收到以下警告:Warning message: Computation failed in stat_count():non-numeric argument to mathematical function。我也无法使用 tidy_pathway_member 重现 ggupset 示例。下面是一个简单的 reprex 以及 ggupset 示例。

感谢您的宝贵时间和帮助!

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

# Simple reprex
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 fails
# desired output is a bar graph with each intersection of categories on x-axis (i.e., "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::scale_x_upset()

  # Reproduce example taken from `ggupset` (https://github.com/const-ae/ggupset)
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)

# ggplot fails
# desired output available at https://github.com/const-ae/ggupset under "Reshaping quadratic data"
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 package (v0.3.0)

于 2021-01-05 创建

因为您已经注释掉 + scale_x_upset(),所以常规的 ggplot2 比例尺不会将列表列识别为可以映射到比例尺的东西。如果你取消对不规则天平的评论,它会像我预期的那样工作:

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

# Simple reprex
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)

df_list_col %>% 
  ggplot(aes(x = colors)) +
  geom_bar() +
  scale_x_upset()

reprex package (v0.3.0)

于 2021-01-05 创建