rarules

How can I increase arrow and edge thickness in plot() with method = "graph" from arulesViz in R?


I'm using the arules and arulesViz packages in R to generate and visualize association rules. When I use plot() with method = "graph", the arrows and edges in the graph are extremely thin and hard to see.

Here is a simplfied version of my code:

library(arules)
library(arulesViz)

rules <- apriori(Groceries, parameter = list(supp = 0.01, conf = 0.5))
highLiftRules <- head(sort(rules, by = "lift"), 5)

plot(highLiftRules, method = "graph")

I tried using the control argument, but it doesn't seem to change anything. How can I properly increase the arrow size and edge thickness in this graph?


Solution

  • Playing around with the object, I found that you can set the edge_alpha up to 1 to increase the visibility. Similarly you can set edge_width which makes the arrows thicker following this.

    out1

    #install.packages(c("arules","arulesViz"))
    library(arules)
    library(arulesViz)
    data("Groceries") # load data
    
    rules <- apriori(Groceries, parameter = list(supp = 0.01, conf = 0.5))
    highLiftRules <- head(sort(rules, by = "lift"), 5)
    
    ig <- plot(highLiftRules, method = "graph")
    ig$layers[[1]]$aes_params$edge_alpha = 1 # set arrow alpha up
    ig$layers[[1]]$aes_params$edge_width = 1 # set arrow width
    plot(ig)
    

    giving

    res

    As per Stefan's comment: For more control of the arrows you can use ig$layers[[1]]$geom_params$arrow <- grid::arrow(...), e.g. ig$layers[[1]]$geom_params$arrow <- grid::arrow(angle = 20, length = grid::unit(4, "mm"), type = "closed") will double the length of the arrows.

    2

    Add

    As per user20650's comment: The default plot method seem to use ggplot and addon packages. So It look s like you can add to the plot in the normal ggplot way rather than messing with the grobs, giving a cleaner approach. Thanks again:

    plot(highLiftRules, method = "graph") +
      ggraph::geom_edge_link(
        width = 1,
        alpha = .8,
        arrow = grid::arrow(
          angle = 20,
          length = grid::unit(4, "mm"),
          type = "closed"
        )
      )