韦恩图的画法实在是太多了,今天再介绍一个ggvenn
,之前介绍过同类型的ggVenndiagram
,但是对于边框颜色的更改不友好还介绍过一个非ggplot
语法的venndiagram
。
安装
代码语言:javascript复制# 2选1
install.packages("ggvenn")
if (!require(devtools)) install.packages("devtools")
devtools::install_github("yanlinlin82/ggvenn")
使用
支持数据框和列表格式的输入数据。
列表
代码语言:javascript复制library(ggvenn)
代码语言:javascript复制## Warning: package 'ggvenn' was built under R version 4.2.1
代码语言:javascript复制## Loading required package: dplyr
代码语言:javascript复制##
## Attaching package: 'dplyr'
代码语言:javascript复制## The following objects are masked from 'package:stats':
##
## filter, lag
代码语言:javascript复制## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
代码语言:javascript复制## Loading required package: grid
代码语言:javascript复制## Loading required package: ggplot2
代码语言:javascript复制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))
两个集合:
代码语言:javascript复制ggvenn(a, c("Set 1", "Set 2"))
plot of chunk unnamed-chunk-3
三个集合:
代码语言:javascript复制ggvenn(a, c("Set 1", "Set 2", "Set 3"))
plot of chunk unnamed-chunk-4
不写名字会自动选前4个集合:
代码语言:javascript复制ggvenn(a)
plot of chunk unnamed-chunk-5
数据框形式
代码语言:javascript复制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))
ggvenn(d) # 不写名字也是默认选前4个集合
plot of chunk unnamed-chunk-6
对于tibble
类型的数据还可以用ggplot2
来画,data.frame
的不行哦~
ggplot(d, aes(A = `Set 1`, B = `Set 2`, C = `Set 3`, D = `Set 4`))
geom_venn() theme_void() coord_fixed()
plot of chunk unnamed-chunk-7
细节修改
- 填充色
- fill_color:填充色,默认是"blue", "yellow", "green", "red"
- fill_alpha:填充透明度,默认0.5
- 边框色
- stroke_color:边框色,默认黑色
- stroke_alpha:边框透明度,默认1
- stroke_size:边框粗细,默认1
- stroke_linetype:边框线的类型默认solid
- 集合名字
- set_name_color:集合名颜色,默认black
- set_name_size:集合名字号,默认6
- 集合内文本
- text_color:文本颜色,默认black
- text_size:文本字号,默认4
- 百分比
- show_percentage:TRUE or FALSE
- 显示元素
- show_elements:显示集合内元素,默认FALSE
- label_sep:元素分隔符,默认是
,
- 小数点位数
- digits:默认是1
a <- list(A = c("apple", "pear", "peach"),
B = c("apple", "lemon"))
ggvenn(a, show_elements = TRUE)
plot of chunk unnamed-chunk-8
代码语言:javascript复制ggvenn(a, show_elements = TRUE, label_sep = "n") # show elements in line
plot of chunk unnamed-chunk-8
简单方便,貌似比ggVennDiagram
好用。