R语言里画韦恩图长用到的R包有
Venndiagram
ggvenn
ggVennDiagram
这几个包有一个缺点就是最终呈现的图不是按照数据集的实际比例来的。每个部分的圆或者椭圆大小都一样。如果想要按照数据集的实际比例来,之前我尝试过Y叔的推文 https://guangchuangyu.github.io/cn/2018/04/ggvenn/
今天又发现一个可以实现按照比例画韦恩图的R包eulerr
,推荐给大家,而且这个R包画6个以上的韦恩图也能够实现
关于这个R包的一个介绍的链接
https://cran.r-project.org/web/packages/eulerr/vignettes/introduction.html
下面以一个简单的小例子来介绍
输入的数据集是我们提前算好的每个部分的交集,这里以三个数据集为例
dat<-c("First" = 25,
"Second" = 5,
"Third" = 5,
"First&Second" = 5,
"First&Third" = 5,
"Second&Third" = 3,
"First&Second&Third" = 3)
安装R包
install.packages("eulerr")
画图
library(eulerr)
plot(euler(dat))
接下来是调节细节
首先是每个部分的填充颜色
plot(euler(dat),
fills = list(fill=c("red","blue",
"green","darkgreen",
"white","black",
"purple"),
alpha=0.5))
image.png
这里可以将颜色依次从3个添加到7个就能够看出来颜色添加到了什么地方
给每个部分添加数字
plot(euler(dat),
fills = list(fill=c("red","blue",
"green","darkgreen",
"white","black",
"purple"),
alpha=0.5),
quantities = c(25,5,5,
1,1,1,1))
image.png
这里图上的 1 1 1 1是我自己随便写的,这个不是真实,如果是自己的数据需要自己算下
对文字标签进行修改
plot(euler(dat),
fills = list(fill=c("red","blue",
"green","darkgreen",
"white","black",
"purple"),
alpha=0.5),
quantities = list(c(25,5,5,
1,1,1,1),
col="black",
cex=4),
labels = list(col="white",font=3,cex=2))
image.png
修改边框的颜色
plot(euler(dat),
fills = list(fill=c("red","blue",
"green","darkgreen",
"white","black",
"purple"),
alpha=0.5),
quantities = list(c(25,5,5,
1,1,1,1),
col="black",
cex=4),
labels = list(col="white",font=3,cex=2),
edges = list(col="white",alpha=0))
image.png
修改边框的线型
-
lty
是线型 -
lwd
是粗细
plot(euler(dat),
fills = list(fill=c("red","blue",
"green","darkgreen",
"white","black",
"purple"),
alpha=0.5),
quantities = list(c(25,5,5,
1,1,1,1),
col="black",
cex=4),
labels = list(col="white",font=3,cex=2),
edges = list(col="darkgreen",lwd=5,
lty=1:3))
image.png
添加图例和标题
plot(euler(dat),
fills = list(fill=c("red","blue",
"green","darkgreen",
"white","black",
"purple"),
alpha=0.5),
quantities = list(c(25,5,5,
1,1,1,1),
col="black",
cex=4),
labels = list(col="white",font=3,cex=2),
edges = list(col="darkgreen",lwd=5,
lty=1:3),
main = list(label=c("XiaoMing"),cex=5),
legend = list(labels=c("A","B","C"),
cex=5))
image.png
最后是拼图
plot(euler(dat),
fills = list(fill=c("red","blue",
"green","darkgreen",
"white","black",
"purple"),
alpha=0.5),
quantities = c(25,5,5,
1,1,1,1)) -> p1
plot(euler(dat),
fills = list(fill=c("red","blue",
"green","darkgreen",
"white","black",
"purple"),
alpha=0.5),
quantities = list(c(25,5,5,
1,1,1,1),
col="black",
cex=4),
labels = list(col="white",font=3,cex=2),
edges = list(col="darkgreen",lwd=5,
lty=1:3),
main = list(label=c("XiaoMing"),cex=5),
legend = list(labels=c("A","B","C"),
cex=5)) -> p2
help(package="gridExtra")
gridExtra::grid.arrange(p1,p2,ncol=2)
Snipaste_2021-12-19_04-49-36.jpg
欢迎大家关注我的公众号
小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
网友评论