这2天我们系统的学习一些画venn图的一些技巧。我们今天先测试这个最常用的包VennDiagram。
library(VennDiagram)
我们先随机生成一组测试数据:
set1 <- paste(rep("test_" , 200) , sample(c(1:10000) , 200 , replace=F) , sep="")
set2 <- paste(rep("test_" , 500) , sample(c(1:10000) , 500 , replace=F) , sep="")
set3 <- paste(rep("test_" , 300) , sample(c(1:10000) , 300 , replace=F) , sep="")
其中rep函数前面用过很多次了,就是重复第一个变量多少次;paste是字符串连接符;sample函数可以完成随机抽样。
sample(x, size, replace = FALSE)
具体参数说明:
x 整体数据,以向量形式给出
size 抽取样本的数目
replace 如果为F(默认),则是不重复抽样,此时size不能大于x的长度;
如果为T,则是重复抽样,此时size允许大于x的长度
先画最简单的venn图:
venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = 'test1.png',
output=TRUE
)
这个就展示一个最简单的venn图,并且直接保存到了输出文件test1.png中。
如果我们想在R里面展示的话,需要通过grid.draw操作实现。
p1 <- venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = NULL
)
# 展示图片:
grid.newpage()
grid.draw(p1)
下面我们来做细化和美化,比如修改自己想要的色系。
library(RColorBrewer)
myCol <- brewer.pal(3, "Pastel2")
venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = 'test2.png',
output=TRUE,
# 设置输出:
imagetype="png" ,
height = 1000 ,
width = 1000 ,
resolution = 300,
compression = "lzw",
# 圆的外观调整:
lwd = 2, # 描边粗细
lty = "blank", # 去掉描边
fill = myCol, # 填充颜色
)
venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = 'test2.png',
output=TRUE,
imagetype="png" ,
height = 1000 ,
width = 1000 ,
resolution = 300,
compression = "lzw",
lwd = 2, # 描边粗细
lty = "blank", # 去掉描边
fill = myCol, # 填充颜色
# 文字大小:
cex = .5, # 大小;
fontface = "bold", # 粗体
fontfamily = "sans", # 字体
)
venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = 'test2.png',
output=TRUE,
imagetype="png" ,
height = 1000 ,
width = 1000 ,
resolution = 300,
compression = "lzw",
lwd = 2, # 描边粗细
lty = "blank", # 去掉描边
fill = myCol, # 填充颜色
# 文字大小:
cex = .5, # 大小;
fontface = "bold", # 粗体
fontfamily = "sans", # 字体
# 每个集合的名称的调整:
cat.cex = 1.2,
cat.fontface = "bold",
cat.default.pos = "outer", # 集合名称位置:outer -- 外部;text -- 内部
cat.pos = c(-30,30, 180), # 集合名称分别在圈圈的什么角度
cat.dist = c(0.07, 0.07, 0.055), # 外部多少距离
cat.fontfamily = "sans",
rotation = 1 # 旋转
)
下面,我们尝试画一下更复杂一点的四维和五维韦恩图。
set4 <- paste(rep("test_" , 500) , sample(c(1:10000) , 500 , replace=F) , sep="")
set5 <- paste(rep("test_" , 300) , sample(c(1:10000) , 300 , replace=F) , sep="")
fillcolor4 <- c("#ffd7d8", "#d8f2e7", "#d9e7f2", "#eadff0")
venn.diagram(
x = list(set1,set2,set3,set4),
category.names = c("Set 1" , "Set 2 " , "Set 3", "Set 4"),
filename = "test3.png",
# 设置输出:
imagetype="png" ,
height = 1000 ,
width = 1000 ,
resolution = 300,
compression = "lzw",
# 圆圈属性:
col = "white", # 描边颜色
lty = 1, # 虚线形式:1,2,3,4,5可选
lwd = 1, # 粗细
fill =fillcolor4, # 填充颜色;
alpha = 0.90, # 透明度
# 标签属性,这里可以修改圈内的文字的属性,比如颜色等
label.col = "black",
#c("#ffd7d8", "black", "#d8f2e7", "black", "black", "black",
#"black", "black", "#d9e7f2", "black",
# "black", "black", "black", "#eadff0", "black"),
cex = 0.9, # 字体大小
fontfamily = "serif",
fontface = "bold",
# 集合名称属性:
cat.col = c("#cb6274", "#7ba498", "#687d94", "#81668b"),
cat.cex = 1.2,
cat.fontfamily = "serif"
)
venn.diagram(
x = list(set1,set2,set3,set4,set5),
category.names = c("Set 1" , "Set 2 " , "Set 3", "Set 4", "Set 5"),
filename = "test4.png",
# 设置输出:
imagetype="png" ,
height = 1200 ,
width = 1200 ,
resolution = 300,
compression = "lzw",
# 圆圈属性:
col = "white", # 描边颜色
lty = 1, # 虚线形式:1,2,3,4,5可选
lwd = 1, # 粗细
fill = fillcolor5, # 填充颜色;
alpha = 0.90, # 透明度
# 标签属性:
label.col = "black",
# c("#ffd7d8", "black", "#d8f2e7", "black", "black", "black",
# "black", "black", "#d9e7f2", "black",
# "black", "black", "black", "#eadff0", "black"),
cex = .9, # 字体大小
fontfamily = "serif",
fontface = "bold",
# 集合名称属性:
cat.col = c("#cb6274", "#7ba498", "#687d94", "#81668b", "#ffcf5c"),
cat.cex = 1.1,
cat.fontfamily = "serif",
cat.pos = c(0,-30,-130, 130, 40),
cat.dist = 0.18 # 外部多少距离
)
但是超过5维的,一般显示就比较混乱,比较推荐用upset图来表示。
网友评论