【问题标题】:selectively color/size nodes in ggraphggraph 中的选择性颜色/大小节点
【发布时间】:2019-05-05 19:02:26
【问题描述】:

我正在使用 tidygraph 和 ggraph 来绘制网络。有没有办法选择性地操纵节点?具体来说,尺寸和颜色,分别。

# example data
    rstat_nodes <- data.frame(name = c("Hadley", "David", "Romain", "Julia"))
    rstat_edges <- data.frame(from = c(1, 1, 1, 2, 3, 3, 4, 4, 4), 
                              to = c(2, 3, 4, 1, 1, 2, 1, 2, 3))
    gr <- tbl_graph(nodes = rstat_nodes, edges = rstat_edges)

    as_tbl_graph(gr) %>% 
      mutate(centrality = centrality_degree(normalized = T)) %>% 
      ggraph(layout = 'auto') + 
      #geom_edge_link() +
      geom_edge_arc(curvature=0.2,alpha=0.5) + 
      geom_node_point(aes(size = 0.2, colour = centrality)) + 
      scale_color_viridis(guide = 'legend') + 
      ggtitle("Network Degree Centrality (Normalized)") +
      theme_graph()

【问题讨论】:

    标签: r igraph ggraph tidygraph


    【解决方案1】:

    是的,您可以使用 tidygraph 包中的 activate 访问 nodesedges 数据帧。然后您可以使用dplyr 来操作每个文件中的数据。您也可以直接通过管道输入ggraph

    library(tidyverse)
    library(igraph)
    library(ggraph)
    library(tidygraph)
    library(graphlayouts)
    library(scales)
    
    
    # example data
    rstat_nodes <-
      data.frame(name = c("Hadley", "David", "Romain", "Julia"))
    rstat_edges <- data.frame(from = c(1, 1, 1, 2, 3, 3, 4, 4, 4),
                              to = c(2, 3, 4, 1, 1, 2, 1, 2, 3))
    
    gr <- tbl_graph(nodes = rstat_nodes, edges = rstat_edges)
    
    gr %>% 
      activate(nodes) %>% # use dplyr on nodes
      mutate(David = 
               case_when(name == 'David' ~ 2, T ~ 0), 
             David = as.character(David)) %>% 
      activate(edges) %>% # same on edge list
      mutate(David = case_when(from == 2 ~ 1, T ~ 0), 
             David = as.character(David)) %>% 
      ggraph(., layout = 'auto')+
      geom_edge_link(aes(color = David), 
                     width = 1)+
      geom_node_point(aes(color = David), 
                      size = 5)+
      geom_node_text(aes(label = name), 
                     nudge_x = .05, 
                     nudge_y = .05)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多