cda

数字化人才认证

您的位置:首页 > 大数据时代 > 几种吊炸天的图,还可以动,了解下??(力导向图,桑基图、树图、弦图)

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

2020-07-15

数据可视化能够很好地展示我们数据分析的结果,对于平常工作中,一份酷炫的可视化图表也能成为我们在工作汇报时的加分项,可是很多小伙伴对于怎样制作吸引人眼球可视化图表却不知晓,今天小编终于为大家找到了集中好看的力导向图,桑基图、树图、弦图的制作方法,特来分享给大家。

以下文章来源于: AI入门学习公众号

作者:伍正祥

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

1、力导向图(force Network)

2、桑基图(Sankey diagrams) 

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

4、弦图(chord Diagram)  

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

#工作空间设置

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

#包加载

library(networkD3)

#http://christophergandrud.github.io/networkD3/#simple

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 <- 'https://raw.githubusercontent.com/christophergandrud/d3Network/sankey/JSONdata/energy.json'

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(

 "https://gist.githubusercontent.com/mbostock/4063550/raw/a05a94858375bd0ae023f6950a2b13fac5127637/flare.json",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')


完 谢谢观看

分享
收藏

OK