networkD3,是一个基于D3引擎的R网络图绘制包,用它可以很简单的绘制高大上的网络图,很容易上手。

安装


# 如果遇见网络问题,可以切换R镜像源
install.packages("networkD3")

一个简单的示例


# 引入R包
library(networkD3)

# 演示数据
src <- c("A", "A", "A", "A",
        "B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J",
            "E", "F", "G", "H", "I")
networkData <- data.frame(src, target)

# 绘图
simpleNetwork(networkData)

绘制桑基图

# 引入R包
library(networkD3)

# 演示数据
# 如果网络不好,无法下载,请移步:https://github.com/christophergandrud/networkD3/tree/master/JSONdata

URL <- paste0(
        "https://cdn.rawgit.com/christophergandrud/networkD3/",
        "master/JSONdata/energy.json")
Energy <- jsonlite::fromJSON(URL)

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

树形图绘制

library(networkD3)

# 下载演示数据,如果网络不好,无法下载,请移步:https://github.com/christophergandrud/networkD3/tree/master/JSONdata

URL <- paste0("https://cdn.rawgit.com/christophergandrud/networkD3/",
              "master/JSONdata//flare.json")
              
# json数据转换
Flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE)

# 绘图
radialNetwork(List = Flare, fontSize = 10, opacity = 0.9)

本文旨在抛砖引玉,更多图形美化请移步参考资料1。

BTW:如果大家有好的图的可视化图片,可以留言给我,下期说不定会有你钟爱的那一款

参考资料:

1.http://christophergandrud.github.io/networkD3/