Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

igraph: Resolving tight overlapping nodes

Tags:

r

igraph

I have a graph with few hundred nodes and edges. The disconnected subgraphs separate out and resolve well but the nodes within subgraphs overlap and do not resolve well. I have tried several layout algorithms and have also tried changing the relevant parameters within the layout algorithm (ex: iter, kkconst, start.temp etc). But, I am still not able to disperse the tightly clustered nodes. See figure below.

I was hoping to find some parameter to control attraction/repulsion/gravity etc but there seems to be none. The answer and figures from bdemarest in this question does seem to fix exactly this issue. Strangely enough, several seemingly useful parameters have been deprecated in the new version of igraph (coolexp, maxdelta, area, repulserad etc).

Does anyone know of a way to keep the sub graphs well separated while spreading out close nodes well enough that they do not overlap?

graphs

like image 829
rmf Avatar asked Sep 02 '16 11:09

rmf


1 Answers

I managed to get it to work using package qgraph.

enter image description here

Here is a working example:

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(qgraph)

g <- barabasi.game(355, directed=FALSE)

png("plot1.png", height=6, width=12, units="in", res=250)
par(mfrow=c(1, 3))

plot(g,layout=layout_with_fr,vertex.size=4,vertex.label=NA)
mtext("layout_with_fr", side=1)

e <- get.edgelist(g,names=FALSE)
l <- qgraph.layout.fruchtermanreingold(e,vcount=vcount(g))
plot(g,layout=l,vertex.size=4,vertex.label=NA)
mtext("qgraph.layout.fruchtermanreingold default", side=1)

l <- qgraph.layout.fruchtermanreingold(e,vcount=vcount(g),
                                       area=8*(vcount(g)^2),repulse.rad=(vcount(g)^3.1))
plot(g,layout=l,vertex.size=4,vertex.label=NA)
mtext("qgraph.layout.fruchtermanreingold modified", side=1)

dev.off()

Created on 2022-04-25 by the reprex package (v2.0.1)

fig2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.0 (2021-05-18)
#>  os       Ubuntu 20.04.4 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en_GB:en
#>  collate  en_GB.UTF-8
#>  ctype    en_GB.UTF-8
#>  tz       Europe/Stockholm
#>  date     2022-04-25
#>  pandoc   2.14.0.3 @ /usr/lib/rstudio/bin/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version date (UTC) lib source
#>  abind          1.4-5   2016-07-21 [1] CRAN (R 4.1.0)
#>  assertthat     0.2.1   2019-03-21 [1] CRAN (R 4.1.0)
#>  backports      1.4.1   2021-12-13 [1] CRAN (R 4.1.0)
#>  base64enc      0.1-3   2015-07-28 [1] CRAN (R 4.1.0)
#>  checkmate      2.0.0   2020-02-06 [1] CRAN (R 4.1.0)
#>  cli            3.2.0   2022-02-14 [1] CRAN (R 4.1.0)
#>  cluster        2.1.3   2022-03-28 [1] CRAN (R 4.1.0)
#>  colorspace     2.0-3   2022-02-21 [1] CRAN (R 4.1.0)
#>  corpcor        1.6.10  2021-09-16 [1] CRAN (R 4.1.0)
#>  crayon         1.5.1   2022-03-26 [1] CRAN (R 4.1.0)
#>  curl           4.3.2   2021-06-23 [1] CRAN (R 4.1.0)
#>  data.table     1.14.2  2021-09-27 [1] CRAN (R 4.1.0)
#>  DBI            1.1.2   2021-12-20 [1] CRAN (R 4.1.0)
#>  digest         0.6.29  2021-12-01 [1] CRAN (R 4.1.0)
#>  dplyr          1.0.8   2022-02-08 [1] CRAN (R 4.1.0)
#>  ellipsis       0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate       0.15    2022-02-18 [1] CRAN (R 4.1.0)
#>  fansi          1.0.3   2022-03-24 [1] CRAN (R 4.1.0)
#>  fastmap        1.1.0   2021-01-25 [1] CRAN (R 4.1.0)
#>  fdrtool        1.2.17  2021-11-13 [1] CRAN (R 4.1.0)
#>  foreign        0.8-82  2022-01-13 [1] CRAN (R 4.1.0)
#>  Formula        1.2-4   2020-10-16 [1] CRAN (R 4.1.0)
#>  fs             1.5.2   2021-12-08 [1] CRAN (R 4.1.0)
#>  generics       0.1.2   2022-01-31 [1] CRAN (R 4.1.0)
#>  ggplot2        3.3.5   2021-06-25 [1] CRAN (R 4.1.0)
#>  glasso         1.11    2019-10-01 [1] CRAN (R 4.1.0)
#>  glue           1.6.2   2022-02-24 [1] CRAN (R 4.1.0)
#>  gridExtra      2.3     2017-09-09 [1] CRAN (R 4.1.0)
#>  gtable         0.3.0   2019-03-25 [1] CRAN (R 4.1.0)
#>  gtools         3.9.2   2021-06-06 [1] CRAN (R 4.1.0)
#>  highr          0.9     2021-04-16 [1] CRAN (R 4.1.0)
#>  Hmisc          4.6-0   2021-10-07 [1] CRAN (R 4.1.0)
#>  htmlTable      2.4.0   2022-01-04 [1] CRAN (R 4.1.0)
#>  htmltools      0.5.2   2021-08-25 [1] CRAN (R 4.1.0)
#>  htmlwidgets    1.5.4   2021-09-08 [1] CRAN (R 4.1.0)
#>  httr           1.4.2   2020-07-20 [1] CRAN (R 4.1.0)
#>  igraph       * 1.3.0   2022-04-01 [1] CRAN (R 4.1.0)
#>  jpeg           0.1-9   2021-07-24 [1] CRAN (R 4.1.0)
#>  knitr          1.38    2022-03-25 [1] CRAN (R 4.1.0)
#>  lattice        0.20-45 2021-09-22 [1] CRAN (R 4.1.0)
#>  latticeExtra   0.6-29  2019-12-19 [1] CRAN (R 4.1.0)
#>  lavaan         0.6-11  2022-03-31 [1] CRAN (R 4.1.0)
#>  lifecycle      1.0.1   2021-09-24 [1] CRAN (R 4.1.0)
#>  magrittr       2.0.3   2022-03-30 [1] CRAN (R 4.1.0)
#>  Matrix         1.4-1   2022-03-23 [1] CRAN (R 4.1.0)
#>  mime           0.12    2021-09-28 [1] CRAN (R 4.1.0)
#>  mnormt         2.0.2   2020-09-01 [1] CRAN (R 4.1.0)
#>  munsell        0.5.0   2018-06-12 [1] CRAN (R 4.1.0)
#>  nlme           3.1-157 2022-03-25 [1] CRAN (R 4.1.0)
#>  nnet           7.3-17  2022-01-13 [1] CRAN (R 4.1.0)
#>  pbapply        1.5-0   2021-09-16 [1] CRAN (R 4.1.0)
#>  pbivnorm       0.6.0   2015-01-23 [1] CRAN (R 4.1.0)
#>  pillar         1.7.0   2022-02-01 [1] CRAN (R 4.1.0)
#>  pkgconfig      2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
#>  plyr           1.8.7   2022-03-24 [1] CRAN (R 4.1.0)
#>  png            0.1-7   2013-12-03 [1] CRAN (R 4.1.0)
#>  psych          2.2.3   2022-03-19 [1] CRAN (R 4.1.0)
#>  purrr          0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
#>  qgraph       * 1.9.2   2022-03-04 [1] CRAN (R 4.1.0)
#>  R.cache        0.15.0  2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3    1.8.1   2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo           1.24.0  2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils        2.11.0  2021-09-26 [1] CRAN (R 4.1.0)
#>  R6             2.5.1   2021-08-19 [1] CRAN (R 4.1.0)
#>  RColorBrewer   1.1-3   2022-04-03 [1] CRAN (R 4.1.0)
#>  Rcpp           1.0.8.3 2022-03-17 [1] CRAN (R 4.1.0)
#>  reprex         2.0.1   2021-08-05 [1] CRAN (R 4.1.0)
#>  reshape2       1.4.4   2020-04-09 [1] CRAN (R 4.1.0)
#>  rlang          1.0.2   2022-03-04 [1] CRAN (R 4.1.0)
#>  rmarkdown      2.13    2022-03-10 [1] CRAN (R 4.1.0)
#>  rpart          4.1.16  2022-01-24 [1] CRAN (R 4.1.0)
#>  rstudioapi     0.13    2020-11-12 [1] CRAN (R 4.1.0)
#>  scales         1.1.1   2020-05-11 [1] CRAN (R 4.1.0)
#>  sessioninfo    1.2.2   2021-12-06 [1] CRAN (R 4.1.0)
#>  stringi        1.7.6   2021-11-29 [1] CRAN (R 4.1.0)
#>  stringr        1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
#>  styler         1.7.0   2022-03-13 [1] CRAN (R 4.1.0)
#>  survival       3.3-1   2022-03-03 [1] CRAN (R 4.1.0)
#>  tibble         3.1.6   2021-11-07 [1] CRAN (R 4.1.0)
#>  tidyselect     1.1.2   2022-02-21 [1] CRAN (R 4.1.0)
#>  tmvnsim        1.0-2   2016-12-15 [1] CRAN (R 4.1.0)
#>  utf8           1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs          0.4.0   2022-03-30 [1] CRAN (R 4.1.0)
#>  withr          2.5.0   2022-03-03 [1] CRAN (R 4.1.0)
#>  xfun           0.30    2022-03-02 [1] CRAN (R 4.1.0)
#>  xml2           1.3.3   2021-11-30 [1] CRAN (R 4.1.0)
#>  yaml           2.3.5   2022-02-21 [1] CRAN (R 4.1.0)
#> 
#>  [1] /home/user/miniconda3/envs/r-4.1/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
like image 50
rmf Avatar answered Nov 08 '22 03:11

rmf