【问题标题】:using tidygraph to merge two edges from the same two nodes into one使用 tidygraph 将来自相同两个节点的两条边合并为一条
【发布时间】:2023-03-08 10:46:01
【问题描述】:

我正在努力弄清楚如何将相同 2 个节点之间的 2 条边折叠为 1 条,然后计算这些边的总和。

我相信igraph有办法做到这一点:

simplify(gcon, edge.attr.comb = list(weight = "sum", function(x)length(x)))

但如果可能的话,我想用tidygraph 来做这件事,因为到目前为止我已经成功地用tidygraph 实现了这一点,而且我更熟悉tidyverse 的工作方式。

我的数据如下所示:

  from to Strength Dataframe Question                Topic
1    0 32        4    weekly        1 Connection Frequency
2    0 19        5    weekly        1 Connection Frequency
3    0  8        3    weekly        1 Connection Frequency
4    0  6        5    weekly        1 Connection Frequency
5    0  2        4    weekly        1 Connection Frequency
6    0 14        5    weekly        1 Connection Frequency

'from' 和 'to' 包含相同的 id(例如 from-to;0-1 和 1-0)。我想浓缩,以便只存在一次 0-1 关系的迭代,并计算出总和 Strength。

到目前为止,这是我的代码:

graph <- data %>%
  filter(Dataframe == "weekly" & Question == 1) %>%
  as_tbl_graph(directed = FALSE) %>%
  activate(edges) %>% # first manipulate edges
  filter(!edge_is_loop()) %>% # remove any loops
  activate(nodes) %>% # now manipulate nodes
  left_join(node.group, by = "name") %>% 
  mutate(
    Popularity = centrality_degree(mode = 'in'),
    Centre = node_is_center(),
    Keyplayer = node_is_keyplayer(k = 5))

是否可以将两条对应的边合并为一条边?我搜索了论坛,但只遇到了相同节点在相同列中重复的引用(即多行中的 0-1)。

【问题讨论】:

  • 请同时包含您预期的输出 data.frame。
  • 似乎有点笨拙,但我会删除每对的第二个实例,切换顺序,将其绑定回来,然后总结。如果您发布一些数据来重新创建它,我会在下面回答。查看此帖子以发布重要问题:stackoverflow.com/questions/5963269/…
  • 据我所知,对于纯粹的tidygraph 解决方案,您不走运。 tidygraph::to_simple() 为您设置 igraph::simplify() 的 edge.attr.comb 参数 (list)。您可以在嵌套属性上使用convert(to_simple),然后使用map(),但这样会给自己带来更多麻烦,而不仅仅是使用igraph 并将结果转换回tbl_g。
  • 感谢大家的帮助。根据建议,我已经放弃了使用 tidygraph,而是通过 igraph 实现了它。

标签: r igraph visnetwork tidygraph


【解决方案1】:
library(tidygraph) # v1.2.0
library(dplyr) # v0.8.5
library(purrr) # v0.3.4

dat <- data.frame(
  from = c("a", "a", "b", "c"),
  to = c("b", "b", "a", "b"),
  n = 1:4
)

在convert() 内调用to_simple() 以折叠平行边。相应的边和权重作为小标题列表存储在.orig_data 中。然后,从.orig_data 中提取折叠边的权重之和。

dat %>% 
  as_tbl_graph() %>% 
  convert(to_simple) %>% 
  activate(edges) %>% 
  mutate(n_sum = map_dbl(.orig_data, ~ sum(.x$n)))

# A tbl_graph: 3 nodes and 3 edges
#
# A directed simple graph with 1 component
#
# Edge Data: 3 x 5 (active)
   from    to .tidygraph_edge_index .orig_data       n_sum
  <int> <int> <list>                <list>           <dbl>
1     1     2 <int [2]>             <tibble [2 x 3]>     3
2     2     1 <int [1]>             <tibble [1 x 3]>     3
3     3     2 <int [1]>             <tibble [1 x 3]>     4
#
# Node Data: 3 x 2
  name  .tidygraph_node_index
  <chr>                 <int>
1 a                         1
2 b                         2
3 c                         3

【讨论】:

    【解决方案2】:

    您可以通过跳转到加权邻接矩阵并返回 igraph 图来折叠图 g 中的多个边,如下所示:

    gg <- graph.adjacency(get.adjacency(g), mode="undirected", weighted=TRUE)
    

    现在gg 将包含与g 中每个顶点对之间出现的边数相对应的边属性$weight。

    我对 tidygraph 不太熟悉,但我制作了这个教学代码来简化你的路径。

    # A graph from sample data
    sample_el <- cbind(c(1,1,1,2,2,2,3,3,3,4,4,5,5,6,6,6,7,7,7,7,8,8),
                       c(2,2,3,6,6,4,4,6,8,5,5,6,8,7,7,2,6,8,3,6,4,4))
    g <- graph_from_edgelist(sample_el, directed=F)
    
    # Always plot graphs with this same layout
    mylaoyt <- layout_(g, as_star())
    plot(g, layout = mylaoyt)
    
    # Merge all duplicate edges using a weighted adjacency matric that
    # is converted back to a graph
    gg <- graph.adjacency(get.adjacency(g), mode="undirected", weighted=TRUE)
    
    # function to return a weighted edgelist from a graph
    get.weighted.edgelist <- function(graph){cbind(get.edgelist(gg), E(gg)$weight)}
    
    # compare your two edge-lists. el has duplicates, wel is weighted
    el <- get.edgelist(g)
    wel<- get.weighted.edgelist(gg)
    el
    wel
    
    # Plot the two graphs to see what el and wel would look like:
    par(mfrow=c(1,2))
    plot(g, layout=mylaoyt, vertex.label=NA, vertex.size=10)
    plot(gg, layout=mylaoyt, vertex.label=NA, vertex.size=10, edge.width=E(gg)$weight * 3)
    

    el 和 wel 中的输出可视化如下:

    希望你能找到你需要的东西。

    【讨论】:

      【解决方案3】:

      我也在努力解决这个问题。 到目前为止,我的解决方案是折叠每个节点的对,然后将权重相加。 像这样的:

      require(dplyr)
      require(tidyr)
      
      pasteCols = function(x, y, sep = ":"){
        stopifnot(length(x) == length(y))
        return(lapply(1:length(x), function(i){paste0(sort(c(x[i], y[i])), collapse = ":")}) %>% unlist())
      }
      data = data %>% 
        mutate(col_pairs = pasteCols(from, to, sep = ":")) %>% 
        group_by(col_pairs) %>% summarise(sum_weight = sum(weight)) %>% 
        tidyr::separate(col = col_pairs, c("from", "to"), sep = ":")
      

      【讨论】:

      • 非常适合我,竖起大拇指!
      【解决方案4】:

      tidygraph 可以在 morphed 状态下通过 simplify_to 调用简化图形,但在 unmorphing 时会恢复为原始状态。

      这是一个整洁的解决方法:

      data <- read.table(header=TRUE, text="
      from to weight
      0 14 5 
      0  1 1 
      1  0 1
      ")
      
      original <- as_tbl_graph(data)
      

      输入:

      > original
      # A tbl_graph: 3 nodes and 3 edges
      #
      # A directed simple graph with 1 component
      #
      # Node Data: 3 x 1 (active)
        name 
        <chr>
      1 0    
      2 1    
      3 14   
      #
      # Edge Data: 3 x 3
         from    to weight
        <int> <int>    <int>
      1     1     3        5
      2     1     2        1
      3     2     1        1
      

      解决方案:

      modified <- original %>% activate(edges) %>% 
          # create a temporary grouping & filtering variable by sorting from/to IDs
          mutate(temp = ifelse(from > to, paste0(to, from), paste0(from, to))) %>% 
          group_by(temp) %>% 
          mutate(weight = sum(weight)) %>% 
          ungroup() %>% 
          dplyr::distinct(temp, .keep_all = TRUE) %>% 
          select(-temp)
      

      输出:

      > modified   
      # A tbl_graph: 3 nodes and 2 edges
      #
      # A rooted tree
      #
      # Edge Data: 2 x 3 (active)
         from    to weight
        <int> <int>    <int>
      1     1     3        5
      2     1     2        2
      #
      # Node Data: 3 x 1
        name 
        <chr>
      1 0    
      2 1    
      3 14   
      

      【讨论】:

        【解决方案5】:

        这是一种方法。它使用tidygraph,在底层使用igraph。

        library(tidygraph)
        #> 
        #> Attaching package: 'tidygraph'
        #> The following object is masked from 'package:stats':
        #> 
        #>     filter
        library(igraph)
        #> 
        #> Attaching package: 'igraph'
        #> The following object is masked from 'package:tidygraph':
        #> 
        #>     groups
        #> The following objects are masked from 'package:stats':
        #> 
        #>     decompose, spectrum
        #> The following object is masked from 'package:base':
        #> 
        #>     union
        library(ggraph)
        #> Loading required package: ggplot2
        library(tidyverse)
        
        
        g <- tibble(from = sample(letters[1:5], 25, T), 
               to = sample(letters[1:5], 25, T)) %>% 
          as_tbl_graph()
        
        
        
        ggraph(g)+
          geom_edge_parallel(arrow = arrow(type = 'closed'), 
                             start_cap = circle(7.5, 'mm'), 
                             end_cap = circle(7.5, 'mm'))+
          geom_node_label(aes(label = name))+
          labs(title = 'Multiple edges shown between node pairs')
        #> Using `stress` as default layout
        

        # Add the weigths as counts in the original dataframe
        
        g_weights <- g %>% 
          activate(edges) %>% 
          as_tibble() %>% 
          mutate(link = glue::glue('{from}_{to}')) %>% 
          add_count(link) %>% 
          distinct(link, n, .keep_all = T) %>% 
          select(from, to, n) %>% 
          as_tbl_graph()
        
        ggraph(g_weights)+
          geom_edge_parallel(arrow = arrow(type = 'closed'), 
                   start_cap = circle(7.5, 'mm'), 
                   end_cap = circle(7.5, 'mm'), 
                   aes(width = n))+
          geom_node_label(aes(label = name))+
          labs(title = 'Single edges shown between node pairs', 
               subtitle = 'Weights used as edge width')+
          scale_edge_width(range = c(.5, 2), name = 'Weight')
        #> Using `stress` as default layout
        

        由reprex package (v0.3.0) 于 2019 年 9 月 3 日创建

        【讨论】:

          猜你喜欢
          • 2013-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多