0

I have a number of data frames I would like to create UpSet Plots for in R and write them to an external folder. I am using similar code that I am successfully using to create and save scatterplots to an external folder.

Below are two example data frames as well as my attempt at a lapply function to complete the task.

library(dplyr)
library(UpSetR)
df <- data.frame("ID" = 1:16)
        df$VarA <- c(1,1,1,1,1,1,1,1,1,1,1,14,NA_real_,NA_real_,NA_real_,16)
        df$VarB <- c(10,0,0,0,12,12,12,12,0,14,NA_real_,14,16,16,16,16)
        df$VarC <- c(10,12,14,16,10,12,14,16,10,12,14,16,10,12,14,16)
        df$VarD <- c(10,12,14,16,10,12,14,16,10,12,14,16,10,12,14,16)
        df$ControlVarA <- factor(c("Group_1","Group_1","Group_1","Group_1","Group_1", "Group_1",
                                   "Group_2","Group_2","Group_2","Group_2","Group_2","Group_2",
                                   "Group_2","Group_2","Group_2","Group_2"))
    df2 <- data.frame("ID" = 1:26)
    df2$VarA <- c(1,1,1,1,1,1,1,1,1,1,1,14,NA_real_,NA_real_,NA_real_,16,16,16,16,16,16,16,16,16,16,16)
    df2$VarB <- c(10,0,0,0,12,12,12,12,0,14,NA_real_,14,16,16,16,16,16,16,16,16,16,16,16,16,16,16)
    df2$VarC <- c(10,12,14,16,10,12,14,16,10,12,14,16,10,12,14,16,16,16,16,16,16,16,16,16,16,16)
    df2$VarD <- c(10,12,14,16,10,12,14,16,10,12,14,16,10,12,14,16,16,16,16,16,16,16,16,16,16,16)
    df2$ControlVarA <- factor(c("Group_1","Group_1","Group_1","Group_1","Group_1", "Group_1",
                               "Group_2","Group_2","Group_2","Group_2","Group_2","Group_2",
                               "Group_2","Group_2","Group_2","Group_2","Group_2","Group_2",
                               "Group_2","Group_2","Group_2","Group_2","Group_2","Group_2","Group_2","Group_2")) 
    
filter_df_names <- list(c("df", "df2"))

lapply(filter_df_names, function(x) {
  
  missupset <- x %>% 
    select(starts_with("Var")) %>% 
    rename_with(~ sub("Var_", "", .x)) %>% 
    mutate(across(everything(), ~if_else(!is.na(.), 1, 0))) %>% 
    as.data.frame()
  
  jpeg(filename=sprintf('C:\\Images\\%s.jpeg', x))
  upset(missupset,
        nsets = 3,
        nintersects = 2,
        order.by = "freq",
        sets.bar.color = "#c7c0cb",
        main.bar.color = "#442040",
        matrix.color = "#442040")
  dev.off()
})
user438383
  • 5,716
  • 8
  • 28
  • 43
chipsin
  • 643
  • 1
  • 4
  • 10
  • 2
    you are just putting the names into the list, so when you use `x` in the function, it's just a string, not a data.frame. You can either put the data itself in a list `filter_df_names <- list(df, df2)` and pass names separately to the function, or use `missupset <- get(x) %>% ...` to get the data from it's string name. Using a strategy with data in the list rather than using `get()` is better long term and makes most things easier. – MrFlick Jul 14 '21 at 06:37
  • This is very helpful. Thanks MrFlick. If I already have already extracted the names of the data frames, and have those names in a string, is there a convenient way to use this list of names to create a list of data frames. I know this can be done easily by removing the quotation marks, but the lists are quite long and I am looking at automating this processing. – chipsin Jul 14 '21 at 06:49
  • 1
    Take a look at mget() – MrFlick Jul 14 '21 at 07:45
  • 3
    As @MrFlick has said, your fundamental issue is the creation of the data frames themselves. Rather than creating standalone data frame objects, put them in a list to start with. Instead of `df <- ...` and `df2 <- ...` etc, use `dfList[[1]] <- ...` and `dfList[[2]] <- ...` and so on. – Limey Jul 14 '21 at 07:46
  • Thanks @MrFlick, this was exactly what I was looking for – chipsin Jul 14 '21 at 08:29

0 Answers0