# NOT RUN {
data(Groceries)
rules <- apriori(Groceries, parameter=list(support = 0.001, confidence = 0.8))
rules
## Getting help
# There are many method, plotting engines and all of them have different control parameters. Use
# "help" to get help. List available methods for the object rules:
plot(rules, method = "help")
# List the available engines for method "scatterplot"
plot(rules, method = "scatterplot", engine = "help")
# List control parameters for scatterplot with engine "ggplot2"
# }
# NOT RUN {
plot(rules, method = "scatterplot", engine = "ggplot2", control = "help")
# }
# NOT RUN {
## Scatter plot
# Display a scatter plot using two quality measures
plot(rules)
# Scatter plot with custom measures
plot(rules, measure = c("support", "lift"), shading = "confidence")
# Custom color scale, , labels, theme and no title (ggplot2)
library(ggplot2)
plot(rules, engine = "ggplot2", main = NULL) +
scale_color_gradient2(low = "red", mid = "gray90", high = "blue",
midpoint = 1, limits = c(0,12)) +
labs(x = "Supp.", y = "Conf.", color = "Lift") +
theme_classic()
# Scatter plot using other engines
plot(rules, engine = "grid")
library(colorspace) # for sequential_hcl
plot(rules, engine = "grid", control = list(col=sequential_hcl(100)))
plot(rules, engine = "base")
# Interactive scatter plot using the grid engine (selected rules are returned)
# }
# NOT RUN {
sel <- plot(rules, engine = "interactive")
# }
# NOT RUN {
# Create a html widget for interactive visualization (uses plotly)
# }
# NOT RUN {
plot(rules, engine = "htmlwidget")
# }
# NOT RUN {
# Two-key plot (a scatter plot with shading = "order")
plot(rules, method = "two-key plot")
## Matrix shading
# Display rules as a matrix with RHS itemsets as rows and LHS itemsets as columns
# works better with small sets of rules
subrules <- subset(rules, lift > 5)
subrules
# 2D matrix with shading (ggplot2). The LHS and RHS are reordered so
# that rules with similar lift are displayed close to each other.
plot(subrules, method = "matrix")
# Reorder rules differently
plot(subrules, method = "matrix", control = list(reorder = "none"))
plot(subrules, method = "matrix", control = list(reorder = "support/confidence"))
plot(subrules, method = "matrix", control = list(reorder = "similarity"))
# Use other engines
plot(subrules, method = "matrix", engine = "grid")
plot(subrules, method = "matrix", engine = "base")
plot(subrules, method = "matrix", engine = "3d")
# Interactive matrix plot
# * Engine interactive: identify rules by clicking on them (click outside to end)
# * Engine htmlwidget: hoover over rules to identify
# }
# NOT RUN {
plot(subrules, method = "matrix", engine = "interactive")
# }
# NOT RUN {
plot(subrules, method = "matrix", engine = "htmlwidget")
# }
# NOT RUN {
## Grouped matrix plot
# Default engine is ggplot2
plot(rules, method="grouped matrix")
plot(rules, method="grouped matrix", shading = "confidence", k = 5)
# Create a htmlwidget
# }
# NOT RUN {
plot(rules, method = "grouped matrix", engine = "htmlwidget")
# }
# NOT RUN {
# Engine grid
plot(rules, method="grouped matrix", engine = "grid")
plot(rules, method="grouped matrix", engine = "grid",
col = grey.colors(10),
gp_labels = grid::gpar(col = "blue", cex=1, fontface="italic"))
# Interactive grouped matrix plot
# }
# NOT RUN {
sel <- plot(rules, method="grouped matrix", engine = "interactive")
# }
# NOT RUN {
## Graph representation
# Graphs only work well with very few rules (we sample a few of the high-lift rules)
subrules2 <- sample(subset(rules, lift > 5), 25)
# Default engine is ggplot2 with ggnetwork
plot(subrules2, method = "graph")
# Specify layout (from igraph) and edge and node representation
# Note: The measures for size and color shading have to be specified in
# the control list in nodes!
plot(subrules2, method="graph",
control = list(
layout = igraph::with_graphopt(),
edges = ggnetwork::geom_edges(color = "grey80",
arrow = arrow(length = unit(5, "pt"), type = "open"), alpha = .5),
nodes = ggnetwork::geom_nodes(aes(size = support, color = lift), na.rm = TRUE),
nodetext = ggnetwork::geom_nodetext(aes(label = label), alpha = .6)
)) +
scale_color_gradient(low = "yellow", high = "red") +
scale_size(range = c(2, 15))
# Engine igraph
plot(subrules2, method = "graph", engine = "igraph")
# Custom colors
plot(subrules2, method = "graph", engine = "igraph",
nodeCol = grey.colors(10), edgeCol = grey(.7), alpha = 1)
# No shading for lift, set node color to gray and add
# labels for support
plot(subrules2, method = "graph", engine = "igraph",
shading = NA, nodeCol = grey(.5), measureLabels = TRUE)
# Use plot_options to alter any aspect of the graph
# (see: https://igraph.org/r/doc/plot.common.html)
plot(subrules2, method = "graph", engine = "igraph",
plot_options = list(
edge.lty = 2,
vertex.label.cex = .6,
margin = c(.1,.1,.1,.1),
asp = .5))
# igraph layout generators can be used (see ? igraph::layout_)
plot(subrules2, method="graph", engine = "igraph", layout=igraph::in_circle())
plot(subrules2, method="graph", engine = "igraph",
layout = igraph::with_graphopt(spring.const = 5, mass = 50))
# Graph rendering using engine graphviz
# }
# NOT RUN {
plot(subrules2, method = "graph", engine = "graphviz")
# }
# NOT RUN {
# Default interactive plot (using igraph's tkplot)
# }
# NOT RUN {
plot(subrules2, method = "graph", engine = "interactive")
# }
# NOT RUN {
# Interactive graph as a html widget (using igraph layout)
# }
# NOT RUN {
plot(subrules2, method = "graph", engine = "htmlwidget")
plot(subrules2, method = "graph", engine = "htmlwidget",
igraphLayout = "layout_in_circle")
# }
# NOT RUN {
## Parallel coordinates plot
plot(subrules2, method = "paracoord")
## Doubledecker and mosaic plot
# Uses functions in package vcd
# Notes: doubledecker and mosaic plots only visualize a single rule
# and the transaction set is needed.
oneRule <- sample(rules, 1)
inspect(oneRule)
plot(oneRule, method = "doubledecker", data = Groceries)
plot(oneRule, method = "mosaic", data = Groceries)
## Visualizing itemsets
data(Groceries)
itemsets <- eclat(Groceries, parameter = list(support = 0.02, minlen = 2))
# default is a scatter plot with ggplot2
plot(itemsets)
plot(itemsets, engine = "grid")
plot(itemsets, engine = "base")
plot(itemsets, method = "graph")
plot(itemsets, method = "graph", engine = "igraph")
plot(itemsets, method = "paracoord", alpha = .5)
## Add more quality measures to use for the scatter plot
quality(itemsets) <- interestMeasure(itemsets, transactions = Groceries)
head(quality(itemsets))
plot(itemsets, measure = c("support", "allConfidence"), shading = "lift")
## Save HTML widget as web page
# }
# NOT RUN {
p <- plot(rules, engine = "html")
htmlwidgets::saveWidget(p, "arules.html", selfcontained = FALSE)
browseURL("arules.html")
# }
# NOT RUN {
# Note: self-contained seems to make the browser slow.
# }
Run the code above in your browser using DataLab