如何从 R 中的数据框列绘制网络图?

How to draw network diagram from data frame columns in R?

我有一个客户数据框。我想将客户阶段绘制为网络图。示例数据如下。

cust_id     checkin time           stage2                     stage3              checkout time
12345   2019-01-01 07:02:50     2019-01-01 07:23:25        2019-01-01 07:23:22  2019-01-01 08:37:43
56789   2019-01-01 07:25:21     2019-01-01 07:35:29        2019-01-01 07:35:27  2019-01-01 09:36:06
43256   2019-01-01 07:27:22     2019-01-01 07:42:49        NA                   2019-01-01 09:34:55
34567   2019-01-01 07:22:15     2019-01-01 08:25:35        2019-01-01 07:26:02  2019-01-01 09:00:40
89765   2019-01-01 08:29:35     2019-01-01 08:30:58        NA                   2019-01-01 09:02:48
23456   2019-01-01 08:54:12     2019-01-01 09:18:46        2019-01-01 09:08:34  2019-01-01 09:46:38

原始数据如上所示。客户没有规则,即一些客户在 stage2 之后结帐,一些客户必须进入 stage 3 并在 stage 3 之后结帐。

基本上,我想绘制如下客户阶段的网络图:

checkin > stage2 > stage3 > checkout
             |
            checkout

如何在 R 中做到这一点?
使用 networkD3 包尝试如下:

library(igraph)
library(networkD3)
p <- simpleNetwork(df, height="100px", width="100px",        
                   Source = 1,                 # column number of source
                   Target = 5,                 # column number of target
                   linkDistance = 10,          # distance between node. Increase this value to have more space between nodes
                   charge = -900,                # numeric value indicating either the strength of the node repulsion (negative value) or attraction (positive value)
                   fontSize = 14,               # size of the node names
                   fontFamily = "serif",       # font og node names
                   linkColour = "#666",        # colour of edges, MUST be a common colour for the whole graph
                   nodeColour = "#69b3a2",     # colour of nodes, MUST be a common colour for the whole graph
                   opacity = 0.9,              # opacity of nodes. 0=transparent. 1=no transparency
                   zoom = T                    # Can you zoom on the figure?
)

p

请帮我找到通往它的路。

我发现 DiagrammeR 包很有用。将您的示例数据转换为 Diagrammer 使用的格式会很尴尬,所以我手动完成了。

library(DiagrammeR)

# Manually represent your data as nodes and edges
nodes <- create_node_df(n=5, label=c("Check in", "Stage 1", "Stage 2", "Stage 3", "Check out"))
edges <- create_edge_df(from = c(1, 2, 3), to = c(2, 3, 4))
lastStage <- c(4, 4, 3, 4, 3, 3)

# Create the base graph
graph <- create_graph(nodes_df=nodes, edges_df=edges) 

# Produce the customer graphs
networks <- lapply(lastStage, function(x) graph %>% add_edge(from=x, to=5) %>% render_graph())
networks[[2]]

举个例子,

您对图表的外观有相当大的控制权。 DiagrammeR 主页是 here.

这是一种使用 networkD3...

的解决方案
library(tidyverse)
library(lubridate)
library(networkD3)

data <- 
  tribble(
  ~cust_id, ~checkin.time,         ~stage2,               ~stage3,               ~checkout.time,
  12345,    "2019-01-01 07:02:50", "2019-01-01 07:23:25", "2019-01-01 07:23:22", "2019-01-01 08:37:43",
  56789,    "2019-01-01 07:25:21", "2019-01-01 07:35:29", "2019-01-01 07:35:27", "2019-01-01 09:36:06",
  43256,    "2019-01-01 07:27:22", "2019-01-01 07:42:49", NA,                    "2019-01-01 09:34:55",
  34567,    "2019-01-01 07:22:15", "2019-01-01 08:25:35", "2019-01-01 07:26:02", "2019-01-01 09:00:40",
  89765,    "2019-01-01 08:29:35", "2019-01-01 08:30:58", NA,                    "2019-01-01 09:02:48",
  23456,    "2019-01-01 08:54:12", "2019-01-01 09:18:46", "2019-01-01 09:08:34", "2019-01-01 09:46:38"
  ) %>% 
  mutate(across(!cust_id, ~ymd_hms(.x, tz = "UTC")))

data %>% 
  select(-cust_id) %>% 
  mutate(across(.fns = ~if_else(is.na(.x), NA_character_, cur_column()))) %>% 
  mutate(row = row_number()) %>%
  mutate(origin = .[[1]]) %>%
  gather("column", "source", -row, -origin) %>%
  mutate(column = match(column, names(data))) %>%
  filter(!is.na(source)) %>% 
  arrange(row, column) %>%
  group_by(row) %>%
  mutate(target = lead(source)) %>%
  ungroup() %>%
  filter(!is.na(source) & !is.na(target)) %>%
  mutate(target = if_else(target == "checkout.time", paste0(target, " from ", source), target)) %>% 
  select(source, target, origin) %>%
  group_by(source, target, origin) %>%
  summarise(count = n()) %>%
  ungroup() %>%
  simpleNetwork()