1

Is there a way to show unobserved sets in ggupset?

Reprex

In this example, I would like to show B even though it is not observed.

library(ggplot2)
library(ggupset)

df <- data.frame(x = I(list(c("A", "C"),
                            "A",
                            "C")))

df %>% 
  ggplot(aes(x = x)) + 
  geom_bar(stat = 'count') + 
  scale_x_upset()

enter image description here

Attempts

# manually give sets (does not work)
df %>% 
  ggplot(aes(x = x)) + 
  geom_bar(stat = 'count') + 
  scale_x_upset(sets = LETTERS[1:3])

# add to data, but do not show (does not work)
df %>% 
  tibble::add_row(x = list("B")) %>% 
  ggplot(aes(x = x)) + 
  geom_bar(stat = 'count') + 
  scale_x_upset(n_sets = 2)

Expected Output

It would be nice to be able to sort the set labels to show A, B, C rather than A, C, B.

enter image description here

LMc
  • 12,577
  • 3
  • 31
  • 43

1 Answers1

1

You can use axis_combmatrix if you first concatenate your list elements into a standard character column with a separator of your choosing:

df %>%
  within(x <- sapply(x, paste, collapse = "_")) %>%
  ggplot(aes(x = x)) + 
  geom_bar(stat = 'count') + 
  axis_combmatrix(sep = "_", levels = c("A", "B", "C"))

enter image description here

You can set whatever order of levels you like here:

df %>%
  within(x <- sapply(x, paste, collapse = "_")) %>%
  ggplot(aes(x = x)) + 
  geom_bar(stat = 'count') + 
  axis_combmatrix(sep = "_", levels = c("A", "C", "B"))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thanks this is great. I also discovered the [ComplexUpset](https://github.com/krassowski/complex-upset) package which, unlike `ggupset`, is still maintained and has much greater capabilities. – LMc Nov 17 '22 at 23:52