R语言1----桑基(sankey diagram)图的绘制--sankeyD3

Python017

R语言1----桑基(sankey diagram)图的绘制--sankeyD3,第1张

https://github.com/fbreitwieser/sankeyD3

实例分析:

### 安装与加载包  

install.packages("devtools")

devtools::install_github("fbreitwieser/sankeyD3")

library(sankeyD3)

第一个为链接数据框 links(起点、靶点、权重、链接的特征1、链接的特征1.....);

然后根据links构建第二个为节点数据框nodes(起点与靶点、点的特征1、点的特征........)

nodes <- data.frame(name=c(as.character(links$source), as.character(links$target)) %>% unique())

然后基于nodes数据框构建links中节点的唯一标识符ID,而非根据节点的name

links$IDsource <- match(links$source, nodes$name)-1 

links$IDtarget <- match(links$target, nodes$name)-1

sankeyNetwork( Links = links, Nodes = nodes, Source = "IDsource", Target = "IDtarget",

              Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',

              height=300,width=300,colourScale=JS("d3.scaleOrdinal(d3.schemeCategory10)"),

              numberFormat=".0f",fontSize = 8)  

nodes$color<-sample(c("red","orange","blue","green"),nrow(nodes),replace=T)  #在这里进行随机自定义颜色,当然也可以按照自己的需求进行设置

sankeyNetwork(Links = links, Nodes = nodes,Source = "IDsource", Target = "IDtarget",

              Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',

              height=300,width=300,numberFormat=".0f",fontSize = 8, NodeColor = "color" ) 

也可以根据节点自定义的分类对节点进行颜色的绘制

nodes$group<-rep("水果",nrow(nodes))

nodes$group[nodes$name %in% c("上海","深圳","北京","南京")]<-"城市"

nodes$group[nodes$name %in% c("律师","老师","白领","公务员","记者","化妆师")]<-"职业"

sankeyNetwork(Links = links, Nodes = nodes,Source = "IDsource", Target = "IDtarget",

              Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',

              numberFormat=".0f",fontSize = 8,height=300,width=300,

              NodeGroup="group",colourScale=JS("d3.scaleOrdinal(d3.schemeCategory10)") ) 

对于缎带的颜色设置同理也可以对其进行分组颜色设置(这里按照其统计量进行分组设置,当然也可以按照其他进行分组)

links$group<-rep("A",nrow(links))

links$group[links$weight<500 &links$weight>=100]<-"B"

links$group[links$weight<100]<-"C"

sankeyNetwork(Links = links, Nodes = nodes,Source = "IDsource", Target = "IDtarget",

              Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',

              numberFormat=".0f",fontSize = 8,height=300,width=300,

              NodeGroup="group", LinkGroup = "group",

              colourScale=JS("d3.scaleOrdinal(d3.schemeCategory10)")) 

有时候想要缎带根据其宽度进行一定透明度的变化,可以使用 linkType="path1"参数进行设置

install.packages("webshot")

library(webshot)

 if(!is_phantomjs_installed()){

  install_phantomjs()

}

library(webshot)

p<-sankeyNetwork(Links = links, Nodes = nodes,Source = "IDsource", Target = "IDtarget",

              Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',

              numberFormat=".0f",fontSize = 8,height=300,width=300,

              NodeGroup="group",LinkGroup = "group",

              colourScale=JS("d3.scaleOrdinal(d3.schemeCategory10)"))  

### 将结果存储PDF

saveNetwork(p,"sankey.html")

webshot("sankey.html" , "sankey.pdf")

可视化数据时,网络可以很好展示各个节点之间的关系以及关系的重要性。网络中存在大量的节点和边,因此绘制一个好的网络会有助于信息的解读。networkD3可以自定义网络的各种特征,绘制一个优美的动态网络。

可以使用R包 igraph 构建网络绘制需要的数据,使用 igraph_to_networkD3 函数将数据转换成 networkD3 要求格式的输入数据。

simpleNetwork是networkD3的基础函数,可以快速出图。

绘制网络

forceNetwork有更多的参数,调整参数可以绘制更复杂的网络图。

绘制网络

forceNetwork的数据格式比较麻烦; 尤其是参数Nodes;建议大家使用igraph包 igraph_to_networkD3()网络图所需数据将转换为networkD3数据格式。本文见 #3. 与igraph交互 部分。

调用 igraph 包创建网络图所需数据,igraph_to_networkD3函数转换数据格式, networkD3 绘制图。

参考 networkD3

在 server.R 调用render*Network(*可以是 Simple , Force , Sankey )

在 app.R 文件中调用*NetworkOutput(*可以是 Simple , Force , Sankey )

shiny例子: networkD3-shiny-example

networkD3 :D3 JavaScript Network Graphs from R

networkD3 github

系列文章:

R语言进行网络分析的基础包 igraph

networkD3 绘制动态网络

网络-visNetwork包绘制炫酷的动态网络图

网络-调用R包构建交互式网络可视化的Shiny App