传入数据
data = read.csv("venn_data.csv",header = TRUE,row=1)
#获取行名;
otus = rownames(data)
head(otus)
#获取列名;
groups = colnames(data)
groups
#指定比较组,"_"为分隔符号;
pairwises="S10_S2NAC_S7_S9_M3_M7_M8_M9"
#获取所指定比较组的样本名;
s<-strsplit(pairwises,"_")
s
compares = unlist(s)
compares
#筛选每个样本中丰度值大于0的otu,保存为list;
x = list()
x
#[]选取数据框、矩阵、向量中的元素,[[]]选取列表中的元素;
for (i in compares){
x[[i]] =otus[as.numeric(data[,i]) > 0]
}
summary(x)
开始绘图
library(ggplot2)
library(ggsci)
library(sf)
library(ggVennDiagram)
#自定义颜色;
color1 <- alpha("#f8766d",0.9)
color2 <- alpha("#FF99CC",0.7)
color3 <- alpha("#c77cff",0.5)
color4 <- alpha("#99CC00",0.5)
#绘制常见的4组数据venn图;
ggVennDiagram(x[1:4], label_alpha=0) +
scale_fill_gradient(low="white",high =color4 ,guide="none")
#绘制5组数据的venn图;
#label_alpha = 0去除文字标签底色;
#category.names参数用于设定样本名称;
ggVennDiagram(x[1:5], label_alpha=0,label_size =3) +
scale_color_brewer(palette = "Paired")+
scale_fill_gradient(low="white",high = color1)
#还可以用交互的方式(plotly)查看每个子集中的基因;
ggVennDiagram(x[1:5], show_intersect = TRUE)
#同样的方法,再看下7组数据的绘制效果;
ggVennDiagram(x, label_alpha=0,label_size =3,
edge_size = 0.5,label ="count") +
scale_color_lancet()+
scale_fill_gradient(low="gray100",high = color2,guide="none")
ggVennDiagram(x, label_alpha=0,label_size =3,
edge_size = 0.5,label ="count") +
scale_color_lancet()+
scale_fill_gradient(low="gray100",high = "gray95",guide="none")
网友评论