ggupset icon indicating copy to clipboard operation
ggupset copied to clipboard

Total Set Size for each category

Open acpguedes opened this issue 4 years ago • 8 comments

Hi,

How I add information about the total size of a set at the left of categories names, as the UpSetR does.

I'd like to use ggupset to generate a plot equal to upsetr, as the image below.

image

acpguedes avatar Feb 28 '20 23:02 acpguedes

Hi Aureliano,

thank you for your interest in my package. The feature that you are asking about is not directly supported by ggupset. This was a conscious design decision because for me the defining feature of the Upset plot is the clever representation of overlaps on the x-axis.

You can however make the a plot equivalent to UpsetR if you use ggupset and combine it with cowplot or patchwork. Both packages allow you to arrange multiple ggplots. This way you can make the Set Size plot separately and add it on the lower left side of the main ggupset plot.

I created a small example to demonstrate the approach. You can play around with the margins and rel_heights / rel_widths to optimize the appearence, but this should help you get started:

library(tidyverse)
library(ggupset)

main_plot <- tidy_movies %>%
  distinct(title, year, length, .keep_all=TRUE) %>%
  ggplot(aes(x=Genres)) +
  geom_bar() +
  scale_x_upset(n_intersections = 20)

side_plot <- tidy_movies %>%
  select(Genres) %>%
  unnest(cols = Genres) %>%
  count(Genres) %>%
  mutate(Genres = fct_reorder(as.factor(Genres), n)) %>%
  ggplot(aes(y = n, x = Genres)) +
    geom_col() +
    coord_flip() +
    scale_y_reverse() +
    xlab("") + ylab("") +
    theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())


cowplot::plot_grid(
  cowplot::plot_grid(NULL, side_plot + theme(plot.margin = unit(c(1, -5, -5, 1), "pt")), ncol = 1, rel_heights = c(2.15, 1)),
  main_plot, nrow = 1, rel_widths = c(1, 3)
)
#> Warning: Removed 100 rows containing non-finite values (stat_count).

Created on 2020-02-29 by the reprex package (v0.3.0)

I will keep this issue open as a reference for other people who might have a similar issue in the future.

const-ae avatar Feb 29 '20 12:02 const-ae

Thank you for your example.

acpguedes avatar Feb 29 '20 15:02 acpguedes

You may also want to check out my package: https://github.com/krassowski/complex-upset which does exactly what @const-ae suggests, glueing many ggplots under the hood, using patchwork.

krassowski avatar Mar 03 '20 18:03 krassowski

Hi Michał, thanks for sharing. That looks like an interesting package to easily assemble more complex UpSet plots :)

const-ae avatar Mar 03 '20 22:03 const-ae

Related question: I'd like to be able to show the number of intersections above each bar. Currently I'm using UpSetR::upset() and would like to switch my code over to using ggupset::scale_x_upset() instead. I'm having a bit of trouble figuring out how to do this. Am I missing something in the documentation, or is there not currently an easy way to add numeric labels above each bar?

mjsteinbaugh avatar Aug 25 '20 16:08 mjsteinbaugh

Hey Michael, try this https://stackoverflow.com/questions/26553526/how-to-add-frequency-count-labels-to-the-bars-in-a-bar-graph-using-ggplot2/26556180#26556180 answer. The good thing about ggupset is that you can use all the tricks from ggplot, it does however mean that sometimes typical patterns are a bit opaque. Maybe I should add this specific example to the documentation, because this question has come a few times already :)

const-ae avatar Aug 25 '20 16:08 const-ae

I added a new section to the README that explains how to add the numbers on top https://github.com/const-ae/ggupset#adding-numbers-on-top

const-ae avatar Aug 25 '20 17:08 const-ae

Perfect thanks @const-ae !

mjsteinbaugh avatar Aug 25 '20 17:08 mjsteinbaugh