venn.diagram: Make a Venn Diagram

View source: R/venn.diagram.R

venn.diagramR Documentation

Make a Venn Diagram

Description

This function takes a list and creates a publication-quality TIFF Venn Diagram

Usage

venn.diagram(x, filename, disable.logging = FALSE, height = 3000, 
    width = 3000, resolution = 500, imagetype = "tiff", 
    units = "px", compression = "lzw", na = "stop", main = NULL, 
    sub = NULL, main.pos = c(0.5, 1.05), main.fontface = "plain",
    main.fontfamily = "serif", main.col = "black",
    main.cex = 1, main.just = c(0.5, 1), sub.pos = c(0.5,
    1.05), sub.fontface = "plain", sub.fontfamily =
    "serif", sub.col = "black", sub.cex = 1, sub.just =
    c(0.5, 1), category.names = names(x), force.unique =
    TRUE, print.mode = "raw", sigdigs = 3, direct.area =
    FALSE, area.vector = 0, hyper.test = FALSE, total.population = NULL, 
    lower.tail = TRUE, ...)

Arguments

x

A list of vectors (e.g., integers, chars), with each component corresponding to a separate circle in the Venn diagram

filename

Filename for image output, or if NULL returns the grid object itself

disable.logging

Boolean to disable log file output and print to console instead

height

Integer giving the height of the output figure in units

width

Integer giving the width of the output figure in units

resolution

Resolution of the final figure in DPI

imagetype

Specification of the image format (e.g. tiff, png or svg)

units

Size-units to use for the final figure

compression

What compression algorithm should be applied to the final tiff

na

Missing value handling method: "none", "stop", "remove"

main

Character giving the main title of the diagram

sub

Character giving the subtitle of the diagram

main.pos

Vector of length 2 indicating (x,y) of the main title

main.fontface

Character giving the fontface (font style) of the main title

main.fontfamily

Character giving the fontfamily (font type) of the main title

main.col

Character giving the colour of the main title

main.cex

Number giving the cex (font size) of the main title

main.just

Vector of length 2 indicating horizontal and vertical justification of the main title

sub.pos

Vector of length 2 indicating (x,y) of the subtitle

sub.fontface

Character giving the fontface (font style) of the subtitle

sub.fontfamily

Character giving the fontfamily (font type) of the subtitle

sub.col

Character Colour of the subtitle

sub.cex

Number giving the cex (font size) of the subtitle

sub.just

Vector of length 2 indicating horizontal and vertical justification of the subtitle

category.names

Allow specification of category names using plotmath syntax

force.unique

Logical specifying whether to use only unique elements in each item of the input list or use all elements. Defaults to FALSE

print.mode

Can be either 'raw' or 'percent'. This is the format that the numbers will be printed in. Can pass in a vector with the second element being printed under the first

sigdigs

If one of the elements in print.mode is 'percent', then this is how many significant digits will be kept

direct.area

If this is equal to true, then the vector passed into area.vector will be directly assigned to the areas of the corresponding regions. Only use this if you know which positions in the vector correspond to which regions in the diagram

area.vector

An argument to be used when direct.area is true. These are the areas of the corresponding regions in the Venn Diagram

hyper.test

If there are only two categories in the venn diagram and total.population is not NULL, then perform the hypergeometric test and add it to the sub title.

total.population

An argument to be used when hyper.test is true. This is the total population size

lower.tail

logical; if TRUE (default), probabilities are P[X <= x], otherwise, P[X > x]

...

A series of graphical parameters tweaking the plot. See below for details

Details

Argument Venn Sizes Class Description
lwd 1,2,3,4,5 numeric Vector giving the width of each circle's circumference
lty 1,2,3,4,5 numeric Vector giving the dash pattern of each circle's circumference
col 1,2,3,4,5 character Vector giving the colour of each circle's circumference
fill 1,2,3,4,5 character Vector giving the colour of each circle's area
alpha 1,2,3,4,5 numeric Vector giving the alpha transparency of each circle's area
label.col 1,2,3,4,5 character Vector giving the colour for each area label (length =
1/3/7/15 based on set-number)
cex 1,2,3,4,5 numeric Vector giving the size for each area label (length =
1/3/7/15 based on set-number)
fontface 1,2,3,4,5 character Vector giving the fontface for each area label (length =
1/3/7/15 based on set-number)
fontfamily 1,2,3,4,5 character Vector giving the fontfamily for each area label (length =
1/3/7/15 based on set-number)
cat.pos 1,2,3,4,5 numeric Vector giving the position (in degrees) of each category
name along the circle, with 0 at 12 o'clock
cat.dist 1,2,3,4,5 numeric Vector giving the distance (in npc units) of each category
name from the edge of the circle (can be negative)
cat.cex 1,2,3,4,5 numeric Vector giving the size for each category name
cat.col 1,2,3,4,5 character Vector giving the colour for each category name
cat.fontface 1,2,3,4,5 character Vector giving the fontface for each category name
cat.fontfamily 1,2,3,4,5 character Vector giving the fontfamily for each category name
cat.just 1,2,3,4,5 numeric List (length = 1/2/3/4 based on set number) of Vectors
of length 2 indicating horizontal and vertical justification
for each category name
cat.default.pos 1,2,3 character One of c('outer', 'text') to specify the default location
of category names (cat.pos and cat.dist are handled differently)
cat.prompts 2 numeric Boolean indicating whether to display help text on category
name positioning or not
margin 1,2,3,4,5 numeric Number giving the amount of whitespace around the diagram
in grid units
rotation.degree 1,2,3,4,5 numeric Number of degrees to rotate the entire diagram
rotation.centre 1,2,3,4,5 numeric Vector of length 2 indicating (x,y) of the rotation
centre
rotation 3 numeric Number giving the clockwise rotation of a three-set Venn
diagram (1, 2, or 3)
reverse 3 logical Reflect the three-set Venn diagram along its central
vertical axis of symmetry. Use in combination with rotation
to generate all possible set orders
euler.d 2, 3 logical Enable Euler diagrams for two-set and three-set Venn
diagrams (Venn Diagrams with moveable circles)
scaled 2, 3 logical Enable scaling for two-set and certain three-set Euler
diagrams. (euler.d must be true to enable this)
sep.dist 2, 3 numeric Controls the separation between distinct circles in
certain two-set or three-set Euler diagrams.
offset 2, 3 numeric Number between 0 and 1 giving the amount to offset the
smaller circle by in the inclusion type of two-set Euler
diagram and certain similar three-set Euler diagrams.
inverted 2 logical Flip the two-set Venn diagram along its vertical
axis (distinguished from reverse)
ext.text 2 logical Allow external text labels when areas are small
ext.percent 2 numeric A vector (length 3) indicating the proportion that
a partial area has to be smaller than to trigger externa
l text placement. The elements allow for individual
control of the areas in the order of the first area,
second area and intersection area.
ext.pos 2 numeric A vector (length 1 or 2) giving the positions (in degrees)
of the external area labels along the
circles, with 0 (default) at 12 o'clock
ext.line.lwd 2 numeric Width of line connecting to ext.text
ext.line.lty 2 numeric The dash pattern of the lines connecting the external
area labels to their anchor points.
ext.dist 2 numeric Vector of length 1 or 2 indicating length of external
line (use negative values to shorten the line )
ext.length 2 numeric Vector of length 1 or 2 indicating the proportion of
the external line that is drawn from the anchor to the text

Value

Plots a figure to the file given by the filename argument.

Author(s)

Hanbo Chen

See Also

draw.single.venn, draw.pairwise.venn, draw.triple.venn, draw.quad.venn, draw.quintuple.venn

Examples

# Note: most examples are listed as dontrun to meet CRAN requirements, 
# but all should work as-is!

# compact and minimal notation
## Not run: 
venn.plot <- venn.diagram(
	list(A = 1:150, B = 121:170), 
	filename = tempfile(
	    pattern = 'Venn_2set_simple',
	    fileext = '.tiff'
	    )
	);
venn.plot <- venn.diagram(
	list(A = 1:150, B = 121:170, C = 101:200), 
	filename = tempfile(
	    pattern = 'Venn_3set_simple',
	    fileext = '.tiff'
	    )
	);

## End(Not run)

# a more elaborate two-set Venn diagram with title and subtitle
venn.plot <- venn.diagram(
	x = list(
		"A" = 1:100,
		"B" = 96:140
		),
	filename = tempfile(
	    pattern = 'Venn_2set_complex',
	    fileext = '.tiff'
	    ),
	scaled = TRUE,
	ext.text = TRUE,
	ext.line.lwd = 2,
	ext.dist = -0.15,
	ext.length = 0.9,
	ext.pos = -4,
	inverted = TRUE,
	cex = 2.5,
	cat.cex = 2.5,
	rotation.degree = 45,
	main = "Complex Venn Diagram",
	sub = "Featuring: rotation and external lines",
	main.cex = 2,
	sub.cex = 1
	);

## Not run: 
# sample three-set Euler diagram
venn.plot <- venn.diagram(
	x = list(
		"Num A" = paste("Num", 1:100),
		"Num B" = c(paste("Num", 61:70), paste("Num", 71:100)),
		"Num C" = c(paste("Num", 41:60), paste("Num", 61:70))),
	euler.d = TRUE,
	filename = tempfile(
	    pattern = 'Euler_3set_simple',
	    fileext = '.tiff'
	    ),
	cat.pos = c(-20, 0, 20),
	cat.dist = c(0.05, 0.05, 0.02),
	cex = 2.5,
	cat.cex = 2.5,
	reverse = TRUE
	);

# sample three-set Euler diagram
venn.plot <- venn.diagram(
	x = list(
		A = c(1:10),
		B = c(11:90),
		C = c(81:90)
		),
	euler.d = TRUE,
	filename = tempfile(
	    pattern = 'Euler_3set_scaled',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.pos = 0
	);

## End(Not run)

# sample four-set Venn Diagram
A <- sample(1:1000, 400, replace = FALSE);
B <- sample(1:1000, 600, replace = FALSE);
C <- sample(1:1000, 350, replace = FALSE);
D <- sample(1:1000, 550, replace = FALSE);
E <- sample(1:1000, 375, replace = FALSE);

venn.plot <- venn.diagram(
	x = list(
		A = A,
		D = D,
		B = B,
		C = C
		),
	filename = tempfile(
	    pattern = 'Venn_4set_pretty', 
	    fileext = '.tiff'
	    ),
	col = "transparent",
	fill = c("cornflowerblue", "green", "yellow", "darkorchid1"),
	alpha = 0.50,
	label.col = c("orange", "white", "darkorchid4", "white", 
	"white", "white", "white", "white", "darkblue", "white", 
	"white", "white", "white", "darkgreen", "white"),
	cex = 1.5,
	fontfamily = "serif",
	fontface = "bold",
	cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"),
	cat.cex = 1.5,
	cat.pos = 0,
	cat.dist = 0.07,
	cat.fontfamily = "serif",
	rotation.degree = 270,
	margin = 0.2
	);

# sample five-set Venn Diagram
venn.plot <- venn.diagram(
	x = list(
		A = A,
		B = B,
		C = C,
		D = D,
		E = E
		),
	filename = tempfile(
	    pattern = 'Venn_5set_pretty',
	    fileext = '.tiff'
	    ),
	col = "black",
	fill = c("dodgerblue", "goldenrod1", "darkorange1", "seagreen3", "orchid3"),
	alpha = 0.50,
	cex = c(1.5, 1.5, 1.5, 1.5, 1.5, 1, 0.8, 1, 0.8, 1, 0.8, 1, 0.8,
	 1, 0.8, 1, 0.55, 1, 0.55, 1, 0.55, 1, 0.55, 1, 0.55, 1, 1, 1, 1, 1, 1.5),
	cat.col = c("dodgerblue", "goldenrod1", "darkorange1", "seagreen3", "orchid3"),
	cat.cex = 1.5,
	cat.fontface = "bold",
	margin = 0.05
	);

# Complex three-way Venn with labels & sub-/super-scripts	
venn.plot <- venn.diagram(
	x = list(
		I = c(1:60, 61:105, 106:140, 141:160, 166:175, 176:180, 181:205, 
		206:220),
		II = c(531:605, 476:530, 336:375, 376:405, 181:205, 206:220, 166:175, 
		176:180),
		III = c(61:105, 106:140, 181:205, 206:220, 221:285, 286:335, 336:375, 
		376:405)
		),
	category.names = c(
		expression( bold('A'['1: subscript']) ),
		expression( bold('B'^'2: going up') ),
		expression( paste(bold('C'^'3'), bold('X'['i' <= 'r'^'2']^'2') ) )
		),
	filename = tempfile(
	    pattern = 'Fig3-1_triple_labels_sub_and_superscripts',
	    fileext = '.tiff'
	    ),
	output = TRUE,
	height = 3000,
	width = 3000,
	resolution = 300,
	compression = 'lzw',
	units = 'px',
	lwd = 6,
	lty = 'blank',
	fill = c('yellow', 'purple', 'green'),
	cex = 3.5,
	fontface = "bold",
	fontfamily = "sans",
	cat.cex = 3,
	cat.fontface = "bold",
	cat.default.pos = "outer",
	cat.pos = c(-27, 27, 135),
	cat.dist = c(0.055, 0.055, 0.085),
	cat.fontfamily = "sans",
	rotation = 1
	);

# Complex 3-way Venn using expressions
venn.plot <- venn.diagram(
   x = list(
		"Num A" = paste("Num", 1:100),
		"Num B" = c(paste("Num", 61:70), paste("Num", 71:100)),
		"Num C" = c(paste("Num", 41:60), paste("Num", 61:70))),
	category.names = c(
		expression( bold('A'['1']) ),
		expression( bold('A'['2']) ),
		expression( bold('A'['3']) )
		),
	euler.d = TRUE,
	filename = tempfile(
	    pattern = 'Fig3-2_Euler_3set_simple_with_subscripts',
	    fileext = '.tiff'
	    ),
	cat.pos = c(-20, 0, 20),
	cat.dist = c(0.05, 0.05, 0.02),
	cex = 2.5,
	cat.cex = 2.5,
	reverse = TRUE
	);

## Not run: 
# Example to print to screen
venn.plot <- venn.diagram(
	x = list(
			sample1 = c(1:40),
			sample2 = c(30:60)
			),
		filename = NULL,
		disable.logging = TRUE
		);

# Save picture to non-TIFF file type
# currently working on adding this functionality directly into venn.diagram
venn.plot <- venn.diagram(
	x = list (
		A = 1:10,
		B = 6:25
		),
	filename = NULL,
	disable.logging = TRUE
	);

jpeg(tempfile(pattern = 'venn_jpeg', fileext = '.jpg'));
grid.draw(venn.plot);
dev.off();


## End(Not run)

#dontrun-starts-here
### NB: All figures from the paper can be run, but are turned off from
###     automatic execution to reduce burden on CRAN computing resources.
## Not run: 
# Figure 1A
venn.plot <- venn.diagram(
	x = list(
		Label = 1:100
		),
	filename = tempfile(
	    pattern = '1A-single_Venn',
	    fileext = '.tiff'
	    ),
	col = "black",
	lwd = 9,
	fontface = "bold",
	fill = "grey",
	alpha = 0.75,
	cex = 4,
	cat.cex = 3,
	cat.fontface = "bold",
	);

# Figure 1B
venn.plot <- venn.diagram(
	x = list(
		X = 1:150,
		Y = 121:180
		),
	filename = tempfile(
	    pattern = '1B-double_Venn',
	    fileext = '.tiff'
	    ),
	lwd = 4,
	fill = c("cornflowerblue", "darkorchid1"),
	alpha = 0.75,
	label.col = "white",
	cex = 4,
	fontfamily = "serif",
	fontface = "bold",
	cat.col = c("cornflowerblue", "darkorchid1"),
	cat.cex = 3,
	cat.fontfamily = "serif",
	cat.fontface = "bold",
	cat.dist = c(0.03, 0.03),
	cat.pos = c(-20, 14)
	);

# Figure 1C
venn.plot <- venn.diagram(
	x = list(
		R = c(1:70, 71:110, 111:120, 121:140),
		B = c(141:200, 71:110, 111:120, 201:230),
		G = c(231:280, 111:120, 121:140, 201:230)
		),
	filename = tempfile(
	    pattern = '1C-triple_Venn',
	    fileext = '.tiff'
	    ),
	col = "transparent",
	fill = c("red", "blue", "green"),
	alpha = 0.5,
	label.col = c("darkred", "white", "darkblue", "white",
	 "white", "white", "darkgreen"),
	cex = 2.5,
	fontfamily = "serif",
	fontface = "bold",
	cat.default.pos = "text",
	cat.col = c("darkred", "darkblue", "darkgreen"),
	cat.cex = 2.5,
	cat.fontfamily = "serif",
	cat.dist = c(0.06, 0.06, 0.03),
	cat.pos = 0
	);

# Figure 1D
venn.plot <- venn.diagram(
	x = list(
		I = c(1:60, 61:105, 106:140, 141:160, 166:175, 176:180, 181:205, 
		206:220),
		IV = c(531:605, 476:530, 336:375, 376:405, 181:205, 206:220, 166:175, 
		176:180),
		II = c(61:105, 106:140, 181:205, 206:220, 221:285, 286:335, 336:375, 
		376:405),
		III = c(406:475, 286:335, 106:140, 141:160, 166:175, 181:205, 336:375, 
		476:530)
		),
	filename = tempfile(
	    pattern = '1D-quadruple_Venn',
	    fileext = '.tiff'
	    ),
	col = "black",
	lty = "dotted",
	lwd = 4,
	fill = c("cornflowerblue", "green", "yellow", "darkorchid1"),
	alpha = 0.50,
	label.col = c("orange", "white", "darkorchid4", "white", "white", "white",
	 "white", "white", "darkblue", "white",
	  "white", "white", "white", "darkgreen", "white"),
	cex = 2.5,
	fontfamily = "serif",
	fontface = "bold",
	cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"),
	cat.cex = 2.5,
	cat.fontfamily = "serif"
	);

# Figure 2-1
venn.plot <- venn.diagram(
	x = list(
		A = 1:105,
		B = 101:115
		),
	filename = tempfile(
	    pattern = '2-1_special_case_ext-text',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.pos = c(-20, 20),
	ext.line.lty = "dotted",
	ext.line.lwd = 2,
	ext.pos = 12,
	ext.dist = -0.12,
	ext.length = 0.85
	);

# Figure 2-2
venn.plot <- venn.diagram(
	x = list(
		A = 1:100,
		B = 1:10
		),
	filename = tempfile(
	    pattern = '2-2_special_case_pairwise-inclusion',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.pos = 0
	);

# Figure 2-3
venn.plot <- venn.diagram(
	x = list(
		A = 1:150,
		B = 151:250
		),
	filename = tempfile(
	    pattern = '2-3_special_case_pairwise-exclusion',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.pos = c(0, 0),
	cat.dist = 0.05
	);

# Figure 2-4
venn.plot <- venn.diagram(
	x = list(
		A = c(1:50, 101:140, 141:160, 161:170),
		B = c(171:230, 101:140, 161:170, 291:320),
		C = c(141:160, 161:170, 291:320)
		),
	filename = tempfile(
	    pattern = '2-4_triple_special_case-001',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.dist = c(0.05, 0.05, -0.1)
	);

# Figure 2-5
venn.plot <- venn.diagram(
	x = list(
		A = c(1:100),
		B = c(61:70, 71:100),
		C = c(41:60, 61:70)
		),
	filename = tempfile(
	    pattern = '2-5_triple_special_case-012AA',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.pos = c(-25, 0, 30),
	cat.dist = c(0.05, 0.05, 0.02)
	);

# Figure 2-6
venn.plot <- venn.diagram(
	x = list(
		A = c(1:90),
		B = c(1:25),
		C = c(1:5)
		),
	filename = tempfile(
	    pattern = '2-6_triple_special_case-022AAAO',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.pos = 0,
	cat.dist = c(0.03, 0.03, 0.01)
	);

# Figure 2-7
venn.plot <- venn.diagram(
	x = list(
		A = c(1:20),
		B = c(21:80),
		C = c(81:210)
		),
	filename = tempfile(
	    pattern = '2-7_triple_special_case-100',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.dist = 0.05
	);

# Figure 2-8
venn.plot <- venn.diagram(
	x = list(
		A = c(1:80),
		B = c(41:150),
		C = c(71:100)
		),
	filename = tempfile(
	    pattern = '2-8_triple_special_case-011A',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.dist = c(0.07, 0.07, 0.02),
	cat.pos = c(-20, 20, 20)
	);

# Figure 2-9
venn.plot <- venn.diagram(
	x = list(
		A = c(1:10),
		B = c(11:90),
		C = c(81:90)
		),
	filename = tempfile(
	    pattern = '2-9_triple_special_case-121AO',
	    fileext = '.tiff'
	    ),
	cex = 2.5,
	cat.cex = 2.5,
	cat.pos = 0,
	cat.dist = c(0.04, 0.04, 0.02),
	reverse = TRUE
	);

#dontrun-ends-here

## End(Not run)

VennDiagram documentation built on April 13, 2022, 1:06 a.m.