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

Python010

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")

今天我们将通过一个例子来说明如何分析两个定类变量。

文章背景:我们想研究CFPS2010和CFPS2012青少年对自身的职业期望。

如表1,我们将原始的职业期望编码整合成9类(职业编码的大类)和其他。由于我们想分析同一个人在跨轮次调查中职业期望的稳定性情况,故将分析对象定义为在CFPS2010和CFPS2012中都回答了自己对自己职业期望的受访者。如表2所示,进行重编码后的数据是宽数据,样本量是1920,数据集名字为expect。我们在进行后续分析时,要将其转换为绘图所需的其他形式。

⭐分析方式1——列联表、频数与频率

在表3中,我们展示了2010与2012年青少年职业期望的交叉统计情况。同时该表内,也附上了频数(落在各类别中的数据个数)、⽐例(某⼀类别数据占全部数据的⽐值)、百分⽐(将对⽐的基数作为100⽽计算的⽐值,包括百分比、行百分比和列百分比)。

⭐分析方式2——统计图表

分析前色彩讲解:预设渐变色,我们这里介绍2个色彩包。

1)专门生产系列颜色的RColorBrewer包,详见图1中的系列颜色。

library(RColorBrewer)

display.brewer.all()

2)色盲友好的配色方案viridis包,详见图2中的系列颜色。

library(viridis)

?viridis()#可以看到更多对这组包色彩的说明

接下来我们来画图吧~【注:图3-图6中的类目数字的含义:1)国家机关、党群组织、企业、事业单位负责人;2)医生;3)教师;4)专业技术人员(刨除教师和医生);5)办事人员和有关人员;6)商业、服务业人员;7)农、林、牧、渔、水利业生产人员;8)生产、运输设备操作人员及有关人员;9)军人;10)其他。】