簡體   English   中英

如何從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

原始數據如上所示。 客戶沒有規則,即一些客戶在第 2 階段之后結賬,而一些客戶必須在第 3 階段和第 3 階段之后結帳。

基本上,我想繪制如下客戶階段的網絡 map:

checkin > stage2 > stage3 > checkout
             |
            checkout

如何在 R 中做到這一點?
使用networkD3 package嘗試如下:

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 package 很有用。 將您的示例數據轉換為 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 主頁在這里

這是使用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()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM