龙空技术网

几种吊炸天的图,还可以动(力导向图,桑基图、树图、弦图)

小伍哥聊风控 196

前言:

现时姐妹们对“d3 力导向图”都比较着重,兄弟们都想要知道一些“d3 力导向图”的相关文章。那么小编在网上收集了一些有关“d3 力导向图””的相关资讯,希望同学们能喜欢,同学们快快来了解一下吧!

给大家分享4种很厉害的图,基于R语言networkD3包实现,学会了可以大大提高可视化水平,R语言实现非常简单,几行代码就搞定,先看图。

1、力导向图(force Network)

2、桑基图(Sankey diagrams)

3、辐射状网络图(Radial networks)

4、弦图(chord Diagram)

下面一步步实现其中的每个图

#工作空间设置

setwd("C:/Users/wuzhengxiang/Desktop/networkD3")

#包加载

library(networkD3)

#

1、力导向图(force Network)

1)简单网络图

#创建数据

src = c("A", "A", "A", "A", "B", "B", "C", "C", "D",'I')

target = c("B", "C", "D", "J", "E", "F", "G", "H", "I",'A')

networkData = data.frame(src, target)

#直接一个函数即可画出简单图,下面第一个图

simpleNetwork(networkData)

#换个颜色和字体大小,下面第二个图

simpleNetwork(networkData,nodeColour = "#FF69B4",fontSize = 12)

2)复杂网络图

#载入数据

data(MisLinks)

data(MisNodes)

#创建一个简单的力图

forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source", Target = "target", Value = "value", NodeID = "name",Group = "group", opacity = 1, zoom = F, bounded = T)

# 当鼠标点击变大大的图

MyClickScript = 'd3.select(this).select("circle").transition().duration(750).attr("r", 30)'

forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",Target = "target", Value = "value", NodeID = "name",Group = "group", opacity = 1, zoom = F, bounded = T,

clickAction = MyClickScript)

# 节点大小赋值

forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",Target = "target", Value = "value", NodeID = "name", Nodesize = 'size', radiusCalculation = "d.nodesize",

Group = "group", opacity = 1, legend = T, bounded = F)

2、桑基图(Sankey diagrams)

URL <- ''

Energy <- jsonlite::fromJSON(URL)

# Plot

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

#动态

#静态

3、树状图 (Tree networks)

1)radialNetwork

Flare <- jsonlite::fromJSON(

"",simplifyDataFrame = FALSE)

hc <- hclust(dist(USArrests), "ave")

radialNetwork(List = Flare, fontSize = 10, opacity = 0.9, margin=0)

radialNetwork(as.radialNetwork(hc))

2)其他类型的树图(不会翻译,弯的树图?)

diagonalNetwork(List = Flare, fontSize = 10, opacity = 0.9, margin=0)

diagonalNetwork(as.radialNetwork(hc), height = 700, margin = 50)

3)dendroNetwork(不会翻译,直的树图?)

hc <- hclust(dist(USArrests), "ave")

dendroNetwork(hc, height = 600)

dendroNetwork(hc, treeOrientation = "vertical")

dendroNetwork(hc, height = 600, linkType = "diagonal")

dendroNetwork(hc, treeOrientation = "vertical", linkType = "diagonal")

dendroNetwork(hc, textColour = c("red", "green", "orange")[cutree(hc, 3)],height = 600)

dendroNetwork(hc, textColour = c("red", "green", "orange")[cutree(hc, 3)], treeOrientation = "vertical")

4、弦图(chordDiagram)

hairColourData = matrix(c(11975, 1951, 8010, 1013,5871, 10048, 16145, 990,8916, 2060, 8090, 940, 2868, 6171, 8045, 6907), nrow = 4)

chordNetwork(hairColourData, width = 500, height = 500,colourScale = c("#000000", "#FFDD89", "#957244", "#F26223"))

#保存为html文件saveNetwork

library(magrittr)

simpleNetwork(networkData) %>% saveNetwork(file = 'Net1.html')

forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",Target = "target", Value = "value", NodeID = "name",Nodesize = 'size', radiusCalculation = " Math.sqrt(d.nodesize)+6",Group = "group", opacity = 1, legend = T, bounded = T) %>%

saveNetwork(file = 'forceNetwork_01.html')

标签: #d3 力导向图