Home > other >  How to show all sets in ggupset?
How to show all sets in ggupset?

Time:11-18

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

CodePudding user response:

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

  • Related