基本简介
韦恩图(Venn diagram)可以展示在不同的事物群组或者集合间的联系。R 语言中的维恩图绘制有很多包,例如Vennerable,VennDigram,venn,ggvenn等。今天我们使用ggvenn包绘制韦恩图。
示例代码
#安装包
if (!require(devtools)) install.packages("devtools")
devtools::install_github("yanlinlin82/ggvenn") # install via GitHub (for latest version)
#构建数据
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))
#可视化绘制
opar <- par(family = "Roboto Condensed")
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, c("Set 1", "Set 2"))
ggvenn(d, c("Set 1", "Set 2", "Set 3"))
ggvenn(d)
#或者
# draw two-set venn (use A, B in aes)
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)
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)
ggplot(d, aes(A = `Set 1`, B = `Set 2`, C = `Set 3`, D = `Set 4`)) +
geom_venn() + theme_void() + coord_fixed()
网友评论