R语言绘图包系列:
- R语言绘图包01--优秀的拼图包patchwork
- R语言绘图包02--热图pheatmap
- R语言绘图包03--火山图EnhancedVolcano
- R语言绘图包04--GOplot:富集分析结果可视化
韦恩图可以用来展示各个数据集之间的重叠关系
绘制韦恩图的方法很多,在这里介绍两种
1. ggvenn绘制韦恩图
2. 使用VennDiagram函数包中的venn.diagram函数绘制韦恩图
1. ggvenn
这个包支持列表或数据框的数据作为输入
install.packages("devtools")
devtools::install_github("yanlinlin82/ggvenn")
1.1 输入list
library(ggvenn)
library(patchwork)
a <- list(`Set 1` = c(1, 3, 5, 7, 9),
`Set 2` = c(1, 5, 9, 13),
`Set 3` = c(1, 2, 8, 9),
`Set 4` = c(6, 7, 10, 12))
p1=ggvenn(a, c("Set 1", "Set 2")) # draw two-set venn
p2=ggvenn(a, c("Set 1", "Set 2", "Set 3")) # draw three-set venn
p3=ggvenn(a) # without set names, the first 4 elements in list will be chose to draw four-set venn
p1|p2|p3
1.2 输入数据框(每个列是一个set)
d <- tibble(value = c(1, 2, 3, 5, 6, 7, 8, 9),
`Set 1` = c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE),
`Set 2` = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE),
`Set 3` = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE),
`Set 4` = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE))
p1=ggvenn(d, c("Set 1", "Set 2")) # draw two-set venn
p2=ggvenn(d, c("Set 1", "Set 2", "Set 3")) # draw three-set venn
p3=ggvenn(d) # without set names, the first 4 logical column in data.frame will be chose to draw four-set venn
p1|p2|p3
1.3 数据框作为输入时,还可以使用ggplot2的语法绘制
# draw two-set venn (use A, B in aes)
p1=ggplot(d, aes(A = `Set 1`, B = `Set 2`)) +
geom_venn() + theme_void() + coord_fixed()
# draw three-set venn (use A, B, C in aes)
p2=ggplot(d, aes(A = `Set 1`, B = `Set 2`, C = `Set 3`)) +
geom_venn() + theme_void() + coord_fixed()
# draw four-set venn (use A, B, C, D in aes)
p3=ggplot(d, aes(A = `Set 1`, B = `Set 2`, C = `Set 3`, D = `Set 4`)) +
geom_venn() + theme_void() + coord_fixed()
p1|p2|p3
1.4 美化
1.4.1 美化颜色和大小
颜色填充参数:
fill_color
- 默认是 c("blue", "yellow", "green", "red")
fill_alpha
- 默认是 0.5
边线设置参数:
stroke_color
- 默认是 "black"
stroke_alpha
- 默认是 1
stroke_size
- 默认是 1
stroke_linetype
- 默认是 "solid"
集合名字设置:
set_name_color
- 默认是 "black"
set_name_size
- 默认是 6
图形中字体设置:
text_color
- 默认是 "black"
text_size
- 默认是 4
以上所有的参数都可以用于ggvenn()
和 geom_venn()
# For example:
a <- list(A = 1:4, B = c(1,3,5))
ggvenn(a, stroke_linetype = 2, stroke_size = 0.5,
set_name_color = "red", set_name_size = 15,
fill_color = c("pink", "gold"))
1.4.2 展示元素
show_elements
- 默认是 FALSE
label_sep
- text used to concatenate elements, default is ","
#For example:
a <- list(A = c("apple", "pear", "peach"),
B = c("apple", "lemon"))
p1=ggvenn(a, show_elements = TRUE)
p2=ggvenn(a, show_elements = TRUE, label_sep = "\n") # show elements in line
p1|p2
1.4.3 隐藏百分比,改变百分比的小数点位数
show_percentage
- 默认是TRUE
digits
- 默认是 1
a <- list(A = 1:5, B = 1:2)
p1=ggvenn(a, show_percentage = FALSE)
p2=ggvenn(a, digits = 2)
p1|p2
2. venn.diagram
参数:
venn.diagram(x, filename, height = 3000, width = 3000, resolution =
500, imagetype = "tiff", units = "px", compression =
"lzw", na = "stop", main = NULL, sub = NULL, main.pos
= c(0.5, 1.05), main.fontface = "plain",
main.fontfamily = "serif", main.col = "black",
main.cex = 1, main.just = c(0.5, 1), sub.pos = c(0.5,
1.05), sub.fontface = "plain", sub.fontfamily =
"serif", sub.col = "black", sub.cex = 1, sub.just =
c(0.5, 1), category.names = names(x), force.unique =
TRUE, print.mode = "raw", sigdigs = 3, direct.area =
FALSE, area.vector = 0, hyper.test = FALSE, total.population = NULL,
lower.tail = TRUE, ...)
2.1 绘制一张最基本的韦恩图
library(VennDiagram)
# 生成数据集
set1 <- paste(rep("word_" , 200) , sample(c(1:1000) , 200 , replace=F) , sep="")
set2 <- paste(rep("word_" , 200) , sample(c(1:1000) , 200 , replace=F) , sep="")
set3 <- paste(rep("word_" , 200) , sample(c(1:1000) , 200 , replace=F) , sep="")
venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = 'venn_diagramm.png',
output=TRUE) #直接在工作目录下生成这个名为venn_diagramm.png的图像
2.2 添加颜色
library(RColorBrewer)
myCol <- brewer.pal(3, "Pastel2")
venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = 'venn_diagramm_color.png',
output=TRUE,
# Output features
imagetype="png" ,
height = 480 ,
width = 480 ,
resolution = 300,
compression = "lzw",
# Circles
lwd = 2,
lty = 'blank',
fill = myCol,
# Numbers
cex = .6,
fontface = "bold",
fontfamily = "sans",
# Set names
cat.cex = 0.6,
cat.fontface = "bold",
cat.default.pos = "outer",
cat.pos = c(-27, 27, 135),
cat.dist = c(0.055, 0.055, 0.085),
cat.fontfamily = "sans",
rotation = 1
)
2.3 更改样式
# Libraries
library(tidyverse)
library(hrbrthemes)
library(tm)
library(proustr)
library(VennDiagram)
#数据准备
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/14_SeveralIndepLists.csv", header=TRUE)
to_remove <- c("_|[0-9]|\\.|function|^id|script|var|div|null|typeof|opts|if|^r$|undefined|false|loaded|true|settimeout|eval|else|artist")
data <- data %>% filter(!grepl(to_remove, word)) %>% filter(!word %in% stopwords('fr')) %>% filter(!word %in% proust_stopwords()$word)
# 绘图
venn.diagram(
x = list(
data %>% filter(artist=="booba") %>% select(word) %>% unlist() ,
data %>% filter(artist=="nekfeu") %>% select(word) %>% unlist() ,
data %>% filter(artist=="georges-brassens") %>% select(word) %>% unlist()
),
category.names = c("Booba (1995)" , "Nekfeu (663)" , "Brassens (471)"),
filename = 'venn.png',
output = TRUE ,
imagetype="png" ,
height = 480 ,
width = 480 ,
resolution = 300,
compression = "lzw",
lwd = 1,
col=c("#440154ff", '#21908dff', '#fde725ff'),
fill = c(alpha("#440154ff",0.3), alpha('#21908dff',0.3), alpha('#fde725ff',0.3)),
cex = 0.5,
fontfamily = "sans",
cat.cex = 0.3,
cat.default.pos = "outer",
cat.pos = c(-27, 27, 135),
cat.dist = c(0.055, 0.055, 0.085),
cat.fontfamily = "sans",
cat.col = c("#440154ff", '#21908dff', '#fde725ff'),
rotation = 1
)
VennDiagram函数包最大能绘制5个数据集合的韦恩图。
参考:
https://github.com/yanlinlin82/ggvenn
R语言画维恩图--ggvenn
venn.diagram函数文档
The R Graph Gallery绘图教程
https://cloud.tencent.com/developer/article/1675092
https://www.jianshu.com/p/f858521828a5