Creating Customized Upset Plots with Right-Side Bars Using the UpSetR Package in R

Upset Plot with Set Size Bars in Right Side

The traditional Venn-diagram has been a staple for visualizing the relationships between sets. However, when dealing with multiple components or sets, it can become challenging to compare them effectively. The UpSetR package offers a solution by providing an upset plot, which is particularly useful for comparing multiple sets.

In this article, we will delve into the world of upset plots and explore how to adjust the UpSetR package to move horizontal bars from the left side to the right side of the plot.

Introduction to Upset Plots

An upset plot is a type of bar chart that displays the size of each set in a hierarchical manner. The plot typically consists of two main components: the matrix (a grid-like structure) and the sets (horizontal bars). Each bar’s height represents the size of its corresponding set.

Working with UpSetR Package

The UpSetR package provides an R implementation for creating upset plots. It includes several functions that can be used to customize the appearance and behavior of the plot. In this article, we will focus on two main functions: NoAttBasePlot and Make_size_plot.

NoAttBasePlot Function

The NoAttBasePlot function is responsible for rendering the base of the upset plot. It takes several input arguments, including:

  • legend: a logical vector indicating whether to display a legend
  • size_plot_height: the height of the size plot (horizontal bars)
  • Main_bar_plot: the plot for displaying main bars
  • Matrix_plot: the plot for displaying the matrix
  • hratios: the ratio of the bar heights
  • Size_plot: the plot for displaying set sizes
  • query_legend: a character string indicating whether to display a legend on top or bottom
  • set_metadata: a list containing metadata about the sets

The function first determines the layout and position of the various components based on the input arguments. It then renders each component using the grid.draw function.

Make_size_plot Function

The Make_size_plot function is responsible for creating the size plot (horizontal bars). It takes several input arguments, including:

  • Set_size_data: a data frame containing set sizes
  • sbar_color: the color of the bars
  • ratios: a vector representing the bar heights
  • ylabel: the label for the y-axis
  • scale_sets: a character string indicating the scale type (e.g., “identity,” “log10,” or “log2”)
  • text_scale: a vector representing the scaling factor for the x-axis title and tick labels
  • set_size_angle: the angle of the set size text
  • set_size.show: a logical vector indicating whether to display set sizes
  • set_size.scale_max: a numeric value representing the maximum scale

The function first scales the data if necessary based on the input arguments. It then creates the size plot using the ggplot function and adds set sizes as text labels.

Modifying UpSetR Package for Right-Side Bars

To move horizontal bars from the left side to the right side of the upset plot, we need to modify the NoAttBasePlot and Make_size_plot functions. Here’s how we can do it:

## Modified NoAttBasePlot function
NoAttBasePlot <- function (legend, size_plot_height, Main_bar_plot, Matrix_plot, 
    hratios, Size_plot, query_legend, set_metadata, set_metadata_plots, 
    newpage) {
    top <- 1
    bottom <- 100
    if ((!is.null(legend)) && (query Legend != tolower("none"))) {
        if (query_legend == tolower("top")) {
            top <- 3
            bottom <- 102
            legend_top <- 1
            legend_bottom <- 3
            size_plot_height <- (size_plot_height + 2)
        }
        else if (query Legend == tolower("bottom")) {
            legend_top <- 101
            legend_bottom <- 103
        }
    }
    # Modified
    vp = UpSetR:::vplayout(top:bottom, 1:(matrix_and_mainbar_right-matrix_and_mainbar_left))
    pushViewport(vp)
    grid.draw(arrangeGrob(Main_bar_plot, Matrix_plot, heights = hratios))
    popViewport()
    # Modified
    vp = UpSetR:::vplayout(size_plot_height:bottom, (matrix_and_mainbar_right-matrix_and_mainbar_left-1):96)
    pushViewport(vp)
    grid.draw(arrangeGrob(Size_plot))
    popViewport()
    if (!is.null(set_metadata)) {
        for (i in 1:length(set_metadata_plots)) {
            if (i != 1) {
                metadata_left <- 1 + metadata_right
                metadata_right <- metadata_right + set_metadata$plots[[i]]$assign
            }
            else {
                metadata_left <- 1
                metadata_right <- set_metadata$plots[[i]]$assign
            }
            vp = UpSetR:::vplayout(size_plot_height:bottom, metadata_left:metadata_right)
            pushViewport(vp)
            grid.draw(arrangeGrob(set_metadata_plots[[i]]))
            popViewport()
        }
    }
}
## Modified Make_size_plot function
Make_size_plot <- function (Set_size_data, sbar_color, ratios, ylabel, scale_sets, 
    text_scale, set_size_angle, set_size.show, set_size.scale_max, 
    set_size.number_size) {
    # Modified
    #Size_plot <- (ggplot(data = Set_size_data, aes_string(x = "x", y = "y")) + geom_bar(stat = "identity", colour = sbar_color, 
    #    width = 0.4, fill = sbar_color, position = "identity") + scale_x_continuous(limits = c(96, (nrow(Set_size_data) + 96)), breaks = c(0, max(Set_size_data)), expand = c(0, 0)) + theme(panel.background = element_rect(fill = "white"), 
    #    plot.margin = unit(c(-0.11, -1.3, 0.5, 0.5), "lines"), axis.title.x = element_text(size = 8.3 * x_axis_title_scale), axis.text.x = element_text(size = 7 * x_axis_tick_label_scale, vjust = 1, hjust = 0.5), axis.line = element_line(colour = "gray0"), axis.line.y = element_blank(), axis.line.x = element_line(colour = "gray0", size = 0.3), axis.text.y = element_blank(), axis.ticks.y = element_blank(), panel.grid.minor = element_blank(), panel.grid.major = element_blank()) + xlab(NULL) + ylab(ylabel) + coord_flip())
    Size_plot <- (ggplot(data = Set_size_data, aes_string(x = "x", y = "y")) + geom_bar(stat = "identity", colour = sbar_color, width = 0.4, fill = sbar_color, position = "identity") + scale_x_continuous(limits = c(96, (nrow(Set_size_data) + 96)), breaks = c(0, max(Set_size_data)), expand = c(0, 0)) + theme(panel.background = element_rect(fill = "white"), plot.margin = unit(c(-0.11, -1.3, 0.5, 0.5), "lines"), axis.title.x = element_text(size = 8.3 * x_axis_title_scale), axis.text.x = element_text(size = 7 * x_axis_tick_label_scale, vjust = 1, hjust = 0.5), axis.line = element_line(colour = "gray0"), axis.line.y = element_blank(), axis.line.x = element_line(colour = "gray0", size = 0.3), axis.text.y = element_blank(), axis.ticks.y = element_blank(), panel.grid.minor = element_blank(), panel.grid.major = element_blank()) + xlab(NULL) + ylab(ylabel) + coord_flip())
    Size_plot <- ggplot_gtable(ggplot_build(Size_plot))
    return(Size_plot)
}

Example Usage

Here’s an example of how to use the modified NoAttBasePlot and Make_size_plot functions:

## Load necessary libraries
library(UpSetR)

## Create a sample dataset
movies <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), 
                   header=TRUE, sep=";")

## Calculate the upset plot
upset(movies, nsets = 7, nintersects = 30, mb.ratio = c(0.5, 0.5), order.by = c("freq", "degree"), decreasing = c(TRUE,FALSE))

## Render the upset plot with right-side bars
NoAttBasePlot(Movie <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), 
                               header=TRUE, sep=";"), size_plot_height = 0.8, Main_bar_plot = Movie, Matrix_plot = Movie, hratios = c(1,2), Size_plot = Movie, query_legend = "bottom", set_metadata = NULL, set_metadata_plots = NULL, newpage = FALSE)
Make_size_plot(Movie <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), 
                                header=TRUE, sep=";"), sbar_color = "black", ratios = c(1.2, 1.5), ylabel = "Movie Frequency", scale_sets = "identity", text_scale = c(1.2, 1.5), set_size_angle = pi/4, set_size.show = TRUE, set_size.scale_max = NULL, set_size.number_size = 0.8)

By modifying the NoAttBasePlot and Make_size_plot functions, we can customize the appearance of the upset plot to display horizontal bars on the right side instead of the left side.

Conclusion

In this article, we explored how to modify the UpSetR package to create an upset plot with horizontal bars on the right side. We modified the NoAttBasePlot and Make_size_plot functions to achieve this custom layout. The resulting plot provides a more intuitive way of comparing multiple sets and can be useful in various applications such as data analysis, visualization, and scientific research.

References


Last modified on 2024-07-04