Sankey图在R [英] Sankey diagram in R

查看:1454
本文介绍了Sankey图在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在R的 networkD3 包的帮助下创建一个相当通用的Sankey图。只是为了参考 - 这是包手册中的示例

 库(networkD3)
库(jsonlite)
library(magrittr)

energy< - https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json%>%
fromJSON

sankeyNetwork(Links = energy $ links,
Nodes = energy $ nodes,
Source =source,
Target =target,
Value =value,
NodeID =name,
units =TWh,
fontSize = 12,
nodeWidth = 30)

其结果是:





我相当直接的扩展包括使用以下基础数据构建一个图表:

  links<  -  structure(list(source = structure(c(1L,2L,3L,1L,2L,3L,4L,
5L,4L,5L),
.Label = c(1,2,3,4,5),
class =factor ,1L,2L,2L,2L,3L,3L,4L,
4L),
Label = c(4,5,6,7 $ b class =factor),
value = c(88L,774L,1220L,412L,5335L,96L,3219L,
1580L,111L,7607L)),
row.names = c(NA,10L),
class =data.frame,
.Names = c(source,target,value))

节点< - 结构(list(lab = c(A,B,C,D,E,F,G)),
row.names = c(NA,-7L),
.Names =lab,class =data.frame)


b $ b

这个简单的应用程序选择,使我的数据最接近地反映手动示例。当我运行类似的功能,虽然:

  sankeyNetwork(Links = links,
Nodes = nodes,
Source =source,
Target =target,
Value =value,
NodeID =lab)
pre>

没有发生任何事情。

解决方案

如果您开始编号 source 目标 #首先强制链接到数字的元素,以便我们可以减去1
links []< - lapply(links,function(x)as.numeric(as.character(x)) )
links [,1:2]< - links [,1:2] - 1
sankeyNetwork(links,nodes,'source','target','value',NodeID ='lab ')


Attempting to make a fairly generic Sankey diagram with the help of R's networkD3 package. Just for reference--here's the example from the package's manual

library(networkD3)
library(jsonlite)
library(magrittr)

energy <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json" %>% 
      fromJSON

sankeyNetwork(Links = energy$links, 
              Nodes = energy$nodes, 
              Source = "source",
              Target = "target", 
              Value = "value", 
              NodeID = "name",
              units = "TWh", 
              fontSize = 12, 
              nodeWidth = 30)

which results in:

My fairly straightforward extension consists of building a diagram with the following underlying data:

links <- structure(list(source = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 4L, 
                                         5L, 4L, 5L),
                                       .Label = c("1", "2", "3", "4", "5"),
                                       class = "factor"), 
                    target = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 
                                         4L),
                                       .Label = c("4", "5", "6", "7"),
                                       class = "factor"), 
                    value = c(88L, 774L, 1220L, 412L, 5335L, 96L, 3219L, 
                              1580L, 111L, 7607L)), 
               row.names = c(NA, 10L), 
               class = "data.frame", 
               .Names = c("source", "target", "value"))

nodes <- structure(list(lab = c("A", "B", "C", "D", "E", "F", "G")),
               row.names = c(NA, -7L),
               .Names = "lab", class = "data.frame")

with this simple application chosen so that my data most closely reflects the manual example. When I run the comparable function, though:

sankeyNetwork(Links = links, 
              Nodes = nodes, 
              Source = "source",
              Target = "target", 
              Value = "value", 
              NodeID = "lab")

Nothing happens. What's my mistake?

解决方案

This works fine if you start numbering your source and target at 0:

# First coercing elements of links to numeric, so that we can subtract 1
links[] <- lapply(links, function(x) as.numeric(as.character(x)))
links[, 1:2] <- links[, 1:2] - 1
sankeyNetwork(links, nodes, 'source', 'target', 'value', NodeID='lab')

这篇关于Sankey图在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆