实例分析:
### 安装与加载包
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")
关于r语言中均质是什么意思相关资料如下r语言中均质的意思是:
均质也称匀浆,是使悬浮液(或乳化液)体系中的分散物微粒化、均匀化的处理过程,这种处理同时起降低分散物尺度和提高分散物分布均匀性的作用。是食品或化工行业生产中经常要运用的一项技术。