geom_venn: Plot a 2-4 way Venn diagram

Description Usage Arguments Value Examples

View source: R/geom_venn.R

Description

Plot a 2-4 way Venn diagram. Use prepare_venn to prepare the input data.frame data in the correct format. See Examples section below for basic usage instructions.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
geom_venn(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  type = "discrete",
  set_name_pos = NULL,
  set_name_colour = NULL,
  set_name_size = 5,
  set_name_face = NULL,
  set_name_family = NULL,
  set_total = FALSE,
  set_total_pos = c(0, -0.15),
  set_total_colour = NULL,
  set_total_size = 4,
  set_total_face = NULL,
  set_total_family = NULL,
  count_colour = "black",
  count_size = 5,
  count_face = NULL,
  count_family = NULL,
  count_nudge = 0.06,
  percentage = TRUE,
  percentage_digits = 1,
  percentage_colour = "black",
  percentage_size = 3,
  percentage_face = NULL,
  percentage_family = NULL,
  percentage_nudge = -count_nudge,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).

stat

The statistical transformation to use on the data for this layer, as a string.

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

...

Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.

type

string, type of Venn diagram to plot. Either "discrete" (the default) or "continuous".

set_name_pos

Optional. data.frame with the same nrow as data and two columns: x and y containing numeric coordinates to precisely customise the position of the set name text.

set_name_colour

string, colour of the set name text (default is "black").

set_name_size

numeric, size of the set name text (default is 5).

set_name_face

string, font face of set name text (default is "plain")

set_name_family

string, font family of set name text (default is "sans")

set_total

logical, should the total number of elements in each set be shown? (default is FALSE).

set_total_pos

Optional. numeric of length 2 specifying the precise position of set total text relative to the center of the set name text (default is c(0, -0.15)).

set_total_colour

string, colour of the set total text (default is "black").

set_total_size

numeric, size of the set total text (default is 3).

set_total_face

string, font face of set total text (default is "plain").

set_total_family

string, font family of set total text (default is "sans").

count_colour

string, colour of the segment count text (default is "black").

count_size

numeric, size of the segment count text (default is 5).

count_face

string, font face of segment count text (default is "plain")

count_family

string, font family of segment count text (default is "sans").

count_nudge

numeric, amount to nudge segment count text in the y direction (default is 0.06).

percentage

logical, should the percentage of elements in each segment be shown? (default is TRUE).

percentage_digits

numeric, number of decimal places to show in the percentage text (default is 1).

percentage_colour

string, colour of the percentage text (default is "black").

percentage_size

numeric, size of the percentage text (default is 3).

percentage_face

string, font face of percentage text (default is "plain").

percentage_family

string, font family of percentage text (default is "sans").

percentage_nudge

numeric, amount to nudge percentage text in the y direction (default is -0.06).

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

Value

Returns a layer ggproto object with geom = GeomVenn.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
library(ggplot2)

# Start with a list of vectors to compare.
# Within each vector, there must not be any duplicated elements.
lst <- list(
  Set1 = c(letters[1:8]),
  Set2 = c(letters[20:26]),
  Set3 = c(letters[8:20])
)

# Use prepare_venn() to convert the list into a data.frame.
# of the correct format. You can add extra columns to the data.frame.
# Here we add a column named fill.
df <- prepare_venn(lst, fill = c("blue", "green", "red"))

# Now we plot a basic Venn diagram
ggplot() +
 geom_venn(aes(set_name = set_name, elements = elements), data = df)

# As this is just a normal ggplot layer we can add whatever we want
# to the plot. Some annotations for example.
ggplot() +
 geom_venn(aes(set_name = set_name, elements = elements), data = df) +
 annotate("curve", x = -1.2, xend = -0.75, y = -0.6, yend = -0.3,
          curvature = 0.3, arrow = arrow(length = unit(2, "mm"))) +
 annotate("text", x = -1.25, y = -0.6, label = "Interesting!", hjust = "right")

# Use theme_void() to get a plain background
ggplot() +
 geom_venn(aes(set_name = set_name, elements = elements), data = df) +
 theme_void()

# Add set totals
ggplot() +
 geom_venn(aes(set_name = set_name, elements = elements),
           data = df, set_total = TRUE) +
 theme_void()

# Remove percentages
ggplot() +
 geom_venn(aes(set_name = set_name, elements = elements),
           data = df, percentage = FALSE, count_nudge = 0) +
 theme_void()

# Add discrete fills to the ellipses
ggplot() +
 geom_venn(aes(set_name = set_name, elements = elements, fill = fill),
           data = df, type = "discrete") +
 theme_void() +
 scale_fill_identity()

# Add continuous fills to the ellipse segments
ggplot() +
 geom_venn(aes(set_name = set_name, elements = elements, fill = count),
           data = df, type = "continuous") +
 theme_void() +
 scale_fill_gradientn(colors = alpha(c("white", "red"), 0.7))

csdaw/ggvd documentation built on Dec. 31, 2021, 6:14 p.m.