在 igraph 图中获取重复顶点的原因
Reason for getting duplicate vertices in igraph plot
我正在尝试使用 iGraph 从 Excel 中的枢轴 table 数据绘制图表。
当我 运行 我的代码时,我注意到我得到了重复的顶点标签。
我附上了我的代码和示例数据。
请帮忙!
数据:
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中默认称为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 创建
我正在尝试使用 iGraph 从 Excel 中的枢轴 table 数据绘制图表。 当我 运行 我的代码时,我注意到我得到了重复的顶点标签。 我附上了我的代码和示例数据。 请帮忙!
数据: 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中默认称为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 创建