Create a custom ggplot2
geom for drawing diamond plots, which are used to
visualize lattice structures. This is particularly useful for representing
association rules and their ancestor–descendant relationships in a concise
graphical form.
Usage
geom_diamond(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
na.rm = FALSE,
linetype = "solid",
nudge_x = 0,
nudge_y = 0.125,
show.legend = NA,
inherit.aes = TRUE,
...
)
Arguments
- mapping
Aesthetic mappings, usually created with
ggplot2::aes()
.- data
A data frame containing the lattice structure to be plotted.
- stat
The statistical transformation to apply, default is
"identity"
.- position
Position adjustment for the geom, default is
"identity"
.- na.rm
Logical; if
TRUE
, missing values are silently removed.- linetype
Line type used for edges. Defaults to
"solid"
.- nudge_x
Horizontal nudge applied to label positions.
- nudge_y
Vertical nudge applied to label positions.
- show.legend
Logical; should a legend be drawn? Defaults to
FALSE
.- inherit.aes
Logical; if
TRUE
, inherit default aesthetics from the plot. Defaults toTRUE
.- ...
Additional arguments passed on to
ggplot2::layer()
.
Value
A ggplot2
layer object representing a diamond plot. This layer can
be added to an existing ggplot
object.
Details
In a diamond plot, nodes (diamonds) represent items or conditions in the lattice, while edges represent inclusion (subset) relationships between them. The geom combines node and edge rendering with flexible aesthetic options for labels and positioning.
Examples
if (FALSE) { # \dontrun{
library(ggplot2)
data("iris")
rules <- dig_associations(part)
# select some rule to visualize the ancestors
rule <- rules[1000, , drop = FALSE]
# prepare data for visualization of rule ancestors
ante <- parse_condition(rule$antecedent)[[1]]
cons <- parse_condition(rule$consequent)[[1]]
res <- dig_associations(part,
antecedent = all_of(ante),
consequent = all_of(cons),
min_length = 0,
max_length = Inf,
min_coverage = 0,
min_support = 0,
min_confidence = 0,
measures = c("lift", "conviction"),
max_results = Inf)
# convert all columns into dummy logical variables
part <- partition(iris, .breaks = 3)
# find all antecedents with Sepal for rules with consequent Species=setosa
rules <- dig_associations(part,
antecedent = starts_with("Sepal"),
consequent = `Species=setosa`,
min_length = 0,
max_length = Inf,
min_coverage = 0,
min_support = 0,
min_confidence = 0,
measures = c("lift", "conviction"),
max_results = Inf)
# add abbreviated condition for labeling
rules$abbrev <- shorten_condition(rules$antecedent)
# plot the lattice of rules
ggplot(rules) +
aes(condition = antecedent, fill = confidence,
linewidth = confidence, size = coverage,
label = abbrev) +
geom_diamond()
} # }