networkD3, 来自 R的D3 JavaScript网络图

分享于 

4分钟阅读

GitHub

  繁體 雙語
Tools for creating D3 JavaScript network graphs from R
  • 源代码名称:networkD3
  • 源代码网址:http://www.github.com/christophergandrud/networkD3
  • networkD3源代码文档
  • networkD3源代码下载
  • Git URL:
    git://www.github.com/christophergandrud/networkD3.git
    Git Clone代码到本地:
    git clone http://www.github.com/christophergandrud/networkD3
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/christophergandrud/networkD3
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    R 中的网络图

    开发版本:0.4.900 0 CRAN VersionBuild StatusCRAN Monthly DownloadsCRAN Total Downloads

    本自述文件包括有关设置和一些基本示例的信息。 有关更多信息,请参见包主页面的

    用法

    以下是 simpleNetwork的一个示例:

    
    library(networkD3)
    
    
    
    # Create fake data
    
    
    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)
    
    
    
    # Plot
    
    
    simpleNetwork(networkData)
    
    
    
    

    这里是 forceNetwork:

    
    # Load data
    
    
    data(MisLinks)
    
    
    data(MisNodes)
    
    
    
    # Plot
    
    
    forceNetwork(Links = MisLinks, Nodes = MisNodes, Source ="source",
    
    
     Target ="target", Value ="value", NodeID ="name",
    
    
     Group ="group", opacity = 0.7,
    
    
     colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);"))
    
    
    
    

    下面是 sankeyNetwork 使用下载的JSON数据 file:

    
    # Recreate Bostock Sankey diagram: http://bost.ocks.org/mike/sankey/
    
    
    # Load energy projection data
    
    
    URL <- paste0("https://cdn.rawgit.com/christophergandrud/networkD3/",
    
    
    "master/JSONdata/energy.json")
    
    
    Energy <- jsonlite::fromJSON(URL)
    
    
    
    # Plot
    
    
    sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source ="source",
    
    
     Target ="target", Value ="value", NodeID ="name",
    
    
     units ="TWh", fontSize = 12, nodeWidth = 30)
    
    
    
    

    与igraph的交互

    你可以使用 igraph 创建网络图数据,这些数据可以用 networkD3 打印。 igraph_to_networkD3 函数将igraph图转换为能很好地使用 networkD3的列表。 例如:

    
    # Load igraph
    
    
    library(igraph)
    
    
    
    # Use igraph to make the graph and find membership
    
    
    karate <- make_graph("Zachary")
    
    
    wc <- cluster_walktrap(karate)
    
    
    members <- membership(wc)
    
    
    
    # Convert to object suitable for networkD3
    
    
    karate_d3 <- igraph_to_networkD3(karate, group = members)
    
    
    
    # Create force directed network plot
    
    
    forceNetwork(Links = karate_d3$links, Nodes = karate_d3$nodes, 
    
    
     Source = 'source', Target = 'target', NodeID = 'name', 
    
    
     Group = 'group')
    
    
    
    

    保存到外部文件

    使用 saveNetwork 将网络保存为独立的HTML file:

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

    注释

    networkD3开始作为 d3Network包的端口到htmlwidgets 框架。 不再支持 d3Network。


    相关文章