ggplot2 中 class 交易对象的项目频率图

Item frequency plots from object of class transactions in ggplot2

包裹arules enables to quickly read transactions data (for mining association rules and frequent itemsets) which is achieved with a dedicated transactions-class。我们还可以使用此包中的 itemFrequencyPlot 函数(带有一些自定义参数)快速创建相当原始和无聊的项目频率图:

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

我想在 ggplot2 中以更具视觉灵活性的方式重新创建此类图,而无需过度编码,但我找不到任何开箱即用的专用函数来实现此目的。有什么建议吗?

I can't find any out-of-box dedicated functions to achieve 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)