📜  如何在 R 中创建维恩图?

📅  最后修改于: 2022-05-13 01:54:25.666000             🧑  作者: Mango

如何在 R 中创建维恩图?

维恩图是用于显示它们之间关系的集合的图形表示。通过使用维恩图,可以突出集合元素之间的差异和相似之处。维恩图也称为逻辑图或集合图。根据它们之间的关系,我们使用圆圈来表示可以相交或重叠或不相交的集合。我们可以使用维恩图执行许多操作,但其中主要的是:并集、交集和补集。

  • Union:集合的并集是指所有集合的元素的组合。
  • 交集:集合的交集是包含两个集合的公共元素的元素集。当没有公共元素时,交集将给出一个空值。
  • 补:集合的补是全集中除集合本身之外的所有元素的集合。空集的补集是全集。

在本文中,我们将使用 R 编程语言实现维恩图。

方法一:使用“VennDiagram R包”

我们需要先安装并加载包:

句法:

对于为对集或三重集或任意数量的集创建维恩图,语法上只有细微的差别。例如,我们使用关键字“pairwise”来创建具有两个集合的维恩图,而对于三个集合,我们使用“ triple”关键字。

  • 对于两套:
  • 三组:

为了使用此包创建维恩图,请在编辑器中安装并加载该包。使用 grid.newpage( ) 添加一个新的绘图页面。然后,使用上述语法为维恩图插入数据。一组使用draw.single,两组使用draw.pairwise,三组使用draw.triple,四组使用draw.quad,五组使用五元组。

示例 1:

R
# load Venn diagram package
library("VennDiagram")
  
# move to new plotting page
grid.newpage()
  
# create pairwise Venn diagram
draw.pairwise.venn(area1=20, area2=45,cross.area=10,
                   category=c("Mango","Banana"),fill=c("Red","Yellow"))


R
# load Venn diagram package
library("VennDiagram")
  
# move to new plotting page
grid.newpage()
  
# create Venn diagram with three sets
draw.triple.venn(area1=40, area2=15, area3=10, 
                 n12=5, n23=12, n13=4, n123=2, 
                 category=c("Science","Economics","English"),
                 col="Red",fill=c("Green","Yellow","Blue"))


R
# load Venn diagram package
library("VennDiagram")
  
# move to new plotting page
grid.newpage()
  
# create Venn diagram with four sets
draw.quad.venn(area1=72, area2=86, area3=50, 
               area4 =52, n12=44, n23=38, n13=27, 
               n14= 32,n24=32, n34=20, n123=18, 
               n124=17, n234=13, n134=11, n1234=6, 
               category=c("Cricket","Football","Badminton","Table Tennis"),
               col="Green",fill=c("Red","Pink","Blue","Orange"),lty="dashed")


R
# load ggvenn package
library("ggvenn")
  
# use list as input
A <-list('India'=c(1,3,5,7),'USA'=c(1,6,5))
  
# create venn diagram and display all the sets
ggvenn(A)


R
# load ggvenn package
library("ggvenn")
  
# use data frame as input
M <-tibble(value=c(1,3,2,7,5),'TEA'=c(TRUE,FALSE,TRUE,FALSE,FALSE),
           'COFFEE'=c(TRUE,TRUE,FALSE,FALSE,TRUE),  
           'JUICE'=c(TRUE,FALSE,FALSE,TRUE,TRUE))
  
# create Venn diagram and display all sets
ggvenn(M)


R
# load ggvenn package
library("ggvenn")
  
# use list as input
D <-list('Autumn'=c(2,3,1),'Winter'=c(4,7,9,1),
         'Summer'=c(3,7,2,5),'Spring'=c(2,5,9,1))
  
# creating venn diagram for four sets
# and displaying only two sets
ggvenn(D,c("Autumn","Spring"),show_percentage=FALSE,
       fill_color=c("red","orange"))


R
# load ggvenn package
library("ggvenn")
  
# use list as input 
H <-list('Bus'=c(6,7,3),'Truck'=c(4,3,9),
         'Cycle'=c(10,3,2,8),'Car'=c(7,5,4,3))
  
# create customised venn diagram
ggvenn(H,show_elements=TRUE,stroke_color="Red",
       stroke_linetype="solid")


R
# load gplots package
library("gplots")
  
# use list as input
x <-list('Plants'=c(6,7,8),'Animals'=c(7,8,3,4))
  
# create Venn diagram with two sets
venn(x)


R
# load gplots package
library("gplots")
  
# creating venn diagram by passing 
# list as direct parameter
venn(list(YouTube=1:6,Netflix=3:8,Instagram=c(5,8:12)))


R
# load ggVennDiagram Package
library("ggVennDiagram")
  
# use list as an input
x <-list('C++'=c(9,3,5,2),'Java'=c(7,8,4,3),
         'Python'=c(11,2,4,5,8),'Ruby'=c(3,8))
  
# creating Venn diagram and displaying 
# all sets
ggVennDiagram(x)


R
# load ggVennDiagram Package
library("ggVennDiagram")
  
# use list as input
M <-list('Rose'=c(2,6),'Lily'=c(5,3,9,2),
         'Sunflower'=c(3,10,2),'Lotus'=c(5,7,8))
  
# creating Venn diagram with four sets
# but displaying only first two
ggVennDiagram(M[1:2],set_color="Red",label_alpha=0,
              show_intersect=FALSE)


R
# load ggVennDiagram Package
library("ggVennDiagram")
  
# use list as input
D<-list(A=c(5,6,8),B=c(1,9,8,2),C=c(8,9,4,7,11))
  
# creating Venn diagram with three sets
# but displaying only last two
ggVennDiagram(D[2:3],category.names=c("BREAKFAST","LUNCH"))


输出:

示例 2:

电阻

# load Venn diagram package
library("VennDiagram")
  
# move to new plotting page
grid.newpage()
  
# create Venn diagram with three sets
draw.triple.venn(area1=40, area2=15, area3=10, 
                 n12=5, n23=12, n13=4, n123=2, 
                 category=c("Science","Economics","English"),
                 col="Red",fill=c("Green","Yellow","Blue"))

输出:

示例 3:



电阻

# load Venn diagram package
library("VennDiagram")
  
# move to new plotting page
grid.newpage()
  
# create Venn diagram with four sets
draw.quad.venn(area1=72, area2=86, area3=50, 
               area4 =52, n12=44, n23=38, n13=27, 
               n14= 32,n24=32, n34=20, n123=18, 
               n124=17, n234=13, n134=11, n1234=6, 
               category=c("Cricket","Football","Badminton","Table Tennis"),
               col="Green",fill=c("Red","Pink","Blue","Orange"),lty="dashed")

输出:

方法二:使用“GGVENN”R包

首先,我们需要安装这个包并加载它以使其可用。

ggvenn 将维恩图绘制为一个独立的函数。它支持数据框和列表作为输入。

这种方法很简单,只需要绘制要绘制的数据,需要传递。

示例 1:

电阻

# load ggvenn package
library("ggvenn")
  
# use list as input
A <-list('India'=c(1,3,5,7),'USA'=c(1,6,5))
  
# create venn diagram and display all the sets
ggvenn(A)

输出:

示例 2:

电阻

# load ggvenn package
library("ggvenn")
  
# use data frame as input
M <-tibble(value=c(1,3,2,7,5),'TEA'=c(TRUE,FALSE,TRUE,FALSE,FALSE),
           'COFFEE'=c(TRUE,TRUE,FALSE,FALSE,TRUE),  
           'JUICE'=c(TRUE,FALSE,FALSE,TRUE,TRUE))
  
# create Venn diagram and display all sets
ggvenn(M)

输出:

示例 3:



电阻

# load ggvenn package
library("ggvenn")
  
# use list as input
D <-list('Autumn'=c(2,3,1),'Winter'=c(4,7,9,1),
         'Summer'=c(3,7,2,5),'Spring'=c(2,5,9,1))
  
# creating venn diagram for four sets
# and displaying only two sets
ggvenn(D,c("Autumn","Spring"),show_percentage=FALSE,
       fill_color=c("red","orange"))

输出:

示例 4:

电阻

# load ggvenn package
library("ggvenn")
  
# use list as input 
H <-list('Bus'=c(6,7,3),'Truck'=c(4,3,9),
         'Cycle'=c(10,3,2,8),'Car'=c(7,5,4,3))
  
# create customised venn diagram
ggvenn(H,show_elements=TRUE,stroke_color="Red",
       stroke_linetype="solid")

输出:

方法三:使用“GPLOTS”R包

gplots 包提供最多五组的维恩图。 venn()函数接受一个集合列表作为参数,或者它接受一个二进制矩阵,每个集合一列,指示每个元素,每行一个,每个集合的成员资格。 venn() 的主页列出了改变图形外观的选项,例如,可以省略集合的名称和改变大小。但是,有很多机会扩展此包的功能。

像往常一样,第一步是安装和加载包:

我们只是在venn()函数中将输入作为参数传递。

示例 1:

电阻

# load gplots package
library("gplots")
  
# use list as input
x <-list('Plants'=c(6,7,8),'Animals'=c(7,8,3,4))
  
# create Venn diagram with two sets
venn(x)

输出:

示例 2:

电阻

# load gplots package
library("gplots")
  
# creating venn diagram by passing 
# list as direct parameter
venn(list(YouTube=1:6,Netflix=3:8,Instagram=c(5,8:12)))

输出:



方法四:使用“GGVENNDIAGRAM”R包

'ggVennDiagram' 返回可用于绘制维恩图的结构化数据。使用这个包的第一步是安装和加载它。

在这里,我们也将简单地在ggVennDiagram()函数传递参数。

为此,首先安装并加载 ggVennDiagram 包并以列表形式提供输入。然后,使用上述语法创建维恩图并使用其他参数自定义维恩图。

示例 1:

电阻

# load ggVennDiagram Package
library("ggVennDiagram")
  
# use list as an input
x <-list('C++'=c(9,3,5,2),'Java'=c(7,8,4,3),
         'Python'=c(11,2,4,5,8),'Ruby'=c(3,8))
  
# creating Venn diagram and displaying 
# all sets
ggVennDiagram(x)

输出:

示例 2:

电阻

# load ggVennDiagram Package
library("ggVennDiagram")
  
# use list as input
M <-list('Rose'=c(2,6),'Lily'=c(5,3,9,2),
         'Sunflower'=c(3,10,2),'Lotus'=c(5,7,8))
  
# creating Venn diagram with four sets
# but displaying only first two
ggVennDiagram(M[1:2],set_color="Red",label_alpha=0,
              show_intersect=FALSE)

输出:

示例 3:

电阻

# load ggVennDiagram Package
library("ggVennDiagram")
  
# use list as input
D<-list(A=c(5,6,8),B=c(1,9,8,2),C=c(8,9,4,7,11))
  
# creating Venn diagram with three sets
# but displaying only last two
ggVennDiagram(D[2:3],category.names=c("BREAKFAST","LUNCH"))

输出: