【问题标题】:Reason for getting duplicate vertices in igraph plot在 igraph 图中获取重复顶点的原因
【发布时间】:2021-11-25 11:10:25
【问题描述】:

我正在尝试使用 iGraph 从 Excel 中的数据透视表数据中进行绘图。 当我运行我的代码时,我注意到我得到了重复的顶点标签。 我附上了我的代码和示例数据。 请帮忙!

数据: https://drive.google.com/file/d/14Mfh41LJ7dN6QJR9wDlzG16XB93LB5Ne/view?usp=sharing

#read matrix csv file without heading
#call igraph library
#read as incidence matrix

library(igraph)
n <- graph_from_incidence_matrix(alzheimer)
pal <- rainbow(5, alpha=.5)

plot(n,
vertex.label = Data$X1,
vertex.size = 3,
label.dist = 100,
vertex.label.font = 3,
vertex.label.cex = 0.58,
vertex.color = pal,
edge.color = "gray",
vertex.label.dist = 1,
vertex.size = 10, asp = 9/16,
layout = layout.fruchterman.reingold
)

【问题讨论】:

    标签: r igraph vertex


    【解决方案1】:

    您的矩阵不是对称的,看起来像一个邻接矩阵。我用它们做了一个边缘列表。第一列没有名称。在 R 中默认为 X1。

    library(tidyverse)
    library(igraph)
    #> 
    #> Attaching package: 'igraph'
    #> The following objects are masked from 'package:dplyr':
    #> 
    #>     as_data_frame, groups, union
    #> The following objects are masked from 'package:purrr':
    #> 
    #>     compose, simplify
    #> The following object is masked from 'package:tidyr':
    #> 
    #>     crossing
    #> The following object is masked from 'package:tibble':
    #> 
    #>     as_data_frame
    #> The following objects are masked from 'package:stats':
    #> 
    #>     decompose, spectrum
    #> The following object is masked from 'package:base':
    #> 
    #>     union
    
    alzheimer <- read_csv("~/Downloads/Data.csv")
    #> Warning: Missing column names filled in: 'X1' [1]
    #> 
    #> ── Column specification ────────────────────────────────────────────────────────
    #> cols(
    #>   X1 = col_character(),
    #>   a = col_logical(),
    #>   b = col_double(),
    #>   c = col_logical(),
    #>   d = col_logical(),
    #>   e = col_logical(),
    #>   f = col_logical(),
    #>   g = col_logical(),
    #>   h = col_double(),
    #>   i = col_logical(),
    #>   j = col_logical(),
    #>   k = col_double(),
    #>   l = col_double()
    #> )
    
    n <-
      alzheimer %>%
      rename(from = X1) %>%
      pivot_longer(-from, names_to = "to") %>%
      filter(! is.na(value)) %>%
      select(from, to) %>%
      as.matrix() %>%
      graph_from_edgelist()
    
    pal <- rainbow(5, alpha=.5)
    
    plot(n,
         vertex.label = alzheimer$X1,
         vertex.size = 3,
         label.dist = 100,
         vertex.label.font = 3,
         vertex.label.cex = 0.58,
         vertex.color = pal,
         edge.color = "gray",
         vertex.label.dist = 1,
         vertex.size = 10, asp = 9/16,
         layout = layout.fruchterman.reingold
    )
    

    由reprex package (v2.0.1) 于 2021-10-05 创建

    【讨论】:

    • 谢谢!我从数据中制作了一个边列表并进行了一些更改,并根据需要得到了图表。
    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 2013-01-21
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多