网络-visNetwork包绘制炫酷的动态网络图

作者: JeremyL | 来源:发表于2020-09-21 13:04 被阅读0次
    visNetwork

    visNetwork是一个用于网络可视化的R包,使用的是vis.js javascript库(http://visjs.org)。

    visNetwork github : https://github.com/datastorm-open/visNetwork.

    #安装visNetwork

    install.packages("visNetwork")
    
    # devtools::install_github("datastorm-open/visNetwork") for development version
    

    ##查看帮助

    # vignette
    vignette("Introduction-to-visNetwork")
    
    # full javascript documentation
    visDocumentation()
    
    # shiny example
    shiny::runApp(system.file("shiny", package = "visNetwork"))
    

    ##visNetwork至少需要两个信息:

    • 节点,data.frame数据,有id列;
    • 边缘,data.frame数据,至少两列,一列起点信息(from),一列终点信息(to)。
    > require(visNetwork, quietly = TRUE)
    # minimal example
    > nodes <- data.frame(id = 1:3)
    id
    1
    2
    3
    > edges <- data.frame(from = c(1,2), to = c(1,3))
    from to
    1  1
    2  3
    > visNetwork(nodes, edges, width = "100%")
    
    visNetwork

    #简单的单个节点和边定制

    注:这些设置只是针对单个节点或着边;全局所有点或者节点设置需要使用visNodes 或 visEdges。

    ##节点属性设置

    • label : 指定节点标签的列
    • value:指定节点大小
    • group:指定节点所属组,igraph包中有许多包装好的方法可以用于网络类群分析
    • shape:节点形状,例如circle, square ,triangle
    • color:节点颜色
    • title:设置提示信息,当您将鼠标悬停在一个节点上时,提示就会出现
    • shadow:定义一个节点是否有阴影(TRUE/FALSE)

    在node data.frame上添加更多变量。有关可用选项,请参阅visNodes()参数。

    visNodes(
      graph,
      id = NULL,
      shape = NULL,
      size = NULL,
      title = NULL,
      value = NULL,
      x = NULL,
      y = NULL,
      label = NULL,
      level = NULL,
      group = NULL,
      hidden = NULL,
      image = NULL,
      mass = NULL,
      physics = NULL,
      borderWidth = NULL,
      borderWidthSelected = NULL,
      brokenImage = NULL,
      labelHighlightBold = NULL,
      color = NULL,
      fixed = NULL,
      font = NULL,
      icon = NULL,
      shadow = NULL,
      scaling = NULL,
      shapeProperties = NULL,
      heightConstraint = NULL,
      widthConstraint = NULL,
      margin = NULL,
      chosen = NULL
    )
    

    示例:

    > nodes <- data.frame(id = 1:10, 
                        label = paste("Node", 1:10), # add labels on nodes
                        group = c("GrA", "GrB"), # add groups on nodes 
                        value = 1:10, # size adding value
                        shape = c("square", "triangle", "box", "circle", "dot", "star",
                                  "ellipse", "database", "text", "diamond"), # control shape of nodes
                        title = paste0("<p><b>", 1:10,"</b><br>Node !</p>"), # tooltip (html or character)
                        color = c("darkred", "grey", "orange", "darkblue", "purple"),# color
                        shadow = c(FALSE, TRUE, FALSE, TRUE, TRUE)) # shadow
    
    > head(nodes)
    id  label group value    shape                     title    color shadow
    1 Node 1   GrA     1   square <p><b>1</b><br>Node !</p>  darkred  FALSE
    2 Node 2   GrB     2 triangle <p><b>2</b><br>Node !</p>     grey   TRUE
    3 Node 3   GrA     3      box <p><b>3</b><br>Node !</p>   orange  FALSE
    4 Node 4   GrB     4   circle <p><b>4</b><br>Node !</p> darkblue   TRUE
    5 Node 5   GrA     5      dot <p><b>5</b><br>Node !</p>   purple   TRUE
    6 Node 6   GrB     6     star <p><b>6</b><br>Node !</p>  darkred  FALSE
    

    ##边属性设置

    • label:指定边标签的列
    • title:设置提示信息,当您将鼠标悬停在一个边上时,提示就会出现
    • shadow:定义一个节点是否有阴影(TRUE/FALSE)
    • length,width:定义网络内边的长度/宽度
    • arrows:箭头在边上的位置
    • dashes:定义边缘是否应该虚线(TRUE/FALSE)
    • smooth:光滑的线(TRUE/FALSE)

    在edges data.frame上添加更多变量。有关可用选项,请参阅visEdges()参数。

    • 在edge data.frame上添加更多变量。有关可用选项,请参阅visEdges。
    visEdges(
      graph,
      title = NULL,
      value = NULL,
      label = NULL,
      length = NULL,
      width = NULL,
      dashes = NULL,
      hidden = NULL,
      hoverWidth = NULL,
      id = NULL,
      physics = NULL,
      selectionWidth = NULL,
      selfReferenceSize = NULL,
      labelHighlightBold = NULL,
      color = NULL,
      font = NULL,
      arrows = NULL,
      arrowStrikethrough = NULL,
      smooth = NULL,
      shadow = NULL,
      scaling = NULL,
      widthConstraint = NULL,
      chosen = NULL
    )
    

    示例:

    > edges <- data.frame(from = sample(1:10, 8), to = sample(1:10, 8),
                        label = paste("Edge", 1:8), # add labels on edges
                        length = c(100,500), # length
                        arrows = c("to", "from", "middle", "middle;to"), # arrows
                        dashes = c(TRUE, FALSE), # dashes
                        title = paste("Edge", 1:8), # tooltip (html or character)
                        smooth = c(FALSE, TRUE), # smooth
                        shadow = c(FALSE, TRUE, FALSE, TRUE)) # shadow
    > head(edges)
    
    ##   from to  label length    arrows dashes  title smooth shadow
    ## 1    4  5 Edge 1    100        to   TRUE Edge 1  FALSE  FALSE
    ## 2   10  6 Edge 2    500      from  FALSE Edge 2   TRUE   TRUE
    ## 3    7  8 Edge 3    100    middle   TRUE Edge 3  FALSE  FALSE
    ## 4    6  4 Edge 4    500 middle;to  FALSE Edge 4   TRUE   TRUE
    ## 5    3  3 Edge 5    100        to   TRUE Edge 5  FALSE  FALSE
    ## 6    2  1 Edge 6    500      from  FALSE Edge 6   TRUE   TRUE
    
    visNetwork(nodes, edges, width = "100%")
    
    visNetwork

    #全局节点/边配置

    • 使用visNodes和visEdges设置节点和边全局属性,并使用visGroups为每个组添加属性。
    nodes <- data.frame(id = 1:5, group = c(rep("A", 2), rep("B", 3)))
    edges <- data.frame(from = c(2,5,3,3), to = c(1,2,4,2))
    
    visNetwork(nodes, edges, width = "100%") %>% 
      visNodes(shape = "square") %>%                        # square for all nodes
      visEdges(arrows ="to") %>%                            # arrow "to" for all edges
      visGroups(groupname = "A", color = "darkblue") %>%    # darkblue for group "A"
      visGroups(groupname = "B", color = "red")             # red for group "B"
    
    visNetwork

    #网络配置

    • 网络配置可以使用visOptions, visInteraction, visLayout, visHierarchicalLayout, visPhysics:

    ##示例数据

    > nb <- 10
    > nodes <- data.frame(id = 1:nb, label = paste("Label", 1:nb),
     group = sample(LETTERS[1:3], nb, replace = TRUE), value = 1:nb,
     title = paste0("<p>", 1:nb,"<br>Tooltip !</p>"), stringsAsFactors = FALSE)
    id   label group value                 title
    1 Label 1     A     1 <p>1<br>Tooltip !</p>
    2 Label 2     C     2 <p>2<br>Tooltip !</p>
    3 Label 3     C     3 <p>3<br>Tooltip !</p>
    4 Label 4     A     4 <p>4<br>Tooltip !</p>
    5 Label 5     A     5 <p>5<br>Tooltip !</p>
    6 Label 6     B     6 <p>6<br>Tooltip !</p>
    
    > edges <- data.frame(from = trunc(runif(nb)*(nb-1))+1,
     to = trunc(runif(nb)*(nb-1))+1,
     value = rnorm(nb, 10), label = paste("Edge", 1:nb),
     title = paste0("<p>", 1:nb,"<br>Edge Tooltip !</p>"))
      from to     value  label                      title
    1  6 10.569034 Edge 1 <p>1<br>Edge Tooltip !</p>
    7  9  9.755302 Edge 2 <p>2<br>Edge Tooltip !</p>
    4  4 10.618589 Edge 3 <p>3<br>Edge Tooltip !</p>
    5  4  9.240707 Edge 4 <p>4<br>Edge Tooltip !</p>
    3  1  8.859133 Edge 5 <p>5<br>Edge Tooltip !</p>
    4  6 11.003060 Edge 6 <p>6<br>Edge Tooltip !</p>
    

    ##添加图例

    • 你现在可以在节点/边缘上添加更多的自定义图例:
    visNetwork(nodes, edges, width = "100%") %>% visLegend()
    
    添加图例

    ##或传递数据

    visNetwork(nodes, edges, width = "100%") %>% 
      visLegend(useGroups = FALSE, addNodes = data.frame(label = "Nodes", shape = "circle"), 
                addEdges = data.frame(label = "link", color = "black"))
    
    passing data

    ##突出临近点连接(highlight Nearest)

    • 使用highlightNearest选项,单击一个节点可以突出显示节点和边:
    visNetwork(nodes, edges, width = "100%") %>% 
      visOptions(highlightNearest = TRUE)
    
    highlight Nearest

    ##可以设置突出程度

    visNetwork(nodes, edges, width = "100%") %>% 
      visOptions(highlightNearest = list(enabled =TRUE, degree = 2))
    
    control the degree of depth

    ##按节点id选择(Select by node id)

    你也可以选择节点的id/标签与一个列表与nodesIdSelection:

    visNetwork(nodes, edges, width = "100%") %>% 
      visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
    
    Select by node id

    ##通过一列设置选择(Select by a column)

    使用selectedBy选项通过列的值选择一些节点:

    # can be the column you want
    > nodes$sel <- sample(c("sel1", "sel2"), nrow(nodes), replace = TRUE)
    id    label group value                  title  sel
    1  Label 1     A     1  <p>1<br>Tooltip !</p> sel1
    2  Label 2     C     2  <p>2<br>Tooltip !</p> sel1
    3  Label 3     C     3  <p>3<br>Tooltip !</p> sel2
    4  Label 4     A     4  <p>4<br>Tooltip !</p> sel2
    5  Label 5     A     5  <p>5<br>Tooltip !</p> sel2
    6  Label 6     B     6  <p>6<br>Tooltip !</p> sel2
    7  Label 7     B     7  <p>7<br>Tooltip !</p> sel1
    8  Label 8     C     8  <p>8<br>Tooltip !</p> sel1
    9  Label 9     C     9  <p>9<br>Tooltip !</p> sel1
    10 Label 10     A    10 <p>10<br>Tooltip !</p> sel2
    > visNetwork(nodes, edges, width = "100%") %>%
     visOptions(selectedBy = "sel")
    
    Select by a column

    ##有向网络

    visNetwork(nodes, edges, width = "100%") %>% 
      visEdges(arrows = 'from')
    
    Directed Network

    ##自定义导航键(Custom navigation)

    vis.js提出了一些导航工具:

    visNetwork(nodes, edges, width = "100%") %>% 
      visInteraction(navigationButtons = TRUE)
    
    Custom navigation

    ##数据操作

    • 添加一些数据操作工具
    visNetwork(nodes, edges, width = "100%") %>% 
      visOptions(manipulation = TRUE)
    
    Data Manipulation

    ##层次分布

    • 可以根据visHierarchicalLayout和visLayout使用和控制分层布局:
    nodes <- data.frame(id = 1:7)
    
    edges <- data.frame(
      from = c(1,2,2,2,3,3),
      to = c(2,3,4,5,6,7)
    )
    visNetwork(nodes, edges, width = "100%") %>% 
      visEdges(arrows = "from") %>% 
      visHierarchicalLayout() 
    
    Hierarchical Layout
    # same as   visLayout(hierarchical = TRUE) 
    
    visNetwork(nodes, edges, width = "100%") %>% 
      visEdges(arrows = "from") %>% 
      visHierarchicalLayout(direction = "LR")
    
    Hierarchical Layout

    固定网络,不允许缩放

    visNetwork(nodes, edges, width = "100%") %>% 
      visInteraction(dragNodes = FALSE, dragView = FALSE, zoomView = FALSE)
    
    Freeze network

    ##在网络中使用字体图标

    nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
    edges <- data.frame(from = c(1,2), to = c(2,3))
    
    visNetwork(nodes, edges, width = "100%") %>%
      visGroups(groupname = "A", shape = "icon", icon = list(code = "f0c0", size = 75)) %>%
      visGroups(groupname = "B", shape = "icon", icon = list(code = "f007", color = "red")) %>%
      visLegend() %>%
      addFontAwesome()
    
    font awesome icons

    #附加特性

    ##可视化rpart对象

    • visTree函数允许可视化和自定义一个rpart分类和回归树。visTreeEditor允许编辑和返回网络,或visTreeModuleServer使用定制树模块。
    Visualize rpart object

    ##在Shiny中使用

    • visNetworkOutputrenderVisNetwork. 在shiny中,可以通过nodesIdSelection选择显示数据。进一步需要实现更多的功能,可以了解visNetworkProxy
    output$mynetwork <- renderVisNetwork({... visOptions(nodesIdSelection = TRUE)}) # created input$mynetwork_selected
    

    And with selectedBy, new input input$mynetwork_selectedBy.

    ##物理、工具提示、事件

    • 可以控制:
      • physics of network : visPhysics
      • events : visEvents

    ##导出

    network <- visNetwork(nodes, edges, width = "100%")
    visSave(network, file = "network.html")
    

    ##直接使用网络数据(Use DOT language data)

    visNetwork(dot = 'dinetwork {1 -> 1 -> 2; 2 -> 3; 2 -- 4; 2 -> 1 }', width = "100%")
    
    DOT language data

    ##使用gephi json文件

    # don't run here
    visNetwork(gephi = 'WorldCup2014.json')
    

    #参考:

    Itroduction to visNetwork
    visNetwork

    系列文章:
    R语言进行网络分析的基础包 igraph
    networkD3 绘制动态网络

    相关文章

      网友评论

        本文标题:网络-visNetwork包绘制炫酷的动态网络图

        本文链接:https://www.haomeiwen.com/subject/qtrvyktx.html