Use a color palette for matrix points in UpSetR

2.8k views Asked by At

Below I've constructed an Upset plot. I'm using a palette of colors to define the bar colors. Is there a way to do this for the matrix of connected dots as well?

library(dplyr)
library(RColorBrewer)
library(UpSetR)

movies <- read.csv(system.file("extdata", "movies.csv",
                   package = "UpSetR"), header=T, sep=";" )
movies <- select(movies, Action:Children)

upset(movies, main.bar.color=brewer.pal(2^ncol(movies)-1, "Set1"))

enter image description here

When attempting to apply the palette to the matrix, I get warnings and only the first color, red, is used.

upset(movies, main.bar.color=brewer.pal(2^ncol(movies)-1, "Set1"),
      matrix.color=brewer.pal(2^ncol(movies)-1, "Set1"))

enter image description here

1

There are 1 answers

0
Marco Sandri On

upset allows to specify only one color for matrix.color.
A solution is to modify the UpSetR:::Create_layout function:

Create_layout <- function (setup, mat_color, mat_col, matrix_dot_alpha) 
{
    Matrix_layout <- expand.grid(y = seq(nrow(setup)), x = seq(ncol(setup)))
    Matrix_layout <- data.frame(Matrix_layout, value = as.vector(setup))
    for (i in 1:nrow(Matrix_layout)) {
        if (Matrix_layout$value[i] > as.integer(0)) {
            # Here I propose to change Matrix_layout$color[i] <- mat_color with
            # Matrix_layout$color[i] <- mat_color[i]
            Matrix_layout$color[i] <- mat_color[i]
            Matrix_layout$alpha[i] <- 1
            Matrix_layout$Intersection[i] <- paste(Matrix_layout$x[i], 
                "yes", sep = "")
        }
        else {
            Matrix_layout$color[i] <- "gray83"
            Matrix_layout$alpha[i] <- matrix_dot_alpha
            Matrix_layout$Intersection[i] <- paste(i, "No", sep = "")
        }
    }
    if (is.null(mat_col) == F) {
        for (i in 1:nrow(mat_col)) {
            mat_x <- mat_col$x[i]
            mat_color <- as.character(mat_col$color[i])
            for (i in 1:nrow(Matrix_layout)) {
                if ((Matrix_layout$x[i] == mat_x) && (Matrix_layout$value[i] != 
                  0)) {
                  Matrix_layout$color[i] <- mat_color
                }
            }
        }
    }
    return(Matrix_layout)
}

# Replace Create_layout in UpSetR with the modified function
assignInNamespace(x="Create_layout", value=Create_layout, ns="UpSetR")

# Now you can set colors for the matrix of connected dots
# The dimension of this matrix is 3 x 7
upset(movies, main.bar.color=brewer.pal(2^ncol(movies)-1, "Set1"),
      matrix.color=rainbow(21))

enter image description here