基于Apriori的关联规则可视化:交互式图及转Plotly平行坐标图问询
Hey there! Awesome questions—both have straightforward, actionable solutions tailored to your Apriori rule set (20 rules, lengths 4-7 per your summary(topRules) output). Let’s dive in:
1. Interactive Graph Visualization for Apriori Rules
Absolutely—you can build an interactive node-edge graph where nodes represent item sets, edges represent directed rules (from LHS to RHS), and hover tooltips display key quality metrics like support, confidence, and lift. This makes it easy to explore how antecedents connect to consequents at a glance.
Here’s a R implementation using arules, igraph, and plotly to create the interactive graph:
# Load required libraries library(arules) library(igraph) library(plotly) # Convert your rules object to a structured data frame rules_df <- DATAFRAME(topRules, separate = TRUE) # Build edge data: map LHS item sets to RHS, include quality metrics edges <- data.frame( from = sapply(rules_df$LHS, paste, collapse = " + "), to = sapply(rules_df$RHS, paste, collapse = " + "), support = rules_df$support, confidence = rules_df$confidence, lift = rules_df$lift ) # Create a directed graph object rule_graph <- graph_from_data_frame(edges, directed = TRUE) # Convert to interactive Plotly figure interactive_graph <- plot_ly( rule_graph, edge = list(color = "#999999"), node = list(color = "#2c3e50", size = 12), layout = list(title = "Interactive Apriori Rule Network", hovermode = "closest") ) %>% add_edges( hoverinfo = "text", text = ~paste( "Rule: ", from, " → ", to, "<br>", "Support: ", round(support, 4), "<br>", "Confidence: ", round(confidence, 4), "<br>", "Lift: ", round(lift, 4) ) ) %>% add_nodes( hoverinfo = "text", text = ~name ) # Display the interactive plot interactive_graph
This plot lets you zoom, pan, and hover over edges to view full rule details—perfect for spotting high-impact rule relationships.
2. Convert Regular Paracoord Plots to Interactive Plotly Paracoord Plots
Yes! You can take the core logic of a standard parallel coordinates plot (like the one from arulesViz::plot(topRules, method="paracoord")) and rebuild it as a fully interactive Plotly version. The key is reshaping your rule data into a format Plotly’s parcoords component can parse.
Here’s how to do it in R:
# Load libraries library(arules) library(plotly) library(dplyr) # Convert rules to a binary matrix: each column = item, 1 = present in rule rule_binary_matrix <- LIST(topRules, decode = TRUE) %>% lapply(function(x) as.data.frame(t(table(x)))) %>% bind_rows() %>% mutate_all(~replace(., is.na(.), 0)) # Add your quality metrics (support, confidence, lift) to the matrix rule_matrix_with_metrics <- cbind(rule_binary_matrix, quality(topRules)) # Build interactive parallel coordinates plot interactive_paracoord <- plot_ly( type = 'parcoords', line = list( color = rule_matrix_with_metrics$lift, colorscale = 'Plasma', showscale = TRUE, colorbar = list(title = "Rule Lift Score") ), dimensions = lapply(colnames(rule_matrix_with_metrics), function(col) { list( label = col, values = rule_matrix_with_metrics[[col]] ) }) ) %>% layout( title = "Interactive Parallel Coordinates for Apriori Rules", hovermode = "x unified" ) # Display the plot interactive_paracoord
This interactive version lets you:
- Hover over any line to view all values (item presence, support, confidence, lift) for that rule
- Click and drag on axis labels to filter rules by specific items or metric thresholds
- Adjust the color scale to highlight rules by support, confidence, or lift based on your needs
内容的提问来源于stack exchange,提问作者Mümin

