#!/share/work/biosoft/R/latest/bin/Rscript
args <- commandArgs(trailingOnly = TRUE)
library(venn)
library(VennDiagram)
library(purrr)
# 读取命令行参数
input_file <- args[1]
output_prefix <- args[2]
# 读取数据文件, 不转换列名
venn_dat <- read.delim(input_file, check.names = FALSE)
# 从文件中创建一个列表
venn_list <- list(venn_dat[, 1], venn_dat[, 2], venn_dat[, 3], venn_dat[, 4], venn_dat[, 5], venn_dat[, 6])
# 把列名赋值给列表的key值
names(venn_list) <- colnames(venn_dat[1:6])
# 删除列表中每个向量中的NA
venn_list <- purrr::map(venn_list, na.omit)
# 创建png设备
png(paste0(output_prefix, "_venn.png"), h = 3000, w = 3000, res = 300)
# 作图
venn(venn_list, zcolor = "style", opacity = 0.3, box = F, ilcs = 0.5, sncs = 1)
# 关闭png设备
dev.off()
# 创建pdf设备
pdf(paste0(output_prefix, "_venn.pdf"), h = 10, w = 10)
# 作图
venn(venn_list, zcolor = "style", opacity = 0.3, box = F, ilcs = 0.5, sncs = 1)
# 关闭pdf设备
dev.off()
# 导出结果
inter <- get.venn.partitions(venn_list)
for (i in 1:nrow(inter)) inter[i, "values"] <- paste(inter[[i, "..values.."]], collapse = "|")
inter <- subset(inter, select = -..values..)
inter <- subset(inter, select = -..set..)
write.table(inter, paste0(output_prefix, "_result.csv"), row.names = FALSE, sep = ",", quote = FALSE)
我的数据是6组,一列一组,,列名为组名,修改成这样就可以直接用