Reputation: 675
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()
})
Upvotes: 0
Views: 281