跟着Nature microbiology学画图~R语言画韦恩图然后拼图

2020-12-18 17:19:21 浏览数 (1)

image.png

重复的图片是Figure2中的韦恩图,然后将两个韦恩图拼到一起

R语言里比较常用的画韦恩图的包是VennDiagram,但是今天的内容涉及到拼图,用VennDiagram画图后如何拼图我暂时还不知道。所以今天使用另外一个包ggvenn,因为是gg系列,拼图实现起来就相对比较容易。之前也录制过视频介绍这个包

第一次使用需要先安装
代码语言:javascript复制
devtools::install_github("yanlinlin82/ggvenn")

输入数据可以是列表,也可以是一个数据框,今天用列表的形式作为输入数据

读入数据
代码语言:javascript复制
df1<-read.csv("example_data/venn_example_1.csv",header=T)
df2<-read.csv("example_data/venn_example_2.csv",header=T)
df3<-read.csv("example_data/venn_example_3.csv",header=T)
df4<-read.csv("example_data/venn_example_4.csv",header=T)
构造一个列表数据
代码语言:javascript复制
x<-list(`High marine protein`=df1$A,
        `Medium fat`=df2$B,
        `High fat`=df3$C,
        `Low marine protein`=df4$D)
最基本的韦恩图
代码语言:javascript复制
library(ggvenn)
ggvenn(x)

image.png

这里遇到一个问题是文字标签超出边界了,如何调节文字标签的位置暂时还不知道如何通过代码实现,只能出图后手动编辑了。

接下来是美化

比如改变配色,去掉百分比,更改文字的颜色等等

代码语言:javascript复制
ggvenn(x,
       show_percentage = F,
       stroke_color = "white",
       fill_color = c("#ffb2b2","#b2e7cb","#b2d4ec","#d3c0e2"),
       set_name_color = c("#ff0000","#4a9b83","#1d6295","#7030a2"))

image.png

接下来是再画一个三维的韦恩图
代码语言:javascript复制
ggvenn(x,
       c("Medium fat","High fat", "Low marine protein"),
       show_percentage = F,
       stroke_color = "white",
       fill_color = c("#ffb2b2","#b2e7cb","#b2d4ec"),
       set_name_color = c("#ff0000","#4a9b83","#1d6295"))

image.png

接下来是拼图

因为是gg系列,拼图可以选择的就很多了,这里我选择cowplot这个包

代码语言:javascript复制
p1<-ggvenn(x,
       show_percentage = F,
       stroke_color = "white",
       fill_color = c("#ffb2b2","#b2e7cb","#b2d4ec","#d3c0e2"),
       set_name_color = c("#ff0000","#4a9b83","#1d6295","#7030a2"),
       set_name_size  = 2)
p2<-ggvenn(x,
       c("Medium fat","High fat", "Low marine protein"),
       show_percentage = F,
       stroke_color = "white",
       fill_color = c("#ffb2b2","#b2e7cb","#b2d4ec"),
       set_name_color = c("#ff0000","#4a9b83","#1d6295"),
       set_name_size = 2)
library(cowplot)
plot_grid(p1,p2,ncol=2,nrow = 1,labels = c("b",""))

image.png

0 人点赞