固定R/networkD3软件包中Sankey流程图的顺序 [英] Fixing the order of a Sankey flow graph in R / networkD3 package

查看:300
本文介绍了固定R/networkD3软件包中Sankey流程图的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Sankey流程图可视化美国各州的排名变化(即相对顺序的变化).我正在使用包的问题,​​并想出了以下:

I would like to visualize a rank change (i.e. change of the relative order) of US states using a Sankey flow graph. I'm using the networkd3 package and came up with the following:

library(dplyr)
library(networkD3)

df <- data_frame(origins=state.name[1:10], destinations=state.name[1:10])
lab <- c(df$origins, df$destinations)
nodes <- data.frame(node=c(0:9), name=lab)
links <- data.frame(source=c(0:9), target=c(10:19), value=rep(1,10))
sankeyNetwork(Links = links, Nodes = nodes, Source = 'source', 
              Target = 'target', Value = 'value', NodeID = 'name')

此代码段生成以下图形: 美国各州的Sankey流程图

This snippet produces the following graph: Sankey flow graph of US states

我现在可以手动更改相对顺序.但是,我想知道是否可以将订单固定在右侧并放置例如阿拉巴马州排名第3,加利福尼亚州排名第1,等等...

I can change the relative order by hand now. However, I wonder whether it is possible to fix the order on the right-hand side and put e.g. Alabama on rank 3, California on rank 1, etc ...

推荐答案

如果在sankeyNetwork()中设置iterations = 0,则将有效地禁用自动确定节点位置的算法(这是函数),然后按照在Nodes数据框中出现的顺序放置节点.

If you set iterations = 0 in sankeyNetwork(), you will effectively disable the algorithm which automatically determines the node placement (which is the primary purpose of the sankeyNetwork() function), and the nodes will be placed in the order that they appear in the Nodes dataframe.

library(networkD3)

states <- state.name[1:10]
ranks <- sample.int(10)

nodes <- data.frame(name = c(states, states[ranks]))
links <- data.frame(source = 1:10 - 1, target = order(ranks) + 10 - 1, value = 1)

sankeyNetwork(Links = links, Nodes = nodes, Source = 'source', 
              Target = 'target', Value = 'value', NodeID = 'name',
              iterations = 0)

这篇关于固定R/networkD3软件包中Sankey流程图的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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