rggplot2nested-listsset-intersectionupsetr

Error creating ggplot/ggupset from list-column


I am trying to create a ggplot from a list column. First I create the list column and then the ggplot. The intended ggplot is a ggupset to show set intersections. I followed the ggupset example to create the list column and plot. However, I get the following warning when trying to create a geom_bar() from the list column: Warning message: Computation failed in stat_count():non-numeric argument to mathematical function. I am also unable to reproduce the ggupset example using tidy_pathway_member. Below is a simple reprex as well as the ggupset example.

Thank you for your time and help!

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()

Created on 2021-01-05 by the reprex package (v0.3.0)


Solution

  • Because you've commented out + scale_x_upset(), the regular ggplot2 scales don't recognize list-columns as something that can map to a scale. If you un-comment the upset scales, it works as I would expect:

    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()
    

    Created on 2021-01-05 by the reprex package (v0.3.0)