#venn 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"                
## [2] "C:\\Users\\liyix\\OneDrive\\Desktop\\鏂板缓鏂囨湰鏂囨。.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
library(VennDiagram)
## Loading required package: grid
## Loading required package: futile.logger
out_dir <- dir_path
p <- venn.diagram(list("AL inhibitor" = unique(na.omit(data_3$Mapping.ID[data_3$A == "AL inhibitor"])),
                  "AL non-inhibitor" = unique(na.omit(data_3$Mapping.ID[data_3$A == "AL non-inhibitor"])),
                  "BB inhibitor" = unique(na.omit(data_3$Mapping.ID[data_3$B == "BB inhibitor"])),
                  "BB non-inhibitor" = unique(na.omit(data_3$Mapping.ID[data_3$B == "BB non-inhibitor"]))),
             filename = paste0(out_dir, Sys.Date(),"_VENN_PLOT",".tiff"),
             counts = TRUE,lwd = 1.5,fontfamily = "sans",cat.fontfamily = "sans",
             cat.col = c("darkblue", "darkred", "#e6b800", "darkgreen"),
             col = c("darkblue", "darkred", "#e6b800", "darkgreen"),
             fill = NULL,
             alpha = 0.5,cat.fontface = 1,lty =1,margin = 0.05,
             cex = 1,height = 2500, width = 2500,
             fontface =1)
#########################################################output data
library(gplots)
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
x <- list("AL inhibitor" = unique(na.omit(data_3$Mapping.ID[data_3$A == "AL inhibitor"])),
          "AL non-inhibitor" = unique(na.omit(data_3$Mapping.ID[data_3$A == "AL non-inhibitor"])),
          "BB inhibitor" = unique(na.omit(data_3$Mapping.ID[data_3$B == "BB inhibitor"])),
          "BB non-inhibitor" = unique(na.omit(data_3$Mapping.ID[data_3$B == "BB non-inhibitor"])))
y  <- x
ItemsList <- gplots::venn(x, show.plot = T,small=0.7)

ItemsList_1 <- attr(ItemsList, "intersections")
length(ItemsList_1) #[1] 4
## [1] 4
names(ItemsList_1)
## [1] "AL inhibitor:BB inhibitor"         "AL inhibitor:BB non-inhibitor"    
## [3] "AL non-inhibitor:BB inhibitor"     "AL non-inhibitor:BB non-inhibitor"
for (i in 1:length(ItemsList_1)) {
  #i =2
  print(gsub(" \n ","-",names(ItemsList_1)[i]))
  data_1 <- ItemsList_1[[names(ItemsList_1)[i]]]
  data_2 <- data.frame(data_1)
  data_2$source <- paste0(gsub(":","_",gsub(" \n ","-",names(ItemsList_1)[i])),"-",length(data_1))
  write.csv(data_2,paste0(dir_path,Sys.Date(),"_",gsub(":","_",gsub(" \n ","-",names(ItemsList_1)[i])),"-",length(data_1),".csv"),row.names = F)
}
## [1] "AL inhibitor:BB inhibitor"
## [1] "AL inhibitor:BB non-inhibitor"
## [1] "AL non-inhibitor:BB inhibitor"
## [1] "AL non-inhibitor:BB non-inhibitor"
#####################method_2
#five sets Venn diagrams
#install.packages("venn")
library(venn)
## 
## Attaching package: 'venn'
## The following object is masked from 'package:gplots':
## 
##     venn
library(ggplot2)
set.seed(12345)
x <- as.data.frame(matrix(sample(0:1, 100, replace = TRUE), ncol = 5))
x 
##    V1 V2 V3 V4 V5
## 1   1  1  1  0  1
## 2   0  0  1  1  0
## 3   1  1  1  1  0
## 4   1  1  1  0  1
## 5   1  0  0  0  0
## 6   1  1  1  0  0
## 7   1  1  0  0  0
## 8   0  1  1  0  1
## 9   0  1  0  0  0
## 10  1  1  1  1  0
## 11  1  0  1  1  0
## 12  1  0  0  0  0
## 13  1  1  1  0  1
## 14  0  0  0  1  0
## 15  1  1  0  0  1
## 16  0  1  0  1  1
## 17  1  1  0  1  1
## 18  1  0  0  1  0
## 19  0  1  0  0  1
## 20  1  1  0  0  1
venn::venn(x,ellipse = F,ilab=TRUE, zcolor = "style")

venn::venn(y,
           color = c("darkblue", "darkred", "#e6b800", "darkgreen"),box = F, size = 0.5,
           ggplot = T) +
  annotate(geom="text", x= 500, y= 900, label= "venn package",
           color="black") 

ggsave(filename = paste0(Sys.Date(),"-venn package",".tif"), 
       plot = last_plot(), 
       device = "tiff", path = out_dir,
       scale = 1, compression = "lzw", width = 8, height = 8, units = "cm",
       dpi = 300, limitsize = TRUE)

###################### sample five-set Venn Diagram
A <- sample(1:1000, 400, replace = FALSE)
B <- sample(1:1000, 600, replace = FALSE)
C <- sample(1:1000, 350, replace = FALSE)
D <- sample(1:1000, 550, replace = FALSE)
E <- sample(1:1000, 375, replace = FALSE)
venn.plot <- venn.diagram(
  x = list(
    A = A,
    B = B,
    C = C,
    D = D,
    E = E
  ),
  filename = "Venn_5set_pretty.tiff",
  col = "black",
  fill = c("dodgerblue", "goldenrod1", "darkorange1", "seagreen3", "orchid3"),
  alpha = 0.50,
  cex = c(1.5, 1.5, 1.5, 1.5, 1.5, 1, 0.8, 1, 0.8, 1, 0.8, 1, 0.8,
          1, 0.8, 1, 0.55, 1, 0.55, 1, 0.55, 1, 0.55, 1, 0.55, 1, 1, 1, 1, 1, 1.5),
  cat.col = c("dodgerblue", "goldenrod1", "darkorange1", "seagreen3", "orchid3"),
  cat.cex = 1.5,
  cat.fontface = "bold",
  margin = 0.05
)