R语言可视化(四十):网络图绘制

作者: Davey1220 | 来源:发表于2020-12-11 18:53 被阅读0次

    40. 网络图绘制


    清除当前环境中的变量

    rm(list=ls())
    

    设置工作目录

    setwd("C:/Users/Dell/Desktop/R_Plots/40network/")
    

    使用igraph包绘制网络图

    # 安装并加载所需的R包
    #install.packages("igraph")
    library(igraph)
    ## 
    ## Attaching package: 'igraph'
    ## The following objects are masked from 'package:stats':
    ## 
    ##     decompose, spectrum
    ## The following object is masked from 'package:base':
    ## 
    ##     union
    
    # 使用邻接矩阵数据绘制网络图
    # 构建示例数据
    set.seed(10)
    data <- matrix(sample(0:2, 25, replace=TRUE), nrow=5)
    colnames(data) = rownames(data) = LETTERS[1:5]
    # 查看示例数据
    head(data)
    ##   A B C D E
    ## A 2 2 1 2 2
    ## B 0 2 2 1 1
    ## C 1 2 1 2 1
    ## D 2 2 1 1 0
    ## E 1 2 0 2 0
    
    # build the graph object
    # 使用graph_from_adjacency_matrix()函数构建网络图对象
    network <- graph_from_adjacency_matrix(data)
    # 查看对象
    network
    ## IGRAPH 8f56656 DN-- 5 33 -- 
    ## + attr: name (v/c)
    ## + edges from 8f56656 (vertex names):
    ##  [1] A->A A->A A->B A->B A->C A->D A->D A->E A->E B->B B->B B->C B->C B->D
    ## [15] B->E C->A C->B C->B C->C C->D C->D C->E D->A D->A D->B D->B D->C D->D
    ## [29] E->A E->B E->B E->D E->D
    
    # plot it
    # 绘制基础网络图,默认得到无权重有方向的网路图
    plot(network)
    
    image.png
    # 使用关联矩阵数据绘制网络图
    # 构建示例数据
    set.seed(1)
    data <- matrix(sample(0:2, 15, replace=TRUE), nrow=3)
    colnames(data) <- letters[1:5]
    rownames(data) <- LETTERS[1:3]
    # 查看示例数据
    head(data)
    ##   a b c d e
    ## A 0 1 2 2 0
    ## B 2 0 1 2 0
    ## C 0 2 1 0 1
    
    # create the network object
    # 使用graph_from_incidence_matrix()函数构建网络图对像
    network <- graph_from_incidence_matrix(data,directed = T)
    network
    ## IGRAPH 8f5ca19 DN-B 8 18 -- 
    ## + attr: type (v/l), name (v/c)
    ## + edges from 8f5ca19 (vertex names):
    ##  [1] A->b b->A A->c c->A A->d d->A B->a a->B B->c c->B B->d d->B C->b b->C
    ## [15] C->c c->C C->e e->C
    
    # plot it
    plot(network)
    
    image.png
    # 使用边列表数据绘制网络图
    # 构建示例数据
    links <- data.frame(
      source=c("A","A", "A", "A", "A","F", "B"),
      target=c("B","B", "C", "D", "F","A","E")
    )
    links
    ##   source target
    ## 1      A      B
    ## 2      A      B
    ## 3      A      C
    ## 4      A      D
    ## 5      A      F
    ## 6      F      A
    ## 7      B      E
    
    # create the network object
    # 使用graph_from_data_frame()函数构建网络图对像
    network <- graph_from_data_frame(d=links, directed=F) 
    network
    ## IGRAPH 8f63302 UN-- 6 7 -- 
    ## + attr: name (v/c)
    ## + edges from 8f63302 (vertex names):
    ## [1] A--B A--B A--C A--D A--F A--F B--E
    
    # plot it
    plot(network)
    
    image.png
    # 自定义一些参数美化网络图
    # Create data
    set.seed(1)
    data <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.8,0.2)), nc=10)
    head(data)
    ##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    ## [1,]    0    0    1    0    1    0    1    0    0     0
    ## [2,]    0    0    0    0    0    1    0    1    0     0
    ## [3,]    0    0    0    0    0    0    0    0    0     0
    ## [4,]    1    0    0    0    0    0    0    0    0     1
    ## [5,]    0    0    0    1    0    0    0    0    0     0
    ## [6,]    1    0    0    0    0    0    0    1    0     0
    
    # 构建网络图对象,设置mode='undirected'参数构建无方向的网络图
    network <- graph_from_adjacency_matrix(data, mode='undirected', diag=T)
    network
    ## IGRAPH 8f69bc6 U--- 10 15 -- 
    ## + edges from 8f69bc6:
    ##  [1] 1-- 3 1-- 4 1-- 5 1-- 6 1-- 7 2-- 6 2-- 8 3-- 9 4-- 5 4--10 6-- 8
    ## [12] 7-- 8 7--10 8--10 9--10
    
    # Default network
    plot(network)
    
    image.png
    # 使用vertex.参数设置节点的大小,形状和颜色等
    plot(network,
         vertex.color = rgb(0.8,0.2,0.2,0.9), # Node color
         vertex.frame.color = "Forestgreen",  # Node border color
         vertex.shape=c("circle","square"),   # One of “none”, “circle”, “square”, “csquare”, “rectangle” “crectangle”, “vrectangle”, “pie”, “raster”, or “sphere”
         vertex.size=c(15:24),                # Size of the node (default is 15)
         vertex.size2=NA,                     # The second size of the node (e.g. for a rectangle)
    )
    
    image.png
    # 使用vertex.label.参数添加标签,并设置标签字体,颜色和大小等
    plot(network,
         vertex.label=LETTERS[1:10],        # Character vector used to label the nodes
         vertex.label.color=c("red","blue"),
         vertex.label.family="Times",       # Font family of the label (e.g.“Times”, “Helvetica”)
         vertex.label.font=c(1,2,3,4),      # Font: 1 plain, 2 bold, 3, italic, 4 bold italic, 5 symbol
         vertex.label.cex=c(0.5,1,1.5),     # Font size (multiplication factor, device-dependent)
         vertex.label.dist=0,               # Distance between the label and the vertex
         vertex.label.degree=0 ,            # The position of the label in relation to the vertex (use pi)
    )
    
    image.png
    # 使用edge.参数设置边的大小,颜色和箭头等
    plot(network,
         edge.color=rep(c("red","pink"),5), # Edge color
         edge.width=seq(1,10),              # Edge width, defaults to 1
         edge.arrow.size=1,                 # Arrow size, defaults to 1
         edge.arrow.width=1,                # Arrow width, defaults to 1
         edge.lty=c("solid")                # Line type, could be 0 or “blank”, 1 or “solid”, 2 or “dashed”, 3 or “dotted”, 4 or “dotdash”, 5 or “longdash”, 6 or “twodash”
         #edge.curved=c(rep(0,5), rep(1,5)) # Edge curvature, range 0-1 (FALSE sets it to 0, TRUE to 0.5)
    )
    
    image.png
    # 使用layout参数设置网络图的展示类型
    # Create data
    data <- matrix(sample(0:1, 400, replace=TRUE, prob=c(0.8,0.2)), nrow=20)
    head(data)
    ##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
    ## [1,]    0    0    0    0    0    0    0    1    0     0     0     0     1
    ## [2,]    0    0    0    0    0    0    1    1    0     0     1     0     0
    ## [3,]    0    0    0    0    0    0    1    0    1     1     0     1     0
    ## [4,]    0    0    1    0    1    0    0    0    0     0     0     1     1
    ## [5,]    0    0    0    0    0    0    1    0    0     1     0     0     0
    ## [6,]    0    0    0    1    0    0    1    1    0     0     0     0     0
    ##      [,14] [,15] [,16] [,17] [,18] [,19] [,20]
    ## [1,]     0     0     0     0     0     0     0
    ## [2,]     0     1     1     1     0     0     0
    ## [3,]     0     0     0     0     0     0     0
    ## [4,]     1     0     0     1     0     0     0
    ## [5,]     0     0     0     1     1     1     0
    ## [6,]     0     0     0     0     1     0     0
    
    # 构建网络图对象
    network <- graph_from_adjacency_matrix(data , mode='undirected', diag=F )
    network
    ## IGRAPH 8f7dd1e U--- 20 69 -- 
    ## + edges from 8f7dd1e:
    ##  [1]  1-- 8  1--13  1--14  1--19  2-- 7  2-- 8  2--11  2--14  2--15  2--16
    ## [11]  2--17  3-- 4  3-- 7  3-- 8  3-- 9  3--10  3--12  3--17  3--18  4-- 5
    ## [21]  4-- 6  4-- 9  4--10  4--12  4--13  4--14  4--16  4--17  4--18  5-- 7
    ## [31]  5-- 9  5--10  5--13  5--17  5--18  5--19  6-- 7  6-- 8  6--15  6--18
    ## [41]  7-- 9  7--11  7--12  7--18  7--19  7--20  8-- 9  8--11  8--16  8--18
    ## [51]  8--19  8--20  9--11  9--12  9--14  9--18 10--12 10--15 10--18 10--19
    ## [61] 11--14 12--14 12--18 13--14 13--17 14--16 14--20 17--19 18--19
    
    # 绘制不同展示类型的网络图
    plot(network, layout=layout.sphere, main="sphere")
    
    image.png
    plot(network, layout=layout.circle, main="circle")
    
    image.png
    plot(network, layout=layout.random, main="random")
    
    image.png
    plot(network, layout=layout.fruchterman.reingold, main="fruchterman.reingold")
    
    image.png
    # 添加节点的属性特征
    # create data:
    links <- data.frame(
      source=c("A","A", "A", "A", "A","J", "B", "B", "C", "C", "D","I"),
      target=c("B","B", "C", "D", "J","A","E", "F", "G", "H", "I","I"),
      importance=(sample(1:4, 12, replace=T))
    )
    # 构建节点属性表
    nodes <- data.frame(
      name=LETTERS[1:10],
      carac=c( rep("young",3),rep("adult",2), rep("old",5))
    )
    # 查看数据
    head(links)
    ##   source target importance
    ## 1      A      B          4
    ## 2      A      B          3
    ## 3      A      C          3
    ## 4      A      D          1
    ## 5      A      J          3
    ## 6      J      A          3
    
    head(nodes)
    ##   name carac
    ## 1    A young
    ## 2    B young
    ## 3    C young
    ## 4    D adult
    ## 5    E adult
    ## 6    F   old
    
    # Turn it into igraph object
    # 构建网络图对象,vertices参数指定节点属性
    network <- graph_from_data_frame(d=links, vertices=nodes, directed=F) 
    # 查看网络图对象
    network
    ## IGRAPH 8f96792 UN-- 10 12 -- 
    ## + attr: name (v/c), carac (v/c), importance (e/n)
    ## + edges from 8f96792 (vertex names):
    ##  [1] A--B A--B A--C A--D A--J A--J B--E B--F C--G C--H D--I I--I
    
    # 自定义颜色
    # Make a palette of 3 colors
    library(RColorBrewer)
    coul  <- brewer.pal(3, "Set1") 
    # Create a vector of color
    my_color <- coul[as.numeric(as.factor(V(network)$carac))]
    my_color
    ##  [1] "#4DAF4A" "#4DAF4A" "#4DAF4A" "#E41A1C" "#E41A1C" "#377EB8" "#377EB8"
    ##  [8] "#377EB8" "#377EB8" "#377EB8"
    
    # 绘值带节点属性的网络图
    plot(network, vertex.color=my_color)
    # 添加图例
    legend("bottomleft", legend=levels(as.factor(V(network)$carac)), 
           col = coul , bty = "n", pch=20 , pt.cex = 3, 
           cex = 1.5, text.col=coul , horiz = FALSE, 
           inset = c(0.1, 0.1))
    
    image.png
    # 添加边的属性特征
    plot(network, vertex.color=my_color, 
         edge.width=E(network)$importance*2 )
    # 添加图例
    legend("bottomleft", legend=levels(as.factor(V(network)$carac)), 
           col = coul , bty = "n", pch=20 , pt.cex = 3, 
           cex = 1.5, text.col=coul , horiz = FALSE, 
           inset = c(0.1, 0.1))
    
    image.png

    使用ggraph包绘制网络图

    library(ggraph)
    ## Warning: package 'ggraph' was built under R version 3.6.3
    ## Loading required package: ggplot2
    ## Warning: package 'ggplot2' was built under R version 3.6.3
    
    library(igraph)
    library(tidyverse)
    ## -- Attaching packages -------------------------------- tidyverse 1.2.1 --
    ## √ tibble  2.1.3     √ purrr   0.3.2
    ## √ tidyr   1.1.2     √ dplyr   1.0.2
    ## √ readr   1.3.1     √ stringr 1.4.0
    ## √ tibble  2.1.3     √ forcats 0.4.0
    ## Warning: package 'tidyr' was built under R version 3.6.3
    ## Warning: package 'dplyr' was built under R version 3.6.3
    ## -- Conflicts ----------------------------------- tidyverse_conflicts() --
    ## x dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
    ## x purrr::compose()       masks igraph::compose()
    ## x tidyr::crossing()      masks igraph::crossing()
    ## x dplyr::filter()        masks stats::filter()
    ## x dplyr::groups()        masks igraph::groups()
    ## x dplyr::lag()           masks stats::lag()
    ## x purrr::simplify()      masks igraph::simplify()
    
    # 构建示例数据
    # create a data frame giving the hierarchical structure of your individuals
    set.seed(1234)
    d1 <- data.frame(from="origin", to=paste("group", seq(1,10), sep=""))
    d2 <- data.frame(from=rep(d1$to, each=10), to=paste("subgroup", seq(1,100), sep="_"))
    hierarchy <- rbind(d1, d2)
    
    # create a dataframe with connection between leaves (individuals)
    all_leaves <- paste("subgroup", seq(1,100), sep="_")
    connect <- rbind( 
      data.frame( from=sample(all_leaves, 100, replace=T) , to=sample(all_leaves, 100, replace=T)), 
      data.frame( from=sample(head(all_leaves), 30, replace=T) , to=sample( tail(all_leaves), 30, replace=T)), 
      data.frame( from=sample(all_leaves[25:30], 30, replace=T) , to=sample( all_leaves[55:60], 30, replace=T)), 
      data.frame( from=sample(all_leaves[75:80], 30, replace=T) , to=sample( all_leaves[55:60], 30, replace=T)) )
    connect$value <- runif(nrow(connect))
    
    # create a vertices data.frame. One line per object of our hierarchy
    vertices  <-  data.frame(
      name = unique(c(as.character(hierarchy$from), as.character(hierarchy$to))) , 
      value = runif(111)
    ) 
    # Let's add a column with the group of each name. It will be useful later to color points
    vertices$group  <-  hierarchy$from[ match( vertices$name, hierarchy$to ) ]
    
    # 查看示例数据
    head(hierarchy)
    ##     from     to
    ## 1 origin group1
    ## 2 origin group2
    ## 3 origin group3
    ## 4 origin group4
    ## 5 origin group5
    ## 6 origin group6
    
    head(connect)
    ##          from          to     value
    ## 1 subgroup_28 subgroup_85 0.9553637
    ## 2 subgroup_80 subgroup_85 0.3994100
    ## 3 subgroup_22 subgroup_32 0.9770535
    ## 4  subgroup_9 subgroup_77 0.5117742
    ## 5  subgroup_5 subgroup_17 0.4671612
    ## 6 subgroup_38 subgroup_66 0.7238355
    
    head(vertices)
    ##     name      value  group
    ## 1 origin 0.04931545   <NA>
    ## 2 group1 0.30197021 origin
    ## 3 group2 0.51204261 origin
    ## 4 group3 0.71036062 origin
    ## 5 group4 0.28465309 origin
    ## 6 group5 0.46494880 origin
    
    # Create a graph object
    # 构建网络图对象,vertices参数指定节点属性
    mygraph <- graph_from_data_frame( hierarchy, vertices=vertices )
    mygraph
    ## IGRAPH 909f7f2 DN-- 111 110 -- 
    ## + attr: name (v/c), value (v/n), group (v/c)
    ## + edges from 909f7f2 (vertex names):
    ##  [1] origin->group1      origin->group2      origin->group3     
    ##  [4] origin->group4      origin->group5      origin->group6     
    ##  [7] origin->group7      origin->group8      origin->group9     
    ## [10] origin->group10     group1->subgroup_1  group1->subgroup_2 
    ## [13] group1->subgroup_3  group1->subgroup_4  group1->subgroup_5 
    ## [16] group1->subgroup_6  group1->subgroup_7  group1->subgroup_8 
    ## [19] group1->subgroup_9  group1->subgroup_10 group2->subgroup_11
    ## [22] group2->subgroup_12 group2->subgroup_13 group2->subgroup_14
    ## + ... omitted several edges
    
    # The connection object must refer to the ids of the leaves:
    from  <-  match( connect$from, vertices$name)
    to  <-  match( connect$to, vertices$name)
    
    # Basic graph
    # 绘制基础网络图
    ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
      # 添加边连接线
      geom_conn_bundle(data = get_con(from = from, to = to), 
                       alpha=0.6, colour="skyblue", tension = .7) + 
      # 设置节点
      geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05)) +
      theme_void()
    
    image.png
    # 更改边连接线的颜色
    p <- ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
      geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05)) +
      theme_void()
    
    # Use the 'value' column of the connection data frame for the color:
    p +  geom_conn_bundle(data = get_con(from = from, to = to), 
                          aes(colour=value, alpha=value)) 
    
    image.png
    # In this case you can change the color palette
    p +  geom_conn_bundle(data = get_con(from = from, to = to), 
                       aes(colour=value)) +
      scale_edge_color_continuous(low="white", high="red")
    
    image.png
    p +  geom_conn_bundle(data = get_con(from = from, to = to), 
                       aes(colour=value)) +
      scale_edge_colour_distiller(palette = "BuPu")
    
    image.png
    # Color depends of the index: the from and the to are different
    p +  geom_conn_bundle(data = get_con(from = from, to = to), 
                       width=1, alpha=0.2, aes(colour=..index..)) +
      scale_edge_colour_distiller(palette = "RdPu") +
      theme(legend.position = "none")
    
    image.png
    # 更改节点的颜色,大小等信息
    # Basic usual argument
    p <- ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
      geom_conn_bundle(data = get_con(from = from, to = to), 
                       width=1, alpha=0.2, aes(colour=..index..)) +
      scale_edge_colour_distiller(palette = "RdPu") +
      theme_void() +
      theme(legend.position = "none")
    
    # just a blue uniform color. Note that the x*1.05 allows to make a space between the points and the connection ends
    p + geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05), 
                        colour="skyblue", alpha=0.3, size=3)
    
    image.png
    # It is good to color the points following their group appartenance
    library(RColorBrewer)
    p + geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05, 
                            colour=group),   size=3) +
      scale_colour_manual(values= rep( brewer.pal(9,"Paired"), 30))
    
    image.png
    # And you can adjust the size to whatever variable quite easily!
    p + geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05, 
                          colour=group, size=value, alpha=0.2)) +
      scale_colour_manual(values= rep( brewer.pal(9,"Paired"), 30)) +
      scale_size_continuous( range = c(0.1,10) ) 
    
    image.png

    使用networkD3包绘制动态网络图

    # 安装并加载所需的R包
    #install.packages("networkD3")
    library(networkD3)
    ## Warning: package 'networkD3' was built under R version 3.6.3
    
    # 使用simpleNetwork()函数绘制简单交互网络图
    # 构建示例数据
    Source <- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
    Target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
    NetworkData <- data.frame(Source, Target)
    NetworkData
    ##   Source Target
    ## 1      A      B
    ## 2      A      C
    ## 3      A      D
    ## 4      A      J
    ## 5      B      E
    ## 6      B      F
    ## 7      C      G
    ## 8      C      H
    ## 9      D      I
    
    # Create graph
    simpleNetwork(NetworkData)
    
    image.png
    # 使用forceNetwork()函数绘制交互网络图
    # Load data
    data(MisLinks)
    data(MisNodes)
    # 查看内置数据集
    head(MisLinks)
    ##   source target value
    ## 1      1      0     1
    ## 2      2      0     8
    ## 3      3      0    10
    ## 4      3      2     6
    ## 5      4      0     1
    ## 6      5      0     1
    
    head(MisNodes)
    ##              name group size
    ## 1          Myriel     1   15
    ## 2        Napoleon     1   20
    ## 3 Mlle.Baptistine     1   23
    ## 4    Mme.Magloire     1   30
    ## 5    CountessdeLo     1   11
    ## 6        Geborand     1    9
    
    # Create graph
    forceNetwork(Links = MisLinks, Nodes = MisNodes, 
                 Source = "source", Target = "target", Value = "value", 
                 NodeID = "name", Group = "group", 
                 opacity = 0.4, zoom = TRUE)
    
    image.png
    # Create graph with legend and varying node radius
    forceNetwork(Links = MisLinks, Nodes = MisNodes, 
                 Source = "source", Target = "target", Value = "value", 
                 NodeID = "name", Nodesize = "size", Group = "group",
                 radiusCalculation = "Math.sqrt(d.nodesize)+6",
                 opacity = 0.4, legend = TRUE)
    
    image.png
    # Create graph directed arrows
    forceNetwork(Links = MisLinks, Nodes = MisNodes, 
                 Source = "source", Target = "target", Value = "value", 
                 NodeID = "name", Group = "group", 
                 opacity = 0.4, arrows = TRUE)
    
    image.png
    # 使用chordNetwork()函数绘制和弦交互式网络图
    # 构建示例数据
    hairColourData <- matrix(c(11975,  1951,  8010, 1013,
                               5871, 10048, 16145,  990,
                               8916,  2060,  8090,  940,
                               2868,  6171,  8045, 6907),
                             nrow = 4)
    head(hairColourData)
    ##       [,1]  [,2] [,3] [,4]
    ## [1,] 11975  5871 8916 2868
    ## [2,]  1951 10048 2060 6171
    ## [3,]  8010 16145 8090 8045
    ## [4,]  1013   990  940 6907
    
    # Create graph
    chordNetwork(Data = hairColourData, 
                 width = 500, 
                 height = 500,
                 colourScale = c("#000000", "#FFDD89", "#957244", "#F26223"),
                 labels = c("red", "brown", "blond", "gray"))
    
    image.png
    # 使用dendroNetwork()函数绘制层次聚类交互式网络图
    # 使用hclust()函数进行层次聚类
    hc <- hclust(dist(USArrests), "ave")
    hc
    ## 
    ## Call:
    ## hclust(d = dist(USArrests), method = "ave")
    ## 
    ## Cluster method   : average 
    ## Distance         : euclidean 
    ## Number of objects: 50
    
    # Create graph
    dendroNetwork(hc, height = 800)
    
    image.png
    # 设置标签颜色
    dendroNetwork(hc, textColour = c("red", "green", "orange")[cutree(hc, 3)],
                  height = 800)
    
    image.png
    # 使用radialNetwork()函数绘制放射状交互式网络图
    # 构建示例数据
    CanadaPC <- list(name = "Canada", children = list(list(name = "Newfoundland",
                                                           children = list(list(name = "St. John's"))),
                                                      list(name = "PEI",
                                                           children = list(list(name = "Charlottetown"))),
                                                      list(name = "Nova Scotia",
                                                           children = list(list(name = "Halifax"))),
                                                      list(name = "New Brunswick",
                                                           children = list(list(name = "Fredericton"))),
                                                      list(name = "Quebec",
                                                           children = list(list(name = "Montreal"),
                                                                           list(name = "Quebec City"))),
                                                      list(name = "Ontario",
                                                           children = list(list(name = "Toronto"),
                                                                           list(name = "Ottawa"))),
                                                      list(name = "Manitoba",
                                                           children = list(list(name = "Winnipeg"))),
                                                      list(name = "Saskatchewan",
                                                           children = list(list(name = "Regina"))),
                                                      list(name = "Nunavuet",
                                                           children = list(list(name = "Iqaluit"))),
                                                      list(name = "NWT",
                                                           children = list(list(name = "Yellowknife"))),
                                                      list(name = "Alberta",
                                                           children = list(list(name = "Edmonton"))),
                                                      list(name = "British Columbia",
                                                           children = list(list(name = "Victoria"),
                                                                           list(name = "Vancouver"))),
                                                      list(name = "Yukon",
                                                           children = list(list(name = "Whitehorse")))
    ))
    CanadaPC
    $name
    [1] "Canada"
    
    $children
    $children[[1]]
    $children[[1]]$name
    [1] "Newfoundland"
    
    $children[[1]]$children
    $children[[1]]$children[[1]]
    $children[[1]]$children[[1]]$name
    [1] "St. John's"
    
    $children[[2]]
    $children[[2]]$name
    [1] "PEI"
    
    $children[[2]]$children
    $children[[2]]$children[[1]]
    $children[[2]]$children[[1]]$name
    [1] "Charlottetown"
    
    $children[[3]]
    $children[[3]]$name
    [1] "Nova Scotia"
    
    $children[[3]]$children
    $children[[3]]$children[[1]]
    $children[[3]]$children[[1]]$name
    [1] "Halifax"
    
    $children[[4]]
    $children[[4]]$name
    [1] "New Brunswick"
    
    $children[[4]]$children
    $children[[4]]$children[[1]]
    $children[[4]]$children[[1]]$name
    [1] "Fredericton"
    
    $children[[5]]
    $children[[5]]$name
    [1] "Quebec"
    
    $children[[5]]$children
    $children[[5]]$children[[1]]
    $children[[5]]$children[[1]]$name
    [1] "Montreal"
    
    $children[[5]]$children[[2]]
    $children[[5]]$children[[2]]$name
    [1] "Quebec City"
    
    $children[[6]]
    $children[[6]]$name
    [1] "Ontario"
    
    $children[[6]]$children
    $children[[6]]$children[[1]]
    $children[[6]]$children[[1]]$name
    [1] "Toronto"
    
    $children[[6]]$children[[2]]
    $children[[6]]$children[[2]]$name
    [1] "Ottawa"
    
    $children[[7]]
    $children[[7]]$name
    [1] "Manitoba"
    
    $children[[7]]$children
    $children[[7]]$children[[1]]
    $children[[7]]$children[[1]]$name
    [1] "Winnipeg"
    
    $children[[8]]
    $children[[8]]$name
    [1] "Saskatchewan"
    
    $children[[8]]$children
    $children[[8]]$children[[1]]
    $children[[8]]$children[[1]]$name
    [1] "Regina"
    
    $children[[9]]
    $children[[9]]$name
    [1] "Nunavuet"
    
    $children[[9]]$children
    $children[[9]]$children[[1]]
    $children[[9]]$children[[1]]$name
    [1] "Iqaluit"
    
    $children[[10]]
    $children[[10]]$name
    [1] "NWT"
    
    $children[[10]]$children
    $children[[10]]$children[[1]]
    $children[[10]]$children[[1]]$name
    [1] "Yellowknife"
    
    $children[[11]]
    $children[[11]]$name
    [1] "Alberta"
    
    $children[[11]]$children
    $children[[11]]$children[[1]]
    $children[[11]]$children[[1]]$name
    [1] "Edmonton"
    
    $children[[12]]
    $children[[12]]$name
    [1] "British Columbia"
    
    $children[[12]]$children
    $children[[12]]$children[[1]]
    $children[[12]]$children[[1]]$name
    [1] "Victoria"
    
    $children[[12]]$children[[2]]
    $children[[12]]$children[[2]]$name
    [1] "Vancouver"
    
    $children[[13]]
    $children[[13]]$name
    [1] "Yukon"
    
    $children[[13]]$children
    $children[[13]]$children[[1]]
    $children[[13]]$children[[1]]$name
    [1] "Whitehorse"
    
    # Create graph
    radialNetwork(List = CanadaPC, fontSize = 10)
    
    image.png
    sessionInfo()
    R version 3.6.0 (2019-04-26)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 18363)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=Chinese (Simplified)_China.936 
    [2] LC_CTYPE=Chinese (Simplified)_China.936   
    [3] LC_MONETARY=Chinese (Simplified)_China.936
    [4] LC_NUMERIC=C                              
    [5] LC_TIME=Chinese (Simplified)_China.936    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
     [1] networkD3_0.4      forcats_0.4.0      stringr_1.4.0     
     [4] dplyr_1.0.2        purrr_0.3.2        readr_1.3.1       
     [7] tidyr_1.1.2        tibble_2.1.3       tidyverse_1.2.1   
    [10] ggraph_2.0.3       ggplot2_3.3.2      RColorBrewer_1.1-2
    [13] igraph_1.2.4.1    
    
    loaded via a namespace (and not attached):
     [1] ggrepel_0.8.1      Rcpp_1.0.5         lubridate_1.7.4   
     [4] lattice_0.20-38    assertthat_0.2.1   digest_0.6.20     
     [7] ggforce_0.3.2      R6_2.4.0           cellranger_1.1.0  
    [10] backports_1.1.4    evaluate_0.14      httr_1.4.0        
    [13] pillar_1.4.2       rlang_0.4.7        readxl_1.3.1      
    [16] rstudioapi_0.10    Matrix_1.2-17      rmarkdown_1.13    
    [19] labeling_0.3       htmlwidgets_1.3    polyclip_1.10-0   
    [22] munsell_0.5.0      broom_0.5.2        xfun_0.8          
    [25] compiler_3.6.0     modelr_0.1.4       pkgconfig_2.0.2   
    [28] htmltools_0.3.6    tidyselect_1.1.0   gridExtra_2.3     
    [31] graphlayouts_0.7.0 viridisLite_0.3.0  crayon_1.3.4      
    [34] withr_2.1.2        MASS_7.3-51.4      grid_3.6.0        
    [37] nlme_3.1-139       jsonlite_1.6       gtable_0.3.0      
    [40] lifecycle_0.2.0    magrittr_1.5       scales_1.0.0      
    [43] cli_1.1.0          stringi_1.4.3      farver_1.1.0      
    [46] viridis_0.5.1      xml2_1.2.0         generics_0.0.2    
    [49] vctrs_0.3.2        tools_3.6.0        glue_1.4.2        
    [52] tweenr_1.0.1       hms_0.4.2          rsconnect_0.8.16  
    [55] yaml_2.2.0         colorspace_1.4-1   tidygraph_1.2.0   
    [58] rvest_0.3.4        knitr_1.23         haven_2.3.1
    

    相关文章

      网友评论

        本文标题:R语言可视化(四十):网络图绘制

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