感谢各位简友一直的关注。
欢迎关注:感谢陪伴
前言
早就想写个绘制韦恩图的函数了,每次调包真的很麻烦 。。
需要准备的包:ggvenn
、 export
(如果你想导出为PPT的话)
功能
- 输入2~4个对象 (vector)。绘制韦恩图,并返回两两交集的表格
- 输出可选择:
- R 图形界面
- PPT
代码
venn <- function(..., Rplot = TRUE, PDF = NULL, PPT = NULL,
cex.label = 10, cex.num = 8,
alpha.fill = 0.7, col.fill = c("#E5D2DD", "#53A85F", "#F1BB72", "#F3B1A0"),
alpha.stroke = 1, size.stroke = 1,
col.stroke = "black",
show_percentage = FALSE
){
getName <- function(...){
as.character(substitute(list(...)))[-1]
}
data_list = list(...)
names(data_list) = getName(...)
# build venn table
Len = length(data_list)
vennM = matrix("", Len, Len, dimnames = list(getName(...), getName(...)))
for(i in 1:Len){
for(j in i:Len){
vennM[i, j] = length(intersect(data_list[[i]], data_list[[j]]))
}
}
resM = apply(vennM, 1, as.numeric)
dimnames(resM) = dimnames(vennM)
if(Len > 4){
message("More than 4 objects, will not plot venn.")
return(resM)
}
# plot venn
require(ggvenn)
p <- suppressWarnings(ggvenn(
data = data_list,
show_percentage = show_percentage,
set_name_size = cex.label,
text_size = cex.num,
stroke_alpha = alpha.stroke,
stroke_size = size.stroke,
fill_alpha = alpha.fill,
fill_color = col.fill,
stroke_linetype = "longdash",
))
if(Rplot){
print(p)
}
if(!is.null(PDF)){
pdf(PDF, 8, 8)
print(p)
dev.off()
}
if(!is.null(PPT)){
require(export)
graph2ppt(p, file = PPT, width = 8, height = 8)
}
return(resM)
}
示例
set.seed(1998)
s1 = sample(LETTERS, 6)
s2 = sample(LETTERS, 8)
s3 = sample(LETTERS, 10)
s4 = sample(LETTERS, 12)
s5 = sample(LETTERS, 14)
venn(s1, s2, s3)
venn(s1, s2, s3, s4)
venn(s1, s2, s3, s4, s5)