原文(

簡書部落格

:柳葉刀與小滑鼠):一文解決韋恩圖(零程式碼版本、R語言、python版本)

(1)送給不喜歡程式設計的同學

http://bioinformatics。psb。ugent。be/webtools/Venn/

一文解決韋恩圖(零程式碼版本、R語言、python版本)

(1)匯入準備做交集的檔案。(2)點選submit選項。

其結果如下所示:

一文解決韋恩圖(零程式碼版本、R語言、python版本)

該網站可以完成最多30個數據集的交集繪製。其樣式可以在上一步的

output control

修改。

一文解決韋恩圖(零程式碼版本、R語言、python版本)

(2)送給喜歡用Python的同學

# -*- coding: utf-8 -*-

“”“

Created on Sat Jul 27 18:35:51 2019

@author: czh

”“”

%

reset

-

f

%

clear

# In[*]

#繪圖程式碼

from

matplotlib

import

pyplot

as

plt

import

numpy

as

np

from

matplotlib_venn

import

venn3

venn3_circles

plt

figure

figsize

=

4

4

))

v

=

venn3

subsets

=

230

32

109

33

56

20

44

),

set_labels

=

‘A’

‘B’

‘C’

))

plt

show

()

一文解決韋恩圖(零程式碼版本、R語言、python版本)

import

matplotlib

pyplot

as

plt

from

matplotlib_venn

import

venn3

import

matplotlib

patheffects

as

path_effects

fig

ax

=

plt

subplots

figsize

=

10

10

))

v

=

venn3

subsets

=

10

10

4

10

4

4

2

),

set_labels

=

‘’

‘’

‘’

),

ax

=

ax

v

get_label_by_id

‘100’

)。

set_text

‘Executive’

v

get_label_by_id

‘010’

)。

set_text

‘Legislative’

v

get_label_by_id

‘001’

)。

set_text

‘Judicial’

v

get_label_by_id

‘110’

)。

set_text

‘Example 1’

v

get_label_by_id

‘011’

)。

set_text

‘Example 2’

v

get_label_by_id

‘101’

)。

set_text

‘Example 3’

v

get_label_by_id

‘111’

)。

set_text

‘’

plt

title

“The Three Branches of the US Government”

example_text

=

‘Example 1: The Vice President is considered “President of the Senate” and can vote to break ties。\n’

‘Example 2: The Legislature confirms Supreme Court justices。\n’

‘Example 3: The Executive appoints potential Supreme Court justices。’

text

=

fig

text

0。0

0。05

example_text

ha

=

‘left’

va

=

‘bottom’

size

=

14

text

set_path_effects

([

path_effects

Normal

()])

plt

show

()

一文解決韋恩圖(零程式碼版本、R語言、python版本)

python的限制比較明顯,不能做三個以上資料集的交集,所以推薦用R語言來做。

(3)喜歡R語言的同學

venneuler包

setwd

‘D:\\F1\\deg’

rm

list

=

ls

())

library

venneuler

MyVenn

<-

venneuler

c

A

=

50

B

=

50

C

=

50

“A&B”

=

10

“A&C”

=

10

“B&C”

=

10

“A&B&C”

=

3

))

MyVenn

$labels

<-

c

“”

“”

“”

“”

“”

“”

plot

MyVenn

text

0。4

0。2

“A(n=60)”

cex

=

1

text

0。4

0。8

“B(n=70)”

cex

=

1

text

0。75

0。5

“C(n=50)”

cex

=

1

text

0。5

0。5

“5”

cex

=

1

text

0。4

0。5

“10”

cex

=

1

text

0。55

0。4

“20”

cex

=

1

text

0。5

0。6

“30”

cex

=

1

一文解決韋恩圖(零程式碼版本、R語言、python版本)

這個程式碼從邏輯上看比較簡單,僅僅在text上修改顯示的內容即可。

VennDiagram包

library

VennDiagram

A

=

1

150

B

=

c

121

170

300

320

C

=

c

20

40

141

200

Length_A

<-

length

A

Length_B

<-

length

B

Length_C

<-

length

C

Length_AB

<-

length

intersect

A

B

))

Length_BC

<-

length

intersect

B

C

))

Length_AC

<-

length

intersect

A

C

))

Length_ABC

<-

length

intersect

intersect

A

B

),

C

))

T

<-

venn

diagram

list

A

=

A

B

=

B

),

filename

=

NULL

lwd

=

1

lty

=

2

col

=

c

‘red’

‘green’

),

fill

=

c

‘red’

‘green’

cat

col

=

c

‘red’

‘green’

rotation

degree

=

90

grid

draw

T

一文解決韋恩圖(零程式碼版本、R語言、python版本)

T

<-

venn

diagram

list

A

=

A

B

=

B

C

=

C

),

filename

=

NULL

lwd

=

1

lty

=

2

col

=

c

‘red’

‘green’

‘blue’

fill

=

c

‘red’

‘green’

‘blue’

cat

col

=

c

‘red’

‘green’

‘blue’

reverse

=

TRUE

grid

draw

T

一文解決韋恩圖(零程式碼版本、R語言、python版本)

(4)UpSetR包

正文

介紹一個R包UpSetR,專門用來集合視覺化,更受雜誌和編輯喜歡。

原理比較簡單,做法大概分為兩種,第一種是定義資料集後,畫圖自動取交集。第二種做法是先取交集,然後畫圖。繪製韋恩圖的目的主要是檢視資料集之間的異同。

(1)第一種:定義資料集後直接畫圖取交集

library

UpSetR

library

dplyr

library

tidyr

rm

list

=

ls

())

diff

<-

read

csv

“diffSig_ttest。csv”

header

=

T

row

names

=

1

載入包和所使用的資料。

AA <- subset

diff,

splice_type

==

“AA”

AD <- subset

diff,

splice_type

==

“AD”

AP <- subset

diff,

splice_type

==

“AP”

AT <- subset

diff,

splice_type

==

“AT”

取出準備取交集的資料集們

#fromList

listinput

<-

list

AD

=

AD

$symbol

AP

=

AP

$symbol

AA

=

AA

$symbol

AT

=

AT

$symbol

library

UpSetR

# pdf(file=‘upset。pdf’,height = 8,width = 8)

p

<-

upset

fromList

listinput

),

nsets

=

4

order

by

=

“freq”

# dev。off()

繪製圖片

一文解決韋恩圖(零程式碼版本、R語言、python版本)

(2)取交集後在畫圖

setwd

“E:

\\

Rwork”

library

UpSetR

require

ggplot2

);

require

plyr

);

require

gridExtra

);

require

grid

);

input

<-

c

‘cancer1’

=

1578

‘cancer2’

=

1284

‘cancer3’

=

2488

‘cancer1&cancer2’

=

205

‘cancer1&cancer3’

=

828

‘cancer2&cancer3’

=

589

‘cancer1&cancer2&cancer3’

=

120

data

<-

fromExpression

input

p1

<-

upset

data

nsets

=

9

sets

=

c

‘cancer1’

‘cancer2’

‘cancer3’

),

keep

order

=

TRUE

# number。angles = 30,

point

size

=

5

line

size

=

1。3

mainbar

y

label

=

“IntersectionSize”

sets

x

label

=

“”

mb

ratio

=

c

0。60

0。40

),

text

scale

=

c

4

4

0。5

0。5

3

4

))

p1

一文解決韋恩圖(零程式碼版本、R語言、python版本)