Preparing data for UpSet Plot in R

As i have multiple comparison so I m trying this UpSet plot, but it needs data to be formatted in particular way, one of the ways is as a list, so now the issue is since between one comparison the no of genes coming out may not be same as the other one, so there is a difference in dimension so if i bind them as data frame then i get dimension error, so the best way to get through is make list so what i have tried so far

dim(A)
dim(B)
ONE <- A[c(1)]#############just taking out the gene column########
TWO <- B[c(1)]#############just taking out the gene column########
THREE <- B[c(1)]#############just taking out the gene column########
dim(ONE)
dim(TWO)
b1 <- apply(ONE,2,list)
b2 <- apply(TWO,2,list)

Now this is the code from their help page

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

so the data "movies" class is data frame, there is not dimension issue as such as in my case if i want to intersect two comparison which may not have the same length, so how do i overcome the issue any suggestion or help would be highly appreciated. Update my question with one more data to add.

Don't you just need a binary matrix with presence/absence data for all of the genes?

library(dplyr)
combined <- full_join(data.frame(gene=ONE, GMP_MONO=1),
data.frame(gene=TWO, CMP=1))
combined[is.na(combined)] <- 0

Edit: To loop over a large number of data frames, the easiest solution is to use the reduce function from the purrr package:

combined <- reduce(list(data.frame(gene=c('gene1', 'gene2', 'gene3'), set1=1),
data.frame(gene=c('gene3', 'gene4', 'gene5'), set2=1),
data.frame(gene=c('gene1', 'gene4'), set3=1)
), full_join)
combined[is.na(combined)] <- 0
combined
#    gene set1 set2 set3
# 1 gene1    1    0    1
# 2 gene2    1    0    0
# 3 gene3    1    1    0
# 4 gene4    0    1    1
# 5 gene5    0    1    0
• Im getting NA in the CMP column in my combined data-frame after doing full_join , im getting 0 as well but not sure why NA..
– kcm
May 31 '18 at 5:19
• Oh yeah. I forgot about that. See updated answer May 31 '18 at 5:51
• yeah now its working but i do see error when i try to do more than ,which is apparently wont happen with full_join ,lets say i do want to intersect , more than three data frame and make a binary matrix can you suggest what more to add to your code?
– kcm
May 31 '18 at 6:40
• see updated answer May 31 '18 at 7:33

We can plot using lists as well, using fromList function, see example:

library(UpSetR)

myGeneSets <- list(
set1 = c("gene1","gene2","gene3"),
set2 = c("gene1","gene4","gene5", "gene7"),
set3 = c("gene1","gene7")
)

upset(fromList(myGeneSets))

fromList

A function to convert a list of named vectors to a data frame compatible with UpSetR.

fromList(myGeneSets)
#   set1 set2 set3
# 1    1    1    1
# 2    1    0    0
# 3    1    0    0
# 4    0    1    0
# 5    0    1    0
# 6    0    1    1
• another way to do the stuff cool ..i will use this as well
– kcm
Jun 4 '18 at 17:05

You need to set a common number of rows, one for each gene (make sure that the gene identifiers are the same for both files). Then in the first column you can set 1 for the file A, and 0 otherwise. In the second column you can do the same. Then you add this dataframe as the input of the upset function.

genes <- unique(c(ONE, TWO))
df <- data.frame(A = ifelse(genes %in% A, 1, 0), B = ifelse(genes %in% b, 1, 0))
upset(df, ...)
• Your solution does work but it looks like what ever intersection it does it gives only one common element ..im not sure what is wrong..
– kcm
May 31 '18 at 6:56
• Why would you expect more than one element? Could you share the data of ONE and TWO? Otherwise could you do length(intersect(ONE, TWO))?
– llrs
May 31 '18 at 7:02
• as I have multiple comparison between lineage type , the ONE ,TWO and now which i added later THREE they are just vector containing my gene list ,so im using only gene list ..
– kcm
May 31 '18 at 7:14
• Then you need to add a third column and genes should be made with genes <- unique(c(ONE, TWO, THREE)), could you post your data
– llrs
May 31 '18 at 7:22
• I will post the data ,update the question with my data.
– kcm
May 31 '18 at 10:02