Creates a Venn or Euler diagram layer that is added to an hd() object
via +. The diagram is described by a list of set entries supplied via
the sets argument.
Usage
hd_geom_venn(
sets,
series_name = "Venn Diagram",
label_font_size = "14px",
value_suffix = "",
use_names = FALSE,
show_legend = FALSE,
legend_offset_y = 0.6,
legend_height = 0.85,
...
)Arguments
- sets
A list of set entries. See Data format above. Use
hd_venn_set()andhd_venn_intersect()to build entries.- series_name
Character. Series label shown in the Highcharts chart. Default
"Venn Diagram". Highcharter only.- label_font_size
Character. CSS font-size for set labels. Default
"14px". Highcharter only.- value_suffix
Character. Appended to each region's value label. Default
""(no suffix). static mode only.- use_names
Logical. If
TRUE, display human-readablenamevalues instead of set ids in labels and legend. DefaultFALSE. static mode only.- show_legend
Logical. If
TRUE, render a legend beneath the diagram. DefaultFALSE. static mode only.- legend_offset_y
Numeric (0–1). Vertical shift of the venn grob. Increase to move diagram upward and create more space for legend.
- legend_height
Numeric (0–1). Relative height of the venn grob. Decrease to allocate more space for legend.
- ...
Additional arguments forwarded to
hd_make().
Venn vs Euler
Supply only the intersections that actually exist in your data. Highcharts will separate circles that share no intersection entry, producing an Euler diagram automatically. No separate geom is needed. Note: mathematically impossible layouts (e.g. three pairs all intersect but the triple intersection is zero) cannot be rendered and will error. If chart fails to display as intended, it might be due to the intersection is larger than the single sets that contain them.
Data format
sets is a list of named lists. Each element describes either a single
set or an intersection between sets:
list(
# Single sets - must have name and value
list(sets = list("A"), name = "Animals", value = 5),
list(sets = list("B"), name = "Four legs", value = 4),
list(sets = list("C"), name = "Mineral", value = 2),
# Intersections - sets contains two or more names, value is overlap size
list(sets = list("A", "B"), value = 3)
)Use hd_venn_set() and hd_venn_intersect() to build entries without
typing the nested list structure by hand.
Extending the set list
Because sets is a plain R list, it can be built and extended
programmatically with c() or append():
base <- list(
hd_venn_set("A", "Animals", 5),
hd_venn_set("B", "Four legs", 4)
)
extended <- c(base, list(hd_venn_intersect(c("A","B"), 3)))
hd(mode = "dynamic") +
hd_geom_venn(sets = extended)Mode differences
- dynamic
Uses Highcharts' native
"venn"series type. All features (labels, tooltips, interactivity) work out of the box.series_nameandlabel_font_sizeare highcharter-only options.- static
Requires the eulerr package (preferred) or ggVennDiagram in
Suggests. Install withinstall.packages("eulerr").
Examples
# Build the set list with helper constructors
my_sets <- list(
hd_venn_set("A", "Animals", value = 5),
hd_venn_set("B", "Four legs", value = 4),
hd_venn_set("C", "Mineral", value = 2),
hd_venn_intersect(c("A", "B"), value = 3)
)
# Dynamic (interactive)
hd(mode = "dynamic") +
hd_geom_venn(sets = my_sets) +
hd_opts(title = "Animals and Minerals")
# Static - requires eulerr or ggVennDiagram
hd(mode = "static") +
hd_geom_venn(sets = my_sets) +
hd_opts(title = "Animals and Minerals")
# Extend a base list programmatically
base_sets <- list(
hd_venn_set("A", "Oslo", value = 120),
hd_venn_set("B", "Bergen", value = 95)
)
extended <- c(base_sets, list(
hd_venn_intersect(c("A", "B"), "Both", value = 40)
))
hd(mode = "dynamic") +
hd_geom_venn(sets = extended) +
hd_opts(title = "City overlap")
# Convert a data.frame to a venn set list with hd_venn_df() function
id = c("A", "B", "A,B")
name = c("Oslo", "Bergen", "Both")
value = c(80, 30, 10)
n = c(120, 95, 40)
type = c(rep("set", 2), "intersect")
df <- data.frame(type, id, name, value, n)
df2 <- hd_venn_df(df, output = "spec")
hd_make(df2, "venn", hd_opts(title = "City overlap"), mode = "dynamic")
