使用R进行VENN_维恩图或韦恩图_的绘制_2020-11-08

## 1.导入R包

  library("VennDiagram")

  library("rio")

## 2.设置当前工作目录

  if (length(path) != 0) {

    setwd(path)

  } else {

    setwd("./")

  }

## 3.读取提前准备好的数据

  venn_data_index <- list.files(path = "./", pattern = "^venn_data")

  venn_data <- import(venn_data_index)

## 4.韦恩图数据集大小判断及绘图

  if (length(colnames(venn_data)) == 2){

    venn_list <- list(venn_data[,1], venn_data[,2])

    names(venn_list) <- colnames(venn_data)

    venn.diagram(venn_list, filename = paste0("venn_two_sets_plot.",imagetype), imagetype = imagetype, margin = 0.1, fill = c('red', 'blue'), alpha = 0.50, col = 'black', cex = 1, fontfamily = 'serif', cat.col = c('black', 'black'), cat.cex = 1, cat.fontfamily = 'serif', cat.default.pos = "outer")

    print("====================================================================")

    print("两组的数据的venn图绘制完成!")

    print("====================================================================")

  } else if (length(colnames(venn_data)) == 3){

    venn_list <- list(venn_data[,1], venn_data[,2], venn_data[,3])

    names(venn_list) <- colnames(venn_data)

    venn.diagram(venn_list, filename = paste0("venn_three_sets_plot.",imagetype), imagetype = imagetype, margin = 0.1, fill = c('red', 'blue', 'green'), alpha = 0.50, col = 'black', cex = 1, fontfamily = 'serif', label.col = c("darkred", "white", "darkblue", "white", "white", "white", "darkgreen"), col = "transparent", cat.cex = 1, cat.fontfamily = 'serif', cat.default.pos = "text", cat.pos = 0)

    print("====================================================================")

    print("三组的数据的venn图绘制完成!")

    print("====================================================================")

  } else if (length(colnames(venn_data)) == 4){

    venn_list <- list(venn_data[,1], venn_data[,2], venn_data[,3], venn_data[,4])

    names(venn_list) <- colnames(venn_data)

    venn.diagram(venn_list, filename = paste0("Venn_four_sets_plot.",imagetype), imagetype = imagetype, col = "black", lty = "dotted", lwd = 3, fill = c("cornflowerblue", "green", "yellow", "darkorchid1"), alpha = 0.50, label.col = c("orange", "white", "darkorchid4", "white", "white", "white", "white", "white", "darkblue", "white", "white", "white", "white", "darkgreen", "white"), cex = 2.0, fontfamily = "serif", fontface = "bold", cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"), cat.cex = 1.8, cat.fontface = "bold", cat.fontfamily = "serif")

    print("====================================================================")

    print("四组的数据的venn图绘制完成!")

    print("====================================================================")

  } else if (length(colnames(venn_data)) == 5){

    venn_list <- list(venn_data[,1], venn_data[,2], venn_data[,3], venn_data[,4], venn_data[,5])

    names(venn_list) <- colnames(venn_data)

    venn.diagram(venn_list, filename = paste0("venn_five_sets_plot.",imagetype), imagetype = imagetype, lty = "dotted", lwd = 2, col = "black", fill = c("dodgerblue", "goldenrod1", "darkorange1", "seagreen3", "orchid3"), alpha = 0.60, cat.col = c("dodgerblue", "goldenrod1", "darkorange1", "seagreen3", "orchid3"), cat.cex = 0.8, cat.fontface = "bold", margin = 0.07, cex = 0.8)

    print("====================================================================")

    print("五组的数据的venn图绘制完成!")

    print("====================================================================")

  }

  ## 5.打印绘制韦恩图所需要的R包版本信息和环境变量

  sessionInfo()

  ## 6.结果展示


## 7.说明

颜色和数据都是可以自己根据需要进行调整的,然后就是可以绘制的数据集的限制,此维恩图包只支持绘制5个数据集,如果超出五个就需要用其他工具进行可视化分析了,下期介绍如何使用R绘制花瓣图!此博文如对您有所帮助,请点赞加关注,有问题欢迎讨论!

你可能感兴趣的:(使用R进行VENN_维恩图或韦恩图_的绘制_2020-11-08)