user142632
user142632

Reputation: 61

Colouring of the main bars in upset plot

I have a dataset consisting of 7 columns with values of 0 and 1 in r. The data has been defined at the last column into different groups based on the value. I want to colour the bars as per my group.I have 17 different combinations but I don't want the colouring based on combinations. I want to colour based on my group column but represent the combination in upset plot.

data <- data.frame(
    x1 = c(0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0),
     x2= c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
    x3 = c(0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
    x4 = c(0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0),
    x5 = c(0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0),
    x6 = c(0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0),
    x7 = c(0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0))

    data1 <- data %>% 
        mutate(numeric=rowSums(.[c("x1", "x2", "x3", "x4", "x5", "x6", "x7")])) %>% 
        mutate(group=case_when(
            x1==1 & x2==1 & x3==1 ~ "A",
            x1==1 & x2==1 ~ "B",
            x1=1 & x3==1 ~ "C",
            numeric ==1 ~ "D",
            numeric >=2 ~ "E",
            TRUE ~ "F"))
    color <- c("A"="red", "B"="orange", "C"="#228822", "D"="#00BFFF", "E"="yellow", 
    "F"=="gray")
    upset(data1, sets=c("x1", "x2", "x3", "x4", "x5", "x6", "x7"),
                group.by = "group",
                main.bar.color = color)


Reproducible data

data <- structure(list(X1 = c(0, 1, 0, 1, 0, 0, 0, 0, 0, 0), X2 = c(1,1, 1,
 1, 1, 1, 1, 1, 1, 1), X3 = c(0, 1, 0, 1, 0, 0, 0, 0, 0,0), X4 = c(0, 1, 0,
 1, 0, 0, 0, 0, 0, 0), X5 = c(0, 1, 1, 1,0, 0, 0, 0, 0, 1), X6 = c(0, 1, 0,
 0, 0, 0, 0, 0, 0, 0), X7 = c(0,0, 0, 0, 0, 0, 0, 0, 0, 0), X8 = c(0, 0, 0,
 0, 0, 0, 0, 0, 0,0), X9 = c(1, 1, 0, 0, 0, 1, 1, 1, 1, 1), X10 = c(0, 0, 0,
 0,0, 0, 0, 0, 0, 0), X11 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X12 = c(0,0, 0,
 0, 0, 0, 0, 0, 0, 0), group = c("C", "D", "C", "A", "B","C", "C", "C", "C",
 "C")), row.names = c(NA, -10L), class = c("tbl_df","tbl", "data.frame"))

Upvotes: 2

Views: 672

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173878

The ordering of the columns is a little obscure, but tracing through the code I have discovered that you can recreate it like this:

color <- c(A = "red", B = "orange", C = "#228822", D = "#00BFFF", 
           E = "yellow", F = "gray")

d <- as.data.frame(plyr::count(data1[, c(order(-colSums(data1[1:7])), 9)]))[-1,]

correct_cols <- color[d$group]

Allowing:

upset(data1, sets = c("x1", "x2", "x3", "x4", "x5", "x6", "x7"),
      group.by = "group", main.bar.color = correct_cols)

enter image description here


Edit

With the OP's actual data we can do:

data1 <- data %>%
  rowwise() %>%
  mutate(numeric = sum(c_across(X1:X12)), .before = group) %>%
  as.data.frame()

color <- c(A = "red", B = "orange", C = "#228822", D = "#00BFFF")

d <- as.data.frame(plyr::count(data1[, c(order(-colSums(data1[1:12])), 14)]))
correct_cols <- color[d$group]

UpSetR::upset(data1, sets = paste0("X", 1:12),
      group.by = "group", main.bar.color = correct_cols)

enter image description here

Upvotes: 4

Related Questions