Get automatically nodes and links from table for sankey diagram

微笑、不失礼 提交于 2019-12-24 19:33:40

问题


For plotting a sankey diagram nodes and links are required. To get the nodes and links from a data frame one could use for example a count function from the package plyr and use it for each node to count the links between the neighbours but is there another elegant way?

example target, aim is to get nodes and links:

param1 | param2 | param3 |
a      | b      | d      |
w      | c      | d      |
a      | b      | d      |
z      | c      | e      |

#nodes:
nodes = data.frame("name" = 
c(
a, #node 0
w, #node 1
z, #node 2
b, #node 3
c, #node 4
d, #node 5
e  #node 6
))

#links
links = as.data.frame(matrix(c(
0, 3, 2, # from node 0,  to node 3, freq
1, 4, 1,
2, 4, 1,
3, 5, 2,
4, 5, 1,
4, 6, 1,
),
byrow = TRUE, ncol = 3))

回答1:


Using igraph package:

library(dplyr)
library(igraph)

# example data
df1 <- read.table(text="
                  param1 param2 param3
                  a b d
                  w c d
                  a b d
                  z c e", header = TRUE, stringsAsFactors = FALSE)

# make graph
g <- graph_from_data_frame(
  rbind(
    setNames(df1[, 1:2], c("from", "to")),
    setNames(df1[, 2:3], c("from", "to"))))


nodes <- data.frame(id = as.numeric(V(g)),
                    name = V(g)$name)
nodes
#   id name
# 1  1    a
# 2  2    w
# 3  3    z
# 4  4    b
# 5  5    c
# 6  6    d
# 7  7    e

links <- as.data.frame(get.edges(g, E(g))) %>%
  group_by(V1, V2) %>%
  summarise(freq = n()) %>% 
  data.frame()

links
#   V1 V2 freq
# 1  1  4    2
# 2  2  5    1
# 3  3  5    1
# 4  4  6    2
# 5  5  6    1
# 6  5  7    1


来源:https://stackoverflow.com/questions/50600070/get-automatically-nodes-and-links-from-table-for-sankey-diagram

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!