Python R
汇总一下有关Venn Diagram(韦恩图) 的绘制方法,主要内容包括:

  • Venn Diagram(韦恩图)的简介
  • Venn Diagram(韦恩图)的R绘制方法
  • Venn Diagram(韦恩图)的Python绘制方法

    Venn Diagram(韦恩图)的简介

    Venn Diagram(韦恩图),或叫Venn图、文氏图、温氏图,是在所谓的集合论(或者类的理论)数学分支中表示集合或者类的一种草图,主要用于显示元素集合重叠区域的图示,如下图所示:
    2021-05-31-10-36-43-088207.png
    Venn Diagram Example
    那么,如何使用R或者Python实现Venn Diagram(韦恩图)的高效绘制呢?首先介绍R绘制的方法。

    Venn Diagram(韦恩图)的R绘制方法

    R-ggvenn包绘制

    使用R绘制Venn Diagram图,首先想到的肯定是ggplot2,而ggvenn包作为ggplot2的拓展包且有geom_*(),这里就最先介绍。ggvenn包主要使用ggvenn()函数和geom_venn()绘图函数(ggplot2图层语法类似)绘制。官网:https://github.com/yanlinlin82/ggvenn
    首先介绍下其主要的参数设置:
    1. For filling:

  • fill_color(填充颜色):默认是 c(“blue”, “yellow”, “green”, “red”)

  • fill_alpha(透明度):默认是0.5
  1. For stroke:
  • stroke_color(线条颜色):默认是”black”
  • stroke_alpha(线条透明度):默认是1
  • stroke_size(线条宽度):默认是1
  • stroke_linetype:默认是”solid”
  1. For set name:
  • set_name_color(文本名颜色):默认是”black”
  • set_name_size(文本名大小):默认是6
  1. For text:
  • text_color(文本颜色):默认是”black”
  • text_size(文本大小):默认是4.

以上各参数为ggvenn()geom_venn()绘图函数的共同参数,其他额外参数可自行查阅~
接下来,结合实例进行解释说明~
「样例一」:ggvenn()函数绘制

  1. # 样例数据
  2. a <- list(`Set 1` = c(1, 3, 5, 7, 9),
  3. `Set 2` = c(1, 5, 9, 13),
  4. `Set 3` = c(1, 2, 8, 9),
  5. `Set 4` = c(6, 7, 10, 12))
  6. #可视化绘制
  7. opar <- par(family = "Roboto Condensed")
  8. ggvenn(a,fill_color=mypal,fill_alpha = .7,stroke_linetype = "longdash",set_name_size = 8,
  9. text_size=5)

2021-05-31-10-36-43-254782.png
Example Of ggvenn()
「样例二」:geom_venn()函数绘制

  1. # 生成样例数据
  2. d <- tibble(value = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
  3. `Set 1` = c(T, F, T, T, F, T, F, T, F, F, F),
  4. `Set 2` = c(T, F, F, T, F, F, F, T, F, F, T),
  5. `Set 3` = c(T, T, F, F, F, F, T, T, F, F, F),
  6. `Set 4` = c(F, F, F, F, T, T, F, F, T, T, F))
  7. # 可视化绘制
  8. ggvenn_4 <- ggplot(d, aes(A = `Set 1`, B = `Set 2`, C = `Set 3`, D = `Set 4`)) +
  9. geom_venn(fill_color=mypal,fill_alpha = .7,stroke_linetype = "longdash",set_name_size = 8,
  10. text_size=5) +
  11. theme_void()+
  12. coord_fixed() +
  13. labs(title = "Example of ggvenn:: geom_venn function",
  14. subtitle = "processed charts with geom_venn()",
  15. caption = "Visualization by DataCharm") +
  16. theme(plot.title = element_text(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  17. size = 20, margin = margin(t = 1, b = 12)),
  18. plot.subtitle = element_text(hjust = 0,vjust = .5,size=15),
  19. plot.caption = element_text(face = 'bold',size = 12))

2021-05-31-10-36-43-360478.png
Example Of geom_venn()
这里分别使用了ggvenn()geom_venn() 函数绘制了韦恩图,更多细节,可参考官网进行理解~

R-ggVennDiagram包绘制

R-ggVennDiagram包和ggvenn包一样也是ggplot2的拓展包,其可以支持2~7维的韦恩图绘制,这里直接通过使用ggVennDiagram()绘制韦恩图进行解释。
「样例」:

  1. library(ggVennDiagram)
  2. # 样例数据
  3. genes <- paste("gene",1:1000,sep="")
  4. set.seed(20210419)
  5. x <- list(A=sample(genes,300),
  6. B=sample(genes,525),
  7. C=sample(genes,440),
  8. D=sample(genes,350))
  9. # 可视化绘制
  10. library(ggplot2)
  11. ggVennDiagram(x, category.names = c("Stage 1","Stage 2","Stage 3", "Stage4"),
  12. size=1,lty="longdash",color="gray60") +
  13. scale_fill_gradient(name="Count",low="#EC7D85",high = "#182F6F") +
  14. hrbrthemes::theme_ipsum(base_family = "sans") +
  15. labs(title = "Example of ggVennDiagram:: ggVennDiagram function",
  16. subtitle = "processed charts with ggVennDiagram()",
  17. caption = "Visualization by DataCharm") +
  18. theme(plot.title = element_text(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  19. size = 20, margin = margin(t = 1, b = 12)),
  20. plot.subtitle = element_text(hjust = 0,vjust = .5,size=15),
  21. plot.caption = element_text(face = 'bold',size = 12),
  22. axis.text.x = element_blank(),
  23. axis.text.y = element_blank(),
  24. axis.title.x = element_blank(),
  25. axis.title.y = element_blank())

2021-05-31-10-36-43-529026.png
Example of ggVennDiagram

Venn Diagram(韦恩图)的Python绘制方法

要想使用Python绘制韦恩图(这里主要介绍基于matplotlib的绘制方法,交互式后期统一介绍),这里介绍一个非常便捷的方法-matplotlib-venn 绘制。安装方式如下:

「安装」

  1. easy_install matplotlib-venn

「官网」

https://github.com/konstantint/matplotlib-venn

「样例一」:两个集合

  1. from matplotlib_venn import venn2, venn2_circles, venn2_unweighted
  2. from matplotlib_venn import venn3, venn3_circles
  3. import matplotlib.pyplot as plt
  4. # 样例数据
  5. Group1 = ['a','b','c','e','f','g','i','p','q']
  6. Group2 = ['b','e','f','h','k','q','r','s','t','u','v','z']
  7. Group3 = ['c','e','g','h','j','k','o','q','z']
  8. #可视化绘制
  9. plt.rcParams['font.family'] = ["Times New Roman"]
  10. fig, ax = plt.subplots(figsize=(5,3),dpi=110)
  11. vee2 = venn2([set(Group1), set(Group2)],set_labels=("Group1", "Group2"),
  12. set_colors=("#0073C2FF", "#EFC000FF"), alpha = 0.8,ax=ax)
  13. venn2_circles([set(Group1), set(Group2)], linestyle="--", linewidth=2, color="black",ax=ax)
  14. # 定制化设置:设置字体属性
  15. for text in vee2.set_labels:
  16. text.set_fontsize(15);
  17. for text in vee2.subset_labels:
  18. text.set_fontsize(16)
  19. text.set_fontweight("bold")
  20. ax.text(.8,-.1,'\nVisualization by DataCharm',transform = ax.transAxes,
  21. ha='center', va='center',fontsize = 8,color='black')
  22. plt.title("Example Of venn2() and venn2_circles()",size=15,fontweight="bold",
  23. backgroundcolor="#868686FF",color="black",style="italic")

2021-05-31-10-36-43-688409.png
Example Of venn2()

「样例二」:三个集合

  1. fig, ax = plt.subplots(figsize=(5,3),dpi=110)
  2. vd3=venn3([set(Group1),set(Group2),set(Group3)],
  3. set_labels=("Group1","Group2","Group3"),
  4. set_colors=('#0073C2FF','#EFC000FF',"#CD534CFF"),
  5. alpha = 0.8,ax=ax)
  6. venn3_circles([set(Group1), set(Group2),set(Group3)], linestyle="--", linewidth=2, color="black",ax=ax)
  7. # 定制化设置
  8. for text in vd3.set_labels:
  9. text.set_fontsize(15);
  10. text.set_fontweight("bold")
  11. for text in vd3.subset_labels:
  12. text.set_fontsize(15)
  13. text.set_fontweight("bold")
  14. ax.text(.8,-.1,'\nVisualization by DataCharm',transform = ax.transAxes,
  15. ha='center', va='center',fontsize = 9,color='black')
  16. plt.title("Example Of venn3() and venn3_circles()",fontweight="bold",fontsize=15,
  17. pad=30,backgroundcolor="#868686FF",color="black",style="italic")

2021-05-31-10-36-43-907231.png
Example Of venn3()
当然,还可以通过如下代码定义”圈“的样式:

  1. ···
  2. c3 = venn3_circles([set(Group1), set(Group2),set(Group3)], linestyle="--", linewidth=2, color="black",ax=ax)
  3. ···
  4. # 对圆进行设置
  5. c3[1].set_lw(7)
  6. c3[1].set_ls(":")
  7. c3[1].set_color("#7AA6DCFF")
  8. ···

2021-05-31-10-36-44-096640.png