block by timelyportfolio 762aa11bb5def57dc27f

R visualization of arules with arulesViz + igraph + visNetwork

Full Screen

Interactive arules with arulesViz and visNetwork

In response to visNetwork issue #6, here is some code to demonstrate how we might interactively visualize an arules object in R using the new htmlwidget visNetwork. Fortunately, arulesViz has already done a lot of the plumbing for us.

# visNetwork view of arules

library(arules)
library(arulesViz)
library(visNetwork)
library(igraph)

# using example from arulesViz vignette
#   http://cran.r-project.org/web/packages/arulesViz/vignettes/arulesViz.pdf

data("Groceries")
rules <- apriori(Groceries, parameter=list(support=0.001, confidence=0.5))

subrules2 <- head(sort(rules, by="lift"), 10)

ig <- plot( subrules2, method="graph", control=list(type="items") )

# saveAsGraph seems to render bad DOT for this case
tf <- tempfile( )
saveAsGraph( subrules2, file = tf, format = "dot" )
# clean up temp file if desired
#unlink(tf)

# let's bypass saveAsGraph and just use our igraph
ig_df <- get.data.frame( ig, what = "both" )
visNetwork(
  nodes = data.frame(
    id = ig_df$vertices$name
    ,value = ig_df$vertices$support # could change to lift or confidence
    ,title = ifelse(ig_df$vertices$label == "",ig_df$vertices$name, ig_df$vertices$label)
    ,ig_df$vertices
  )
  , edges = ig_df$edges
) %>%
  visEdges( style = "arrow" ) %>%
  visOptions( highlightNearest = T )