之前有观众老爷问怎么根据OTU表来绘制venn图,觉得做微生物分析其它朋友应该也有此需求,今天就通过一篇文档来详细介绍一下
加载R包
library(tidyverse)
library(ggvenn)
读入数据
group <- read.delim("group.xls")
df <- read.delim("otu.xls",check.names=F) %>%
as_tibble() %>% pivot_longer(-ASV) %>%
filter(value !=0) %>% rename(sample=name)
OTU表中如果丰度=0即表示此OTU不在此样本中出现因此要将其剔除,上述代码通过宽表转长表及
filter
很轻松的解决了这个问题
数据整合
A <- df %>% filter(sample=="A_1")
B <- df %>% filter(sample=="B_1")
C <- df %>% filter(sample=="C_1")
D <- df %>% filter(sample=="D_1")
绘制样本间venn图
list(A=A$ASV,B=B$ASV,C=C$ASV,D=D$ASV) %>%
ggvenn(show_percentage = T,show_elements = F,label_sep = ",",
digits = 1,stroke_color = "white",stroke_alpha=1,
# stroke_linetype="dashed",
fill_color = c("#E41A1C", "#1E90FF", "#FF8C00",
"#4DAF4A"),
set_name_color = c("#E41A1C", "#1E90FF","#FF8C00","#4DAF4A"))
按分组进行数据整合
通过联接函数将分组信息与OTU表整合,从而用于绘制组建venn图
A <- df %>% left_join(group,by="sample") %>% filter(group=="A")
B <- df %>% left_join(group,by="sample") %>% filter(group=="B")
C <- df %>% left_join(group,by="sample") %>% filter(group=="C")
D <- df %>% left_join(group,by="sample") %>% filter(group=="D")
绘制组间venn图
list(A=A$ASV,B=B$ASV,C=C$ASV,D=D$ASV) %>%
ggvenn(show_percentage = T,show_elements = F,label_sep = ",",
digits = 1,stroke_color = "white",stroke_alpha=1,
stroke_size=0.6,
stroke_linetype="dashed",
fill_color = c("#E41A1C", "#1E90FF", "#FF8C00",
"#4DAF4A", "#984EA3"),
set_name_color = c("#E41A1C", "#1E90FF","#FF8C00","#4DAF4A"))
可以看到通过上面简单的步骤我们就完成了venn分析,喜欢的小伙伴欢迎关注我的公众号
R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助