Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Total Set Size for each category #6

Open
acpguedes opened this issue Feb 28, 2020 · 8 comments
Open

Total Set Size for each category #6

acpguedes opened this issue Feb 28, 2020 · 8 comments

Comments

@acpguedes
Copy link

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

@const-ae
Copy link
Owner

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.

@acpguedes
Copy link
Author

Thank you for your example.

@krassowski
Copy link

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.

@const-ae
Copy link
Owner

const-ae commented Mar 3, 2020

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

@mjsteinbaugh
Copy link

mjsteinbaugh commented Aug 25, 2020

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?

@const-ae
Copy link
Owner

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
Copy link
Owner

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

@mjsteinbaugh
Copy link

Perfect thanks @const-ae !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants