查看原文
其他

ggplot2版本的韦恩图画法

阿越就是我 医学和生信笔记 2023-02-25

关注公众号,发送R语言Python,获取学习资料!

  Stay hungry, stay foolish!   


前几天介绍了使用venndiagram包绘制韦恩图,这个包很简单,参数也不复杂,基本能满足日常需求,但是每次运行都要产生一个日志文件在本地,很是难受,最多支持5个集合。

现在在R里面画图,如果不能支持图层语法,感觉都拿不出手了,一切都在ggplot2化。

韦恩图当然也不例外,比如这个国产R包:ggvenndiagram

这个包比较牛逼的地方在于,它可以通过封装好的函数直接画图,也可以把画图数据提取出来,让你用ggplot2自己画,支持2-7个集合,应该够你用了吧?

俗话说:数就是图,图就是数,相比较于画图来说,我还是觉得整理数据比较难!

  • 安装

  • 使用

  • 提取数据,用ggplot2画

安装

任选一种:

install.packages("ggVennDiagram")

# install.packages("devtools")
devtools::install_github("gaospecial/ggVennDiagram")

这个包发了文章的,大家使用的话记得引用!

Gao, C.-H., Yu, G., and Cai, P. (2021). ggVennDiagram: An Intuitive, Easy-to-Use, and Highly Customizable R Package to Generate Venn Diagram. Frontiers in Genetics 12, 1598. doi: 10.3389/fgene.2021.706907.

使用

library(ggVennDiagram)
library(ggplot2)
sets <- list(
  a = sample(1:500,300),
  b = sample(1:500,350),
  c = sample(1:500,420),
  d = sample(1:500,300)
)

ggVennDiagram(sets,
              category.names = c("set 1","set 2","set 3","set 4"), # 集合名字
              set_color = c("blue","red","yellow","black"), # 集合名字颜色
              set_size = 6,# 集合名字大小
              label = "both"# "count","percent","both","none"
              label_geom = "label"# label/text,
              label_alpha = 0# label背景色
              label_color = "firebrick",
              label_percent_digit = 2# 保留几位小数
              edge_lty = "dashed"# 边框线型,solid
              edge_size = 1.2 # 边框粗细
              )+
  scale_fill_gradient(low = "grey90",high = "grey60")+ # 填充色
  scale_color_manual(values = c("grey10","grey10","grey10","grey10")) # 边框色,貌似不能变为无边框
plot of chunk unnamed-chunk-3

上面这个例子是直接出图版,简单快捷,不过默认颜色不是很好看,需要自己鼓捣一番才能找到合适的颜色。

下面是比较有意思的部分,提取数据,让你用ggplot2自己画!

提取数据,用ggplot2画

作者把韦恩图的主要结构拆解为3个部分:集合标签/集合边框/交集区域,把这3个部分分装在一个S4对象里,方便你自由调取使用,自由改变。

venn <- Venn(sets)
df <- process_data(venn)
df
## An object of class "VennPlotData"
## Slot "setEdge":
## Simple feature collection with 4 features and 5 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: 0.0649949 ymin: 0.1849949 xmax: 0.9350051 ymax: 0.8391534
## CRS:           NA
## # A tibble: 4 x 6
##   id                                      geometry component item    count name 
##   <chr>                               <LINESTRING> <chr>     <named> <int> <chr>
## 1 1     (0.1025126 0.7174874, 0.09412107 0.708119~ setEdge   <int [~   300 a    
## 2 2     (0.2525126 0.8174874, 0.246341 0.8103391,~ setEdge   <int [~   350 b    
## 3 3     (0.7333452 0.8033452, 0.7262248 0.8095447~ setEdge   <int [~   420 c    
## 4 4     (0.8974874 0.7174874, 0.8881191 0.7258789~ setEdge   <int [~   300 d    
## 
## Slot "setLabel":
## Simple feature collection with 4 features and 3 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 0.08 ymin: 0.78 xmax: 0.93 ymax: 0.86
## CRS:           NA
## # A tibble: 4 x 4
##   id       geometry component name 
##   <chr>     <POINT> <chr>     <chr>
## 1 1     (0.08 0.78) setLabel  a    
## 2 2     (0.26 0.86) setLabel  b    
## 3 3     (0.71 0.85) setLabel  c    
## 4 4     (0.93 0.78) setLabel  d    
## 
## Slot "region":
## Simple feature collection with 15 features and 5 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 0.0649949 ymin: 0.1849949 xmax: 0.9350051 ymax: 0.8391534
## CRS:           NA
## # A tibble: 15 x 6
##    id                                    geometry component item    count name  
##    <chr>                                <POLYGON> <chr>     <list>  <int> <chr> 
##  1 1     ((0.1025126 0.7174874, 0.1118809 0.7258~ region    <int [~     7 a     
##  2 2     ((0.2525126 0.8174874, 0.2596609 0.8236~ region    <int [~     9 b     
##  3 3     ((0.7333452 0.8033452, 0.7395447 0.7962~ region    <int [~    26 c     
##  4 4     ((0.8974874 0.7174874, 0.9058789 0.7081~ region    <int [~     6 d     
##  5 12    ((0.2494531 0.7508377, 0.266399 0.74722~ region    <int [~    18 a..b  
##  6 13    ((0.3598131 0.3161471, 0.3466157 0.3144~ region    <int [~    23 a..c  
##  7 14    ((0.6341476 0.306281, 0.6321686 0.29195~ region    <int [~     6 a..d  
##  8 23    ((0.4087951 0.6905086, 0.4240163 0.7044~ region    <int [~    47 b..c  
##  9 24    ((0.7013464 0.5605964, 0.7121743 0.5437~ region    <int [~     7 b..d  
## 10 34    ((0.7562978 0.7359764, 0.7555797 0.7233~ region    <int [~    28 c..d  
## 11 123   ((0.4254307 0.668526, 0.4425419 0.65529~ region    <int [~    67 a..b.~
## 12 124   ((0.6020164 0.4567956, 0.6098817 0.4389~ region    <int [~    24 a..b.~
## 13 134   ((0.4966178 0.374675, 0.4805314 0.36463~ region    <int [~    51 a..c.~
## 14 234   ((0.5085786 0.6114214, 0.5243976 0.6266~ region    <int [~    74 b..c.~
## 15 1234  ((0.5066822 0.5956024, 0.5213246 0.5792~ region    <int [~   104 a..b.~

通过上面两个函数,就把韦恩图的结构变成了数据框形式,方便使用ggplot2画图,比较牛逼的是geometry这列,存储的是各个集合的坐标!

接下来就可以使用ggplot2了。

ggplot()+
  geom_sf(data = venn_region(df), aes(fill=count))+
  geom_sf(data = venn_setedge(df),size=2,lty="dashed",color="grey")+
  geom_sf(data = venn_setlabel(df),aes(label=name))+
  geom_sf_label(data = venn_region(df),aes(label=id),fontface="bold")+
  scale_fill_distiller(palette = 5)+
  theme_void()
plot of chunk unnamed-chunk-5

是不是很神奇,不过还是封装好的画图函数的更好用的一点。

而且,不太容易找到好看的颜色搭配。



以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发

欢迎在评论区留言或直接添加我的微信!

 
End



欢迎关注公众号:医学和生信笔记

医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!


往期回顾

通过数据看上海疫情

2022-03-31

让R运算速度快100倍!!!

2022-04-02

画一个好看的桑基图?

2022-04-03

可视化可爱的小蜜蜂

2022-04-04

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存