使用R的networkD3包画可交互的网络图

  • R d3network包
  • 力导向图
  • 桑基图(Sankeydiagram)
  • Reingold-Tilford树型图
  • 反馈与建议

R d3network包

通过Christopher Gandrud编写的d3network包可以轻松创建基于Htmlwidgets框架的网络图。它目前支持三种类型的网络图:

  • 力导向图,可以显示复杂的网络划分关系;
  • 桑基图(Sankeydiagram),利于展现分类维度间的相关性,以流的形式呈现共享同一类别的元素数量。特别适合表达集群的发展,比如展示特定群体的人数分布等;
  • Reingold-Tilford树型图,可以把一个树形结构的数据,用不重叠、紧凑、分层的形式展示出来。

下面通过例子展示这三种类型的网络图。

力导向图

首先载入networkD3包,然后创建src源、target目标两个向量,整合成数据框networkData,最后就可以通过simpleNetwork函数画出一个简单的力导向图(见例1);此外,也可以通过自有数据框MisLinks、MisNodes创建复杂一点的力导向图(见例2)。

#例1
# 载入软件包
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, zoom = TRUE) # 画图 simpleNetwork(networkData)

 

ABCDJEFGHI

 

#例2
# 直接载入数据包(数据框)
data(MisLinks) data(MisNodes) # 画图 forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source", Target = "target", Value = "value", NodeID = "name", Group = "group", opacity = 0.8, zoom = TRUE)

 

 

桑基图(Sankeydiagram)

桑基图(Sankeydiagram),利于展现分类维度间的相关性,以流的形式呈现共享同一类别的元素数量。特别适合表达集群的发展,比如展示特定群体的人数分布等;可以直接使用网上下载的JSON数据创建桑基图,例子如下:

 

Agricultural 'waste'Bio-conversionLiquidLossesSolidGasBiofuel importsBiomass importsCoal importsCoalCoal reservesDistrict heatingIndustryHeating and cooling - commercialHeating and cooling - homesElectricity gridOver generation / exportsH2 conversionRoad transportAgricultureRail transportLighting & appliances - commercialLighting & appliances - homesGas importsNgasGas reservesThermal generationGeothermalH2HydroInternational shippingDomestic aviationInternational aviationNational navigationMarine algaeNuclearOil importsOilOil reservesOther wastePumped heatSolar PVSolar ThermalSolarTidalUK land based bioenergyWaveWind

 

Reingold-Tilford树型图

RT树型图可以把一个树形结构的数据,用不重叠、紧凑、分层的形式展示出来。

URL <- paste0(
        "https://cdn.rawgit.com/christophergandrud/networkD3/", "master/JSONdata//flare.json") ## 格式转化 Flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE) # 使用部分数据1-3 Flare$children = Flare$children[1:3] #环形的RT树,如下图: radialNetwork(List = Flare, fontSize = 10, opacity = 0.9)

 

flareanalyticsclusterAgglomerativeClusterCommunityStructureHierarchicalClusterMergeEdgegraphBetweennessCentralityLinkDistanceMaxFlowMinCutShortestPathsSpanningTreeoptimizationAspectRatioBankeranimateEasingFunctionSequenceinterpolateArrayInterpolatorColorInterpolatorDateInterpolatorInterpolatorMatrixInterpolatorNumberInterpolatorObjectInterpolatorPointInterpolatorRectangleInterpolatorISchedulableParallelPauseSchedulerSequenceTransitionTransitionerTransitionEventTweendataconvertersConvertersDelimitedTextConverterGraphMLConverterIDataConverterJSONConverterDataFieldDataSchemaDataSetDataSourceDataTableDataUtil

 

#直接生成一棵从左到右的树,如下图:
diagonalNetwork(List = Flare, fontSize = 10, opacity = 0.9)

 

flareanalyticsclusterAgglomerativeClusterCommunityStructureHierarchicalClusterMergeEdgegraphBetweennessCentralityLinkDistanceMaxFlowMinCutShortestPathsSpanningTreeoptimizationAspectRatioBankeranimateEasingFunctionSequenceinterpolateArrayInterpolatorColorInterpolatorDateInterpolatorInterpolatorMatrixInterpolatorNumberInterpolatorObjectInterpolatorPointInterpolatorRectangleInterpolatorISchedulableParallelPauseSchedulerSequenceTransitionTransitionerTransitionEventTweendataconvertersConvertersDelimitedTextConverterGraphMLConverterIDataConverterJSONConverterDataFieldDataSchemaDataSetDataSourceDataTableDataUtil

 

详细资料,参见http://christophergandrud.github.io/networkD3/

 

转载于:https://www.cnblogs.com/zhp2016/p/6005546.html

你可能感兴趣的:(json)