Search code examples
rggplot2data-visualizationarules

Item frequency plots from object of class transactions in ggplot2


Package arules enables to quickly read transactions data (for mining association rules and frequent itemsets) which is achieved with a dedicated transactions-class. We can also quickly create quite raw and boring item frequency plots using itemFrequencyPlot function from this package (with some arguments for customisation):

library(arules)
data("Groceries")
itemFrequencyPlot(Groceries, topN = 20)

enter image description here

I would like to recreate such plots with more visual flexibility in ggplot2 without overly excessive coding but I can't find any out-of-box dedicated functions to achieve this. Any suggestions?


Solution

  • I can't find any out-of-box dedicated functions to achieve this

    Well, I guess you can build one like this:

    library(arules)
    library(tidyverse)
    data("Groceries")
    itemFrequencyGGPlot <- function(x, topN) {
      library(tidyverse)
      x %>%
        itemFrequency %>%
        sort %>%
        tail(topN) %>%
        as.data.frame %>%
        tibble::rownames_to_column() %>%
        ggplot(aes(reorder(rowname, `.`),`.`)) + 
        geom_col() + 
        coord_flip()
    }  
    itemFrequencyGGPlot(Groceries, 20)
    

    enter image description here