0

我正在尝试从列表列创建一个 ggplot。首先我创建列表列,然后创建 ggplot。预期的 ggplot 是一个显示集合交点的ggupset 。我按照 ggupset 示例创建列表列和绘图。但是,尝试geom_bar()从列表列创建一个时,我收到以下警告:Warning message: Computation failed in stat_count():non-numeric argument to mathematical function. 我也无法使用tidy_pathway_member. 下面是一个简单的 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 包于 2021-01-05 创建(v0.3.0)

4

1 回答 1

1

因为您已注释掉+ 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 包于 2021-01-05 创建(v0.3.0)

于 2021-01-05T14:29:44.963 回答