#upset plot
rm(list = ls())
#############################################################1.1  input data 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- dir(dir_path,pattern = "*.txt",full.names = T)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\AB.txt"
data_1 <- read.delim(grep("AB.txt",dir_path_name,value = T),header = T,stringsAsFactors = F)
head(data_1)
##       Mapping.ID A B
## 1 OIRAEJWYWSAQNG 0 0
## 2 ZZQMUJGZCZTLQD 0 0
## 3 TWVUMMQUXMYOOH 0 0
## 4 GEDPQKGMHLHFOZ 0 0
## 5 GUIBJJJLGSYNKE 0 0
## 6 TUOSYWCFRFNJBS 0 0
dim(data_1) #[1] 1818    3
## [1] 1818    3
#A
data_1$A[data_1$A == 1] <- "AL inhibitor"
data_1$A[data_1$A == 0] <- "AL non-inhibitor"
##B
data_1$B[data_1$B == 1] <- "BB inhibitor"
data_1$B[data_1$B == 0] <- "BB non-inhibitor"
########################
data_3 <- data_1
head(data_3)
##       Mapping.ID                A                B
## 1 OIRAEJWYWSAQNG AL non-inhibitor BB non-inhibitor
## 2 ZZQMUJGZCZTLQD AL non-inhibitor BB non-inhibitor
## 3 TWVUMMQUXMYOOH AL non-inhibitor BB non-inhibitor
## 4 GEDPQKGMHLHFOZ AL non-inhibitor BB non-inhibitor
## 5 GUIBJJJLGSYNKE AL non-inhibitor BB non-inhibitor
## 6 TUOSYWCFRFNJBS AL non-inhibitor BB non-inhibitor
for (i in 1:nrow(data_3)) {
  #i = 2
  data_3$label[i] <- list(as.character(data_3[i,2:3])[complete.cases(as.character(data_3[i,2:3]))])
}

class(data_3) #[1] "data.frame"
## [1] "data.frame"
class(data_3$label) #[1] "list"
## [1] "list"
library(ggplot2)
library(ggupset)
p1 <- ggplot(data_3,aes(x=label)) +
  geom_bar(fill = "#004c99",width = 0.8) +
  labs(title = glue::glue(""),subtitle = "",
       x = NULL, y = "Intersection size")+
  scale_x_upset(order_by = "freq",expand = c(0.04, 0.04),n_intersections = 20,reverse = F) +
  scale_y_continuous(expand = c(0.01, 0.01)) +
  axis_combmatrix(levels = c("AL inhibitor","AL non-inhibitor","BB inhibitor","BB non-inhibitor"))+
  theme_combmatrix(text = element_text(family = "Roboto Condensed", 
                                       color = "black",face="plain"),
                   plot.title = element_text(family="sans",size = 20,
                                             color = "#a70042",face="plain"),
                   plot.subtitle = element_text(family="sans",size = 20,
                                                color = "black",face="plain"),
                   axis.title.x = element_text(family="sans", size = 20,
                                               color = "black",face="plain"), 
                   axis.title.y = element_text(family="sans", size = 20,
                                               color = "black",vjust = -20,face="plain"),
                   axis.text.x = element_text(family="sans", size = 12,
                                              color = "black",face="plain"),
                   axis.text.y = element_text(family="sans", size = 20,
                                              color = "black",face="plain"),
                   panel.background = element_rect(fill = "white"),
                   combmatrix.panel.point.size = 3,
                   combmatrix.panel.point.color.fill = "black",
                   combmatrix.panel.line.color = "black",
                   panel.grid = element_line(color = "black"),
                   panel.grid.major.x = element_blank(),
                   axis.ticks = element_blank())
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
##004c99
p1

ggsave(filename = paste0(Sys.Date(),"-upset.tif"), plot = p1, 
       device = "tiff", path = dir_path,
       scale = 1, width = 15, height = 15, units = "cm",
       dpi = 300, limitsize = TRUE, compression = "lzw")