简介
维恩图和UpSet图均可用于集合共有和特有元素信息可视化,如果分组数小于等于5时,推荐用维恩图进行展示;当分组数大于5时,推荐用Upset图进行展示。
维恩图
# input OTUs
otu_table <- read.delim("16S-amplicon-analysis/otutab.txt", header=T, sep="\t", row.names=1, stringsAsFactors = FALSE)
metadata <- read.delim("16S-amplicon-analysis/metadata.txt", header=T, sep="\t", stringsAsFactors = FALSE)
# check data
index <- metadata$Sample %in% colnames(otu_table)
metadata <- metadata[index, ]
otu_table <- otu_table[ ,colnames(otu_table)]
# matrix transpose
transp_otu <- as.data.frame(t(otu_table))
transp_otu$Sample <- rownames(transp_otu)
transp_otu <- merge(metadata, transp_otu, by="Sample")
# transp_otu[c(1:3), c(1:3)]
transp_otu <- transp_otu[,c(-1)]
# transp_otu[c(1:3), c(1:3)]
library(tidyverse)
transp_otu %>% group_by(Group) %>%
summarise_all(mean) -> group_otu
# threshold value
## VennDiagram 要求输入每个圆圈的元素名列表,可以先进行筛选然后再可视化。
## 当然,也可以在可视化的时候进行筛选。
venn_otu <- as.data.frame(group_otu)
rownames(venn_otu) <- venn_otu$Group
venn_otu <-as.data.frame(t(venn_otu[,-1]))
# backups
venn_otu2 <- venn_otu
venn_otu[venn_otu > 0] <- 1
# visualization
library(VennDiagram)
p1 <- venn.diagram(
x=list(KO=row.names(venn_otu[venn_otu$KO==1, ]),
OE=row.names(venn_otu[venn_otu$OE==1, ]),
WT=row.names(venn_otu[venn_otu$WT==1, ])),
filename = "venn1.png", lwd = 3, alpha = 0.6,
label.col = "white", cex = 1.5,
fill = c("dodgerblue", "goldenrod1", "darkorange1"),
cat.col = c("dodgerblue", "goldenrod1", "darkorange1"),
fontfamily = "serif", fontface = "bold",
cat.fontfamily = "serif",cat.fontface = "bold",
margin = 0.05)
p2 <- venn.diagram(
x=list(KO=row.names(venn_otu2[venn_otu2$KO>0, ]),
OE=row.names(venn_otu2[venn_otu2$OE>0, ]),
WT=row.names(venn_otu2[venn_otu2$WT>0, ])),
filename = "venn2.png", lwd = 3, alpha = 0.6,
label.col = "white", cex = 1.5,
fill = c("#eb507e", "#2f90b9", "#fba414"),
cat.col = c("#eb507e", "#2f90b9", "#fba414"),
fontfamily = "serif", fontface = "bold",
cat.fontfamily = "serif",cat.fontface = "bold",
margin = 0.05)
UPset
当分组多于5个时,整个韦恩图会略显混乱,此时upset图是一个较好的选择。
# judgement
judge_char <- function(x){
if_else(x > 0, true = 1, false = 0)}
temp_data <- apply(plot_data[,c(2:length(plot_data))], 2, judge_char)
# check NA
temp_data[is.na(temp_data)] <- 0
# setting colnames
library(UpSetR)
col_names <- colnames(venn_otu)
col_names
upset(venn_otu, sets = col_names,
order.by = c("freq"),
main.bar.color = "#009688")
网友评论