rule_heatmap: Create a Heatmap for Association Rules

View source: R/rule_heatmap.R

rule_heatmapR Documentation

Create a Heatmap for Association Rules

Description

Generates a heatmap visualization of association rules, showing relationships between antecedents and consequents based on a specified metric.

Usage

rule_heatmap(
  rules,
  metric = "confidence",
  graph_title = "",
  graph_title_size = 14,
  x_axis_title = "Antecedents",
  x_axis_title_size = 12,
  x_axis_text_size = 11,
  x_axis_text_angle = 45,
  y_axis_title = "Consequents",
  y_axis_title_size = 12,
  y_axis_text_size = 11,
  y_axis_text_angle = 0,
  legend_title = metric,
  legend_text_size = 8,
  legend_position = "right",
  low_color = "lightblue",
  high_color = "navy",
  include_zero = FALSE
)

Arguments

rules

An object of class rules from the arules package.

metric

A character string specifying the metric to use for coloring the heatmap. Must be one of "confidence", "support", or "lift". Defaults to "confidence".

graph_title

A character string specifying the title of the graph. Defaults to an empty string ("").

graph_title_size

A numeric value specifying the size of the graph title text. Defaults to 14.

x_axis_title

A character string specifying the title for the x-axis. Defaults to "Antecedents".

x_axis_title_size

A numeric value specifying the size of the x-axis title text. Defaults to 12.

x_axis_text_size

A numeric value specifying the size of the x-axis text. Defaults to 11.

x_axis_text_angle

A numeric value specifying the angle of the x-axis text. Defaults to 45.

y_axis_title

A character string specifying the title for the y-axis. Defaults to "Consequents".

y_axis_title_size

A numeric value specifying the size of the y-axis title text. Defaults to 12.

y_axis_text_size

A numeric value specifying the size of the y-axis text. Defaults to 11.

y_axis_text_angle

A numeric value specifying the angle of the y-axis text. Defaults to 0.

legend_title

A character string specifying the title of the legend. Defaults to the value of metric.

legend_text_size

A numeric value specifying the size of the legend text. Defaults to 8.

legend_position

A character string specifying the position of the legend. Possible values are "right" (default), "left", "top", "bottom", or "none".

low_color

A valid R color or hex color code for the lower bound of the gradient. Defaults to "lightblue".

high_color

A valid R color or hex color code for the upper bound of the gradient. Defaults to "navy".

include_zero

A logical value indicating whether to include zero values for missing antecedent-consequent combinations. Defaults to FALSE.

Value

A ggplot object representing the heatmap visualization of the association rules.

Examples

library(arules)
library(tidyr)
data(BrookTrout)

# Discretise data
discrete_bt <- dtize_df(BrookTrout, cutoff="median")

# Generate rules
rules <- apriori(
  discrete_bt,
  parameter = list(supp = 0.01, conf = 0.5, target = "rules"),
  appearance = list(rhs="eDNAConc=high")
)

# Subset ruleset (too many rules won't fit on the heatmap)
rules <- rules %>%
  subset(!is.redundant(., measure = "confidence")) %>%
  subset(is.significant(., alpha = 0.05)) %>%
  sort(by = c("confidence", "lift", "support"))

# Create a heatmap of the rules using confidence as the metric
rule_heatmap(
  rules,
  metric = "confidence",
  graph_title = "Confidence Heatmap"
)

# Create a heatmap of the rules using lift as the metric
rule_heatmap(
  rules,
  metric = "lift",
  graph_title = "Lift Heatmap",
  low_color = "#D4A221",
  high_color = "darkgreen"
)


RulesTools documentation built on April 3, 2025, 5:53 p.m.