I am trying to make a plot using iGraph from a pivot table data in Excel. When I run my code, I notice I am getting duplicated vertex labels. I am attaching my code and sample data. Please help!
Data: 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
)
Your matrix was not symmetric and looks like an adjacency matrix. I made an edgelist out of them. The first column did not have a name. It will be called X1 by default in R.
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
)
Created on 2021-10-05 by the reprex package (v2.0.1)