R-韦恩图系列-ggVennDiagram - 简书 (jianshu.com)
R-韦恩图系列-VennDiagram - 简书 (jianshu.com)
R-韦恩图系列-UpSetR - 简书 (jianshu.com)
ggVennDiagram包是基于ggplot绘图体系的,因此适用于ggplot的语法结构。
主要参考教程:https://mirror.linux.duke.edu/cran/web/packages/ggVennDiagram/vignettes/using-ggVennDiagram.html
# install.packages("ggVennDiagram")
library(ggVennDiagram)
packageVersion("ggVennDiagram")
# [1] ‘1.2.0’
library(ggplot2)
library(patchwork)
- 示例数据与绘图要素
genes <- paste0("gene",1:1000)
set.seed(20210302)
gene_list <- list(A = sample(genes,100),
B = sample(genes,200),
C = sample(genes,300),
D = sample(genes,200))
# List of 4
# $ A: chr [1:100] "gene284" "gene106" "gene712" "gene905" ...
# $ B: chr [1:200] "gene403" "gene644" "gene438" "gene29" ...
# $ C: chr [1:300] "gene27" "gene788" "gene287" "gene361" ...
# $ D: chr [1:200] "gene601" "gene165" "gene479" "gene390" ...
#最简单的用法
ggVennDiagram(gene_list)
如下图:关于维恩图的细节修改有两部分:一是标签内容部分,而是椭圆(圆)的部分
1、标签部分
1.1 分类名 category name
(1)默认为list name,可通过category.names
参数修改
ggVennDiagram(gene_list, category.names = c("AA","BB","CC","DD"))
(2)set_size
与set_color
参数可以分别修改字体大小与颜色
ggVennDiagram(gene_list, category.names = c("AA","BB","CC","DD"),
set_size = 8, set_color = rainbow(4))
有时名字太长,可能会显示不全,可以通过ggplot2函数拉宽画布
p1=ggVennDiagram(gene_list,
category.names = c("a very long name","short name","name","another name"))
p2=ggVennDiagram(gene_list,
category.names = c("a very long name","short name","name","another name")) +
scale_x_continuous(expand = expansion(mult = .2))
p1 / p2
1.2 交集标签
- 如上图可以看出:默认为数值加百分比格式,且为白色透明度背景
(1) label
参数:c("both", "count", "percent", "none") 四选一,默认为第一个;label_alpha
可设置背景的不透明度
p1 = ggVennDiagram(gene_list, label = "count")
p2 = ggVennDiagram(gene_list, label = "percent",
label_percent_digit = 1, label_alpha = 0)
p1 / p2
(2) label_color
、label_size
可以分别设置标签颜色和字体大小
ggVennDiagram(gene_list, label_color = "firebrick", label_size = 5)
2、椭圆(圆)部分
(1) 关于椭圆的fill填充色设置为根据数值大小的渐变填充色
RColorBrewer::display.brewer.all()
p1 = ggVennDiagram(gene_list) +
scale_fill_distiller(palette = "RdBu") + ggtitle("p1")
p2 = ggVennDiagram(gene_list) +
scale_fill_distiller(palette = "Reds", direction = 1) + ggtitle("p2")
# If -1, the order of colours is reversed.
p3 = ggVennDiagram(gene_list) +
scale_fill_gradient(low="blue",high = "red") + ggtitle("p3")
# https://colorbrewer2.org/
p4 = ggVennDiagram(gene_list) +
scale_fill_gradient(low="#ffffb2",high = "#b10026") + ggtitle("p4")
(p1 | p2) / (p3 | p4)
(2) 椭圆边的设置
-
scale_color_*
设置颜色 -
edge_size
设置粗细 -
edge_lty
设置边的格式
ggVennDiagram(gene_list, edge_lty = "dashed", edge_size = 1) +
scale_color_brewer(palette = "Set1")