ComplexUpset R Package

repository·master·Indexed 20 days ago

https://github.com/krassowski/complex-upset

An R package for creating highly extensible UpSet visualizations using ggplot2 and patchwork. It allows users to add complex annotations (such as boxplots or violin plots) and queries to highlight specific intersections or sets, providing more flexibility than UpSetR. The package supports installation via CRAN, GitHub, and conda-forge, and includes integration guides for using R within Jupyter Notebooks via rpy2.

Tokens
1.9K
Snippets
9
Records
11
Agent score
69%

What's inside ComplexUpset

  1. How ComplexUpset compares to other UpSet packages

    master

    Choosing the right tool depends on your specific needs:

    PackageComparison
    ComplexUpsetProvides full ggplot2 extensibility using patchwork. Best for highly customized, annotated plots.
    UpSetRThe pioneer tool, but no longer actively maintained and not designed for ggplot2 extension.
    ggupsetUses scales (e.g., scale_x_upset) to convert plots. Recommended for simple ggplot workflows.
    ComplexHeatmapOffers UpSet plots with annotations. Best if you are already using it for heatmap workflows.
    VCG/upsetAn option for interactive use.
  2. Install ComplexUpset

    master

    You can install ComplexUpset using several methods depending on your environment:

    From GitHub (Latest Version)

    Use devtools to install the most recent development version:

    if(!require(devtools)) install.packages("devtools")
    devtools::install_github("krassowski/complex-upset")

    From CRAN (Stable Release)

    Install the stable version available on CRAN:

    install.packages('ComplexUpset')

    Using Conda/Mamba

    Install via the conda-forge channel:

    conda install -c conda-forge r-complexupset
    if(!require(devtools)) install.packages("devtools")
    devtools::install_github("krassowski/complex-upset")
  3. Prepare datasets for ComplexUpset

    master

    ComplexUpset typically operates on datasets where set membership is represented by columns. If your dataset uses integer indicators (e.g., 1 for presence, 0 for absence), you should convert these columns to boolean values (TRUE/FALSE) to ensure compatibility with the package's logic.

    Example workflow:

    1. Identify the columns representing the sets (e.g., genres).
    2. Convert those columns to boolean: df[set_columns] = df[set_columns] == 1.
    3. Ensure there are no missing values in the columns used for set membership or grouping if using na.omit().
    # Example: Converting genre columns to boolean
    genres = colnames(movies)[18:24]
    movies[genres] = movies[genres] == 1
  4. Install ComplexUpset from GitHub via R magic

    master

    If ComplexUpset is not already installed in your R environment, you can install it directly from the GitHub repository using the %%R magic command. This step also ensures devtools is available.

    %%R
    if (!require(devtools, quietly=T)) install.packages("devtools")
    if (!require(ComplexUpset, quietly=T)) devtools::install_github("krassowski/complex-upset")
  5. Configure plot output size in Jupyter notebooks

    master

    If you are working in a Jupyter notebook using the IRKernel, you can use a utility function to adjust the output dimensions of your plots. This is not required if you are using RStudio or running R from a terminal.

    set_size = function(w, h, factor=1.5) {
        s = 1 * factor
        options(
            repr.plot.width=w * s,
            repr.plot.height=h * s,
            repr.plot.res=100 / factor,
            jupyter.plot_mimetypes='image/png',
            jupyter.plot_scale=1
        )
    }
  6. Create an UpSet plot with annotations and queries

    master

    The upset() function is the primary interface for generating UpSet plots. It allows you to extend UpSetR functionality with ggplot2's extensibility.

    Key features include:

    • annotations: A list of ggplot2 objects that add data visualizations (like boxplots or violin plots) corresponding to the intersections.
    • queries: A list of upset_query() objects used to highlight specific intersections or sets with custom colors and fills.
    • min_size: Filters intersections by a minimum size.
    • width_ratio: Adjusts the width ratio of the plot components.

    Note: If you use ggbeeswarm::geom_quasirandom in your annotations, ensure the ggbeeswarm package is installed; otherwise, you can use geom_jitter from ggplot2.

    library(ggplot2)
    library(ComplexUpset)
    
    if(!require(ggplot2movies)) install.packages('ggplot2movies')
    movies = ggplot2movies::movies
    genres = c('Action', 'Animation', 'Comedy', 'Drama', 'Documentary', 'Romance')
    
    upset(
        movies,
        genres,
        annotations = list(
            'Length'=ggplot(mapping=aes(x=intersection, y=length)) + geom_boxplot(),
            'Rating'=ggplot(mapping=aes(x=intersection, y=rating))
                + ggbeeswarm::geom_quasirandom(aes(color=log10(votes)))
                + geom_violin(width=1.1, alpha=0.5)
        ),
        queries=list(
            upset_query(
                intersect=c('Drama', 'Comedy'),
                color='red',
                fill='red',
                only_components=c('intersections_matrix', 'Intersection size')
            ),
            upset_query(
                set='Drama',
                fill='blue'
            ),
            upset_query(
                intersect=c('Romance', 'Drama'),
                fill='yellow',
                only_components=c('Length')
            )
        ),
        min_size=10,
        width_ratio=0.1
    )