Skip to content

Commit

Permalink
Speed up examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhahsler committed Nov 19, 2021
1 parent 60c8e40 commit 112d6f8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 74 deletions.
12 changes: 9 additions & 3 deletions man/inspectDT.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ rules_df$count <- NULL
head(rules_df)
inspectDT(rules_df)

# save a datatable as a html page.
# Save HTML widget as web page
p <- inspectDT(rules)
htmlwidgets::saveWidget(p, "arules.html", selfcontained = FALSE)
browseURL("arules.html")}
# Note: self-contained seems to make the browser slow.

# inspect the widget
browseURL("arules.html")

# clean up
unlink(c("arules.html", "arules_files"), recursive = TRUE)}
}
\keyword{ print }
\keyword{ print }
122 changes: 51 additions & 71 deletions man/plot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ and interactive HTML widget-based visualizations.
render the plot.
The "default" engine uses (mostly) \pkg{ggplot2}.
Other engines include "base" (base R plots),
"grid", "interactive", "plotly", "visnetwork", "igraph", "graphviz", and "htmlwidget".
"grid", "interactive", "plotly", "visnetwork", "igraph", "graphviz",
and "htmlwidget" (which can be embedded in RMarkdown).
Note that not all engines are available for all methods.
Specify "help" to get
a complete list of available engines for the selected visualization method.
Expand Down Expand Up @@ -165,6 +166,9 @@ Hahsler, M. (2016), Grouping association rules using lift. In C. Iyigun, R. Mogh
\code{\link[seriation]{seriate}} in \pkg{seriation}
}
\examples{
# Note: To speed example execution, not all examples are not run when using example("plot").
# Use example("plot", run.dontrun = TRUE) to run all examples.

data(Groceries)
rules <- apriori(Groceries, parameter=list(support = 0.001, confidence = 0.8))
rules
Expand All @@ -177,43 +181,38 @@ plot(rules, method = "help")
# List the available engines for method "scatterplot"
plot(rules, method = "scatterplot", engine = "help")

\dontrun{
# List control parameters for scatterplot with engine "ggplot2"
\dontrun{plot(rules, method = "scatterplot", engine = "ggplot2", control = "help")}
plot(rules, method = "scatterplot", engine = "ggplot2", control = "help")}


## Scatter plot
# Display a scatter plot using two quality measures
plot(rules)

# Limit the number of rules displayed to the 100 with the largest value for
# lift (shading measure)
plot(rules, limit = 100)

# Scatter plot with custom measures
plot(rules, measure = c("support", "lift"), shading = "confidence")
# Scatter plot with custom measures and limiting the plot to the 100 with the
# largest value for for the shading measure.
plot(rules, measure = c("support", "lift"), shading = "confidence", limit = 100)

\dontrun{
# Custom color scale, labels, theme and no title (ggplot2)
library(ggplot2)
plot(rules, engine = "ggplot2", main = NULL) +
plot(rules, engine = "ggplot2", main = NULL, limit = 100) +
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")
plot(rules, engine = "grid", control = list(col = gray.colors(100)))

plot(rules, engine = "base")

# Interactive scatter plot using the grid engine (selected rules are returned)
\dontrun{sel <- plot(rules, engine = "interactive")}
if(interactive()) {
sel <- plot(rules, engine = "interactive")

# Create a html widget for interactive visualization (uses plotly)
\donttest{plot(rules, engine = "htmlwidget")}
plot(rules, engine = "htmlwidget")}
}

# Two-key plot (a scatter plot with shading = "order")
plot(rules, method = "two-key plot")
plot(rules, method = "two-key plot", limit = 100)


## Matrix shading
Expand All @@ -227,47 +226,36 @@ subrules
# 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")

\dontrun{
# Interactive matrix plot
# * Engine interactive: identify rules by clicking on them (click outside to end)
# * Engine htmlwidget: hoover over rules to identify
\dontrun{plot(subrules, method = "matrix", engine = "interactive")}
\donttest{plot(subrules, method = "matrix", engine = "htmlwidget")}
if(interactive()) {
plot(subrules, method = "matrix", engine = "interactive")
plot(subrules, method = "matrix", engine = "htmlwidget")
}}


## Grouped matrix plot
# Default engine is ggplot2
plot(rules, method="grouped matrix")
plot(rules, method="grouped matrix", shading = "confidence", k = 5)
plot(rules, method="grouped matrix", k = 5)

\dontrun{
# Create a htmlwidget
\donttest{plot(rules, method = "grouped matrix", engine = "htmlwidget")}

# 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"))
plot(rules, method = "grouped matrix", engine = "htmlwidget")

# Interactive grouped matrix plot
\dontrun{sel <- plot(rules, method="grouped matrix", engine = "interactive")}

if(interactive()){
sel <- plot(rules, method="grouped matrix", engine = "interactive")}
}

## Graph representation
# Default engine is ggplot2 with ggraph. Associations are represented as nodes.
# We limit the number of rules to the 10 with the larges
# lift (measure used for shading)
plot(subrules, method = "graph", limit = 10)

\dontrun{
# Circular layout (see? ggraph for the meaning of the arguments)
plot(subrules, method = "graph", layout = 'linear', circular = TRUE, limit = 10)

Expand All @@ -276,6 +264,7 @@ plot(subrules, method = "graph", layout = 'igraph',
ggraphdots = list(algorithm = 'graphopt', spring.const = 1, mass = 10), limit = 10)

# Specify edge and node representation
library(ggplot2)
plot(subrules, method = "graph",
control = list(
edges = ggraph::geom_edge_link(
Expand All @@ -296,22 +285,14 @@ plot(subrules, method = "graph",
# ggplot also can represent associations as edges. Here a rules is represented as a set of
# arrows going from the LHS items to the RHS item.
plot(subrules, method = "graph", asEdges = TRUE, limit = 10)
plot(subrules, method = "graph", asEdges = TRUE, circular = FALSE, limit = 10)
plot(subrules, method = "graph", asEdges = TRUE, circular = FALSE, limit = 10)}

# Engine igraph
plot(subrules, method = "graph", engine = "igraph", limit = 10)

# Custom colors
plot(subrules, method = "graph", engine = "igraph",
nodeCol = grey.colors(10), edgeCol = grey(.7), alpha = 1,
limit = 10)

# No shading for lift, set node color to gray and add
# labels for support
plot(subrules, method = "graph", engine = "igraph",
shading = NULL, nodeCol = grey(.5), measureLabels = TRUE,
limit = 10)

# Use plot_options to alter any aspect of the graph
# (see: https://igraph.org/r/doc/plot.common.html)
plot(subrules, method = "graph", engine = "igraph",
Expand All @@ -324,22 +305,19 @@ plot(subrules, method = "graph", engine = "igraph",

# igraph layout generators can be used (see ? igraph::layout_)
plot(subrules, method="graph", engine = "igraph", layout = igraph::in_circle(), limit = 10)
plot(subrules, method="graph", engine = "igraph",
layout = igraph::with_graphopt(spring.const = 5, mass = 50), limit = 10)

\dontrun{
# Graph rendering using engine graphviz
\dontrun{plot(subrules, method = "graph", engine = "graphviz", limit = 10)}
plot(subrules, method = "graph", engine = "graphviz", limit = 10)

if(interactive()) {
# Default interactive plot (using igraph's tkplot)
\dontrun{plot(subrules, method = "graph", engine = "interactive", limit = 10)}
plot(subrules, method = "graph", engine = "interactive", limit = 10)

# Interactive graph as a html widget (using igraph layout)
\donttest{
plot(subrules, method = "graph", engine = "htmlwidget", limit = 10)
plot(subrules, method = "graph", engine = "htmlwidget",
igraphLayout = "layout_in_circle", limit = 10)
}

plot(subrules, method = "graph", engine = "htmlwidget", limit = 10)
plot(subrules, method = "graph", engine = "htmlwidget",
igraphLayout = "layout_in_circle", limit = 10)}}

## Parallel coordinates plot
plot(subrules, method = "paracoord", limit = 10)
Expand All @@ -352,7 +330,6 @@ plot(subrules, method = "paracoord", limit = 10)
oneRule <- sample(rules, 1)
inspect(oneRule)
plot(oneRule, method = "doubledecker", data = Groceries)
plot(oneRule, method = "mosaic", data = Groceries)


## Visualizing itemsets
Expand All @@ -361,28 +338,31 @@ 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", limit = 20)
plot(itemsets, method = "graph", asEdges = TRUE, limit = 20)
plot(itemsets, method = "graph", asEdges = TRUE, circular = FALSE, limit = 20) +
theme(plot.margin = margin(10, 10, 30, 20, "mm"))
plot(itemsets, method = "graph", limit = 10)

plot(itemsets, method = "graph", engine = "igraph", limit = 20)
\dontrun{
plot(itemsets, method = "graph", asEdges = TRUE, limit = 10)
plot(itemsets, method = "graph", asEdges = TRUE, circular = FALSE, limit = 10) +
theme(plot.margin = margin(10, 10, 30, 20, "mm"))}

plot(itemsets, method = "paracoord", alpha = .5, limit = 20)
plot(itemsets, method = "paracoord", alpha = .5, limit = 10)

# 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
\dontrun{
# Save HTML widget as web page
p <- plot(rules, engine = "html")
htmlwidgets::saveWidget(p, "arules.html", selfcontained = FALSE)
browseURL("arules.html")}
# Note: self-contained seems to make the browser slow.

# inspect the widget
browseURL("arules.html")

# clean up
unlink(c("arules.html", "arules_files"), recursive = TRUE)}
}
\keyword{ hplot }

0 comments on commit 112d6f8

Please sign in to comment.