Title: | A 'ggplot2' Implement of Venn Diagram |
---|---|
Description: | Easy-to-use functions to generate 2-7 sets Venn or upset plot in publication quality. 'ggVennDiagram' plot Venn or upset using well-defined geometry dataset and 'ggplot2'. The shapes of 2-4 sets Venn use circles and ellipses, while the shapes of 4-7 sets Venn use irregular polygons (4 has both forms), which are developed and imported from another package 'venn', authored by Adrian Dusa. We provided internal functions to integrate shape data with user provided sets data, and calculated the geometry of every regions/intersections of them, then separately plot Venn in four components, set edges/labels, and region edges/labels. From version 1.0, it is possible to customize these components as you demand in ordinary 'ggplot2' grammar. From version 1.4.4, it supports unlimited number of sets, as it can draw a plain upset plot automatically when number of sets is more than 7. |
Authors: | Chun-Hui Gao [aut, cre] , Guangchuang Yu [ctb] , Adrian Dusa [aut, cph] (<https://orcid.org/0000-0002-3525-9253>, Adrian Dusa is the author and copyright holder of venn, where ggVennDiagram imports the polygon coordinates enabling 5 - 7 sets Venn diagram.), Turgut Yigit Akyol [ctb] |
Maintainer: | Chun-Hui Gao <[email protected]> |
License: | GPL-3 |
Version: | 1.5.3 |
Built: | 2024-11-22 03:51:56 UTC |
Source: | https://github.com/gaospecial/ggvenndiagram |
All members of a list have the same elements
all_identical(list)
all_identical(list)
list |
a list |
TRUE or FALSE
all possible combinations of n sets
combinations(n)
combinations(n)
n |
dim |
discern
returns the difference between two group of sets selected from
a Venn
object. If multiple sets are chosen for the slices, union of
those sets will be used.
discern(venn, slice1, slice2 = "all") ## S4 method for signature 'Venn' discern(venn, slice1, slice2 = "all")
discern(venn, slice1, slice2 = "all") ## S4 method for signature 'Venn' discern(venn, slice1, slice2 = "all")
venn |
(Required) A |
slice1 |
(Required) The name or the index of the set of interest. Multiple sets can be selected. |
slice2 |
(Optional) The name or the index of the set of interest. Multiple sets can be selected. Default is all the sets except the sets of slice1. |
A vector showing the difference between slice1 and slice2.
venn = Venn(list(letters[1:10], letters[3:12], letters[6:15])) discern(venn, slice1 = 1) discern(venn, slice1 = c(1, 2), slice2 = 3)
venn = Venn(list(letters[1:10], letters[3:12], letters[6:15])) discern(venn, slice1 = 1) discern(venn, slice1 = c(1, 2), slice2 = 3)
calculate the unique region defined by 'Venn' object and the parameter 'slice'.
discern_overlap(venn, slice = "all") ## S4 method for signature 'Venn' discern_overlap(venn, slice = "all")
discern_overlap(venn, slice = "all") ## S4 method for signature 'Venn' discern_overlap(venn, slice = "all")
venn |
a Venn object |
slice |
index of Venn members, default is "all" |
region items
library(ggVennDiagram) venn <- Venn(list(A=1:3,B=2:5,C=c(1L,3L,5L))) discern_overlap(venn, slice = "all") # is equal to overlap(venn, slice = "all") # however, `discern_overlap()` only contains specific region discern_overlap(venn, slice = 1:2) # is different from overlap(venn, slice = 1:2)
library(ggVennDiagram) venn <- Venn(list(A=1:3,B=2:5,C=c(1L,3L,5L))) discern_overlap(venn, slice = "all") # is equal to overlap(venn, slice = "all") # however, `discern_overlap()` only contains specific region discern_overlap(venn, slice = 1:2) # is different from overlap(venn, slice = 1:2)
Specifying a shape
get_shape_by_id(id)
get_shape_by_id(id)
id |
shape id |
a shape
get_shape_by_id("401f")
get_shape_by_id("401f")
ggVennDiagram stores shapes as internal data. You may see all the shapes by using 'plot_shapes()' or 'get_shapes()'.
get_shape_data(nsets, type = NULL, shape_id = NULL)
get_shape_data(nsets, type = NULL, shape_id = NULL)
nsets |
number of sets |
type |
type of shape |
shape_id |
shape id |
a tibble describing specific shape
get_shape_data(nsets = 4, type = "polygon")
get_shape_data(nsets = 4, type = "polygon")
Get all shapes
get_shapes()
get_shapes()
a tibble
get_shapes()
get_shapes()
ggVennDiagram main parser
ggVennDiagram( x, category.names = names(x), show_intersect = FALSE, set_color = "black", set_size = NA, label = c("both", "count", "percent", "none"), label_alpha = 0.5, label_geom = c("label", "text"), label_color = "black", label_size = NA, label_percent_digit = 0, label_txtWidth = 40, edge_lty = "solid", edge_size = 1, force_upset = FALSE, nintersects = 20, order.intersect.by = c("size", "name", "none"), order.set.by = c("size", "name", "none"), relative_height = 3, relative_width = 0.3, ... )
ggVennDiagram( x, category.names = names(x), show_intersect = FALSE, set_color = "black", set_size = NA, label = c("both", "count", "percent", "none"), label_alpha = 0.5, label_geom = c("label", "text"), label_color = "black", label_size = NA, label_percent_digit = 0, label_txtWidth = 40, edge_lty = "solid", edge_size = 1, force_upset = FALSE, nintersects = 20, order.intersect.by = c("size", "name", "none"), order.set.by = c("size", "name", "none"), relative_height = 3, relative_width = 0.3, ... )
x |
list of items |
category.names |
default is names(x) |
show_intersect |
if TRUE the text can be visualized by 'plotly' |
set_color |
color of set labels ("black") |
set_size |
size of set labels (NA) |
label |
format of region labels, select one from c("count","percent","both","none") |
label_alpha |
set 0 to remove the background of region labels |
label_geom |
layer of region labels, choose from c("label", "text") |
label_color |
color of region labels ("black") |
label_size |
size of region labels (NA) |
label_percent_digit |
number of digits when formatting percent label (0) |
label_txtWidth |
width of text used in showing intersect members, will be ignored unless show_intersection is TRUE (40) |
edge_lty |
line type of set edges ("solid") |
edge_size |
line width of set edges (1) |
force_upset |
if TRUE, will always produce Upset plot no matter how many sets have (FALSE) |
nintersects |
number of intersects. If NULL, all intersections will show. |
order.intersect.by |
'size', 'name', or "none" |
order.set.by |
'size', 'name', or "none" |
relative_height |
the relative height of top panel in upset plot |
relative_width |
the relative width of left panel in upset plot |
... |
useless |
From version 1.4.4, 'ggVennDiagram' will plot a upset plot when the number of sets is more than 7. Besides, user can switch to a upset plot with 'upset_plot()' function. Please check the document of this function.
A ggplot object
library(ggVennDiagram) x = list(A=1:5,B=2:7,C=3:6,D=4:9) ggVennDiagram(x) # 4d venn ggVennDiagram(x[1:3]) # 3d venn ggVennDiagram(x[1:2]) # 2d venn
library(ggVennDiagram) x = list(A=1:5,B=2:7,C=3:6,D=4:9) ggVennDiagram(x) # 4d venn ggVennDiagram(x[1:3]) # 3d venn ggVennDiagram(x[1:2]) # 2d venn
Launch Reactor Data Shiny App
launch_app()
launch_app()
a shiny app
overlap
returns the same elements of the sets in a Venn
object.
overlap(venn, slice = "all") ## S4 method for signature 'Venn' overlap(venn, slice = "all")
overlap(venn, slice = "all") ## S4 method for signature 'Venn' overlap(venn, slice = "all")
venn |
(Required) A |
slice |
(Optional) The names or the indices of sets of interest. Default is "all", meaning the intersection will be calculated for all the sets. |
A vector showing the intersection of the sets.
venn = Venn(list(letters[1:10], letters[3:12], letters[6:15])) overlap(venn) overlap(venn, slice = c(1, 2))
venn = Venn(list(letters[1:10], letters[3:12], letters[6:15])) overlap(venn) overlap(venn, slice = c(1, 2))
This is for viewing the shape id and appearance of the shape.
plot_shape_edge(x)
plot_shape_edge(x)
x |
a VennPlotData object |
a ggplot object
shape = get_shape_by_id("301") plot_shape_edge(shape)
shape = get_shape_by_id("301") plot_shape_edge(shape)
These shapes are mainly collected from the package venn
, and VennDiagram
.
For Venn plot with more than 4 sets, it is usually impossible to plot with
simple circle or ellipse. So we need to use a predefined coordinates in plot.
plot_shapes()
plot_shapes()
Shape 101, 201, 301, 401, 402, 501, 502, 601 and 701 are from venn
Shape 401f is from VennDiagram
see data-raw/shapes.R
to find how we incorporate these data.
plot_shapes()
plot_shapes()
plot codes
plot_venn( data, show_intersect = FALSE, set_color = "black", set_size = NA, label = "both", label_geom = "label", label_alpha = 0.5, label_color = "black", label_size = NA, label_percent_digit = 0, label_txtWidth = 40, edge_lty = "solid", edge_size = 1, ... )
plot_venn( data, show_intersect = FALSE, set_color = "black", set_size = NA, label = "both", label_geom = "label", label_alpha = 0.5, label_color = "black", label_size = NA, label_percent_digit = 0, label_txtWidth = 40, edge_lty = "solid", edge_size = 1, ... )
data |
plot data |
show_intersect |
if TRUE the text can be visualized by 'plotly' |
set_color |
color of set labels ("black") |
set_size |
size of set labels (NA) |
label |
format of region labels, select one from c("count","percent","both","none") |
label_geom |
layer of region labels, choose from c("label", "text") |
label_alpha |
set 0 to remove the background of region labels |
label_color |
color of region labels ("black") |
label_size |
size of region labels (NA) |
label_percent_digit |
number of digits when formatting percent label (0) |
label_txtWidth |
width of text used in showing intersect members, will be ignored unless show_intersection is TRUE (40) |
edge_lty |
line type of set edges ("solid") |
edge_size |
line width of set edges (1) |
... |
useless |
ggplot object, or plotly object if show_intersect is TRUE
join the shape data with set data
plotData_add_venn(plotData, venn)
plotData_add_venn(plotData, venn)
plotData |
a VennPlot object that stores plot shapes |
venn |
a Venn object that stores set values |
upsetPlotData
S3 method for upsetPlotData
S3 method for VennPlotData
## S3 method for class 'upsetPlotData' print(x, ...) ## S3 method for class 'VennPlotData' print(x, ...)
## S3 method for class 'upsetPlotData' print(x, ...) ## S3 method for class 'VennPlotData' print(x, ...)
x |
a VennPlotData object |
... |
useless |
get plot data
process_data(venn, nsets = NULL, shape_id = NULL, type = NULL) ## S4 method for signature 'Venn' process_data(venn, nsets = length(venn@sets), shape_id = NULL, type = NULL)
process_data(venn, nsets = NULL, shape_id = NULL, type = NULL) ## S4 method for signature 'Venn' process_data(venn, nsets = length(venn@sets), shape_id = NULL, type = NULL)
venn |
a Venn object |
nsets |
This parameter will be set automatically. |
shape_id |
apply filter to internal shapes. i.e. shape_id = "601" |
type |
apply filter to internal shapes. i.e. type = "polygon" |
This function will conduct set operations and combine the outputs will stored shapes, thus produce a dataset for plot in next step.
Run 'get_shapes()' to show all the characteristics of available shapes. Run 'plot_shapes()' to view those shapes.
## Not run: venn = Venn(list(A=1:3,B=2:5,C=4:8)) data = process_data(venn) ## End(Not run)
## Not run: venn = Venn(list(A=1:3,B=2:5,C=4:8)) data = process_data(venn) ## End(Not run)
process upset data
process_upset_data( venn, nintersects = 30, order.intersect.by = "size", order.set.by = "name", specific = TRUE )
process_upset_data( venn, nintersects = 30, order.intersect.by = "size", order.set.by = "name", specific = TRUE )
venn |
a class Venn object |
nintersects |
number of intersects. If NULL, all intersections will show. |
order.intersect.by |
'size', 'name', or "none" |
order.set.by |
'size', 'name', or "none" |
specific |
whether return ONLY specific items for a subset, default is TRUE |
ggVennDiagram, by default, only return the specific subsets of a region. However, sometimes, we want to show all the overlapping items for two or more sets. For example: https://github.com/gaospecial/ggVennDiagram/issues/64 Therefore, we add a 'specific' switch to this function. While 'specific = FALSE', the seperator will be changed from "/" to "~", and all the overlapping items will be returned. This feature is useful in plotting upset plot.
a upsetPlotData object
tidyr::separate_longer_delim
Implement of tidyr::separate_longer_delim
separate_longer_delim(df, col, delim)
separate_longer_delim(df, col, delim)
df |
a data.frame |
col |
column |
delim |
delimeter |
a data.frame
a collection of geometric shapes, which defined the edge and label of sets in a Venn plot.
use plot_shapes()
to see some of them.
a list with several slots see "?VennPlotData".
The venn
datasets authored by Adrian Dusa (https://CRAN.R-project.org/package=venn).
Parameters used to generate fancy four set ellipses are adopted from VennDiagram
(https://CRAN.R-project.org/package=VennDiagram).
check and format slice name
slice_idx(venn, slice)
slice_idx(venn, slice)
venn |
a Venn object |
slice |
a numeric or character vector |
the index of Venn (numeric vector) or "all"
unite
returns the union of the sets in a Venn
object.
unite(venn, slice = "all") ## S4 method for signature 'Venn' unite(venn, slice = "all")
unite(venn, slice = "all") ## S4 method for signature 'Venn' unite(venn, slice = "all")
venn |
(Required) A |
slice |
(Optional) The names or the indices of sets of interest. Default is "all", meaning the union will be calculated for all the sets. |
A vector showing the union of the sets.
venn = Venn(list(letters[1:10], letters[3:12], letters[6:15])) unite(venn) unite(venn, slice = c(1, 2))
venn = Venn(list(letters[1:10], letters[3:12], letters[6:15])) unite(venn) unite(venn, slice = c(1, 2))
This function generate a upset plot by creating a composite plot which contains subplots generated by ggplot2.
plot_upset( venn, nintersects = NULL, order.intersect.by = c("size", "name", "none"), order.set.by = c("size", "name", "none"), relative_height = 3, relative_width = 0.3, top.bar.color = "grey30", top.bar.y.label = NULL, top.bar.show.numbers = TRUE, top.bar.numbers.size = 3, sets.bar.color = "grey30", sets.bar.show.numbers = FALSE, sets.bar.x.label = "Set Size", intersection.matrix.color = "grey30", specific = TRUE, ... )
plot_upset( venn, nintersects = NULL, order.intersect.by = c("size", "name", "none"), order.set.by = c("size", "name", "none"), relative_height = 3, relative_width = 0.3, top.bar.color = "grey30", top.bar.y.label = NULL, top.bar.show.numbers = TRUE, top.bar.numbers.size = 3, sets.bar.color = "grey30", sets.bar.show.numbers = FALSE, sets.bar.x.label = "Set Size", intersection.matrix.color = "grey30", specific = TRUE, ... )
venn |
a class Venn object |
nintersects |
number of intersects. If NULL, all intersections will show. |
order.intersect.by |
'size', 'name', or "none" |
order.set.by |
'size', 'name', or "none" |
relative_height |
the relative height of top panel in upset plot |
relative_width |
the relative width of left panel in upset plot |
top.bar.color |
default is "grey30" |
top.bar.y.label |
default is NULL |
top.bar.show.numbers |
default is TRUE |
top.bar.numbers.size |
text size of numbers |
sets.bar.color |
default is "grey30" |
sets.bar.show.numbers |
default is FALSE |
sets.bar.x.label |
default is "Set Size" |
intersection.matrix.color |
default is "grey30" |
specific |
whether only include specific items in subsets, default is TRUE. |
... |
useless |
an upset plot
list = list(A = sample(LETTERS, 20), B = sample(LETTERS, 22), C = sample(LETTERS, 14), D = sample(LETTERS, 30, replace = TRUE)) venn = Venn(list) plot_upset(venn) plot_upset(venn, order.intersect.by = "name") plot_upset(venn, nintersects = 6)
list = list(A = sample(LETTERS, 20), B = sample(LETTERS, 22), C = sample(LETTERS, 14), D = sample(LETTERS, 30, replace = TRUE)) venn = Venn(list) plot_upset(venn) plot_upset(venn, order.intersect.by = "name") plot_upset(venn, nintersects = 6)
Prepare Venn data
process_set_data(venn) process_region_data(venn, sep = "/", specific = TRUE)
process_set_data(venn) process_region_data(venn, sep = "/", specific = TRUE)
venn |
a Venn object |
sep |
name and id separator for intersections |
specific |
whether return ONLY specific items for a subset, default is TRUE |
ggVennDiagram, by default, only return the specific subsets of a region. However, sometimes, we want to show all the overlapping items for two or more sets. For example: https://github.com/gaospecial/ggVennDiagram/issues/64 Therefore, we add a 'specific' switch to this function. While 'specific = FALSE', the seperator will be changed from "/" to "~", and all the overlapping items will be returned. This feature is useful in plotting upset plot.
a tibble
x = list( A = sample(letters, 8), B = sample(letters, 8), C = sample(letters, 8), D = sample(letters, 8) ) venn = Venn(x) process_set_data(venn) process_region_data(venn)
x = list( A = sample(letters, 8), B = sample(letters, 8), C = sample(letters, 8), D = sample(letters, 8) ) venn = Venn(x) process_set_data(venn) process_region_data(venn)
Get VennPlotData slot
venn_regionedge(obj) venn_regionlabel(obj) venn_setedge(obj) venn_setlabel(obj) venn_set(obj) venn_region(obj)
venn_regionedge(obj) venn_regionlabel(obj) venn_setedge(obj) venn_setlabel(obj) venn_set(obj) venn_region(obj)
obj |
a list that stores all the data from the S3 class 'VennPlotData' object |
a tibble
venn = Venn(list(A=1:5,B=2:7,C=3:6,D=4:9)) obj = process_data(venn) venn_regionlabel(obj) # return regionLabel data venn_regionedge(obj) # return regionEdge data venn_setlabel(obj) # return setLabel data venn_setedge(obj) # return setEdge data venn_set(obj) # set items venn_region(obj) # region items
venn = Venn(list(A=1:5,B=2:7,C=3:6,D=4:9)) obj = process_data(venn) venn_regionlabel(obj) # return regionLabel data venn_regionedge(obj) # return regionEdge data venn_setlabel(obj) # return setLabel data venn_setedge(obj) # return setEdge data venn_set(obj) # set items venn_region(obj) # region items
Venn
is a S4 class to represent multiple sets.Print user-friendly information of a Venn object
Venn(sets, names = NULL) ## S4 method for signature 'ANY' Venn(sets, names = NULL) ## S4 method for signature 'Venn' show(object)
Venn(sets, names = NULL) ## S4 method for signature 'ANY' Venn(sets, names = NULL) ## S4 method for signature 'Venn' show(object)
sets |
(Required) A list containing vectors in the same class. If a vector contains duplicates they will be discarded. If the list doesn't have names the sets will be named as "Set_1", "Set_2", "Set_3" and so on. |
names |
names of sets |
object |
a Venn class object |
A Venn
object.
sets
A list
object containing vectors in the same type.
names
The names of the sets
if it has names. If the list
doesn't have names, the sets will be named as "Set_1", "Set_2", "Set_3" and
so on.
venn = Venn(list(letters[1:10], letters[3:12], letters[6:15])) print(venn)
venn = Venn(list(letters[1:10], letters[3:12], letters[6:15])) print(venn)
An S3 class constructor of representing Venn plot components.
VennPlotData(x)
VennPlotData(x)
x |
data source of a VennPlotData object |
shapeId
shape id
type
type of shape
nsets
number of sets
setEdge
a data.frame, the coordinates of set edges, can be retrieved by venn_setedge()
setLabel
a data.frame, the coordinates of set labels, can be retrieved by venn_setlabel()
regionEdge
a data.frame, the coordinates of different regions, can be retrieved by venn_regionedge()
regionLabel
a data.frame, the centroid of the regions, where region labels anchored, can be retrieved by venn_regionlabel()
setData
a data.frame, the set data provided by user, can be retrieved by venn_set()
regionData
a data.frame, the region data that calculated by ggVennDiagram
, can be retrieved by venn_region()
Import venn shape coordinates
vensets()
vensets()
a data frame