---
title: "Running the Leiden algorithm with R on multiplex graphs"
author: "S. Thomas Kelly"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Running the Leiden algorithm with R on multiplex graphs}
  %\VignetteEngine{knitr::knitr}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
knitr::opts_chunk$set(fig.cap = "", fig.path = "Plot")
knitr::opts_chunk$set(fig.align = "center")
knitr::opts_chunk$set(dev.args = list(type = "cairo"))
options(width = 120, cli.unicode = FALSE, cli.width = 120)
library("reticulate")
module <- py_available() && reticulate::py_module_available("leidenalg") && reticulate::py_module_available("igraph")
```

## Clustering with the Leiden Algorithm on Multiplex Graphs

The Leiden R package supports calling built-in methods for Multiplex graphs. This vignette assumes you already have the 'leiden' package installed. See the other vignettes for details.

## Set up 

First we import the functions required in the package.

```{r, eval=module}
library("leiden")
```

We also require a example multiplex graph. Here we import a dataset from the 'mplex' package (bear in mind this is a remote package not available on CRAN).

```{r, eval=FALSE}
remotes::install_github("Achab94/mplex")
multiplex_graph <- mplex::aarhus_mplex
multiplex_graph 
```

```{r, echo=FALSE}
library("igraph")
suppressWarnings(suppressMessages({
  #imported from Achab94/mplex::aarhus_mplex
  multiplex_mplex <-  list(nodes = structure(list(nodeID = 1:61, nodeLabel = structure(c(3L,
                                                                       18L, 33L, 4L, 5L, 10L, 11L, 1L, 26L, 27L, 29L, 30L, 32L, 36L,
                                                                       37L, 40L, 43L, 60L, 12L, 23L, 51L, 56L, 58L, 6L, 13L, 15L, 16L,
                                                                       24L, 31L, 38L, 42L, 45L, 54L, 55L, 57L, 61L, 2L, 14L, 22L, 19L,
                                                                       25L, 28L, 34L, 35L, 53L, 7L, 9L, 17L, 41L, 47L, 48L, 52L, 8L,
                                                                       39L, 49L, 50L, 46L, 44L, 59L, 20L, 21L),
                                                                     .Label = c("U1", "U10",
                                                                                "U102", "U106", "U107", "U109", "U110", "U112", "U113", "U118",
                                                                                "U123", "U124", "U126", "U13", "U130", "U134", "U138", "U139",
                                                                                "U14", "U140", "U141", "U142", "U17", "U18", "U19", "U21", "U22",
                                                                                "U23", "U26", "U29", "U3", "U32", "U33", "U37", "U4", "U41",
                                                                                "U42", "U47", "U48", "U49", "U53", "U54", "U59", "U6", "U62",
                                                                                "U63", "U65", "U67", "U68", "U69", "U71", "U72", "U73", "U76",
                                                                                "U79", "U86", "U90", "U91", "U92", "U97", "U99"), class = "factor")),
                           class = "data.frame",
                           row.names = c(NA, -61L)), layerNames = c("lunch", "facebook", "coauthor", "leisure",  "work"),
         L1 = structure(list(ID_Start = c(1L, 1L, 10L, 10L, 10L,
                                          10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 15L,
                                          15L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L,
                                          18L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 21L, 21L, 21L, 22L, 23L,
                                          23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
                                          24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 26L,
                                          26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
                                          28L, 28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L, 29L, 3L,
                                          3L, 3L, 3L, 3L, 30L, 30L, 31L, 31L, 31L, 31L, 32L, 32L, 32L,
                                          33L, 33L, 33L, 34L, 34L, 34L, 35L, 37L, 37L, 38L, 38L, 39L, 4L,
                                          4L, 4L, 4L, 4L, 4L, 4L, 40L, 40L, 40L, 41L, 41L, 42L, 43L, 43L,
                                          43L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 46L, 46L, 46L, 47L, 47L,
                                          47L, 48L, 49L, 49L, 5L, 5L, 5L, 50L, 50L, 51L, 51L, 52L, 53L,
                                          53L, 54L, 54L, 54L, 55L, 55L, 56L, 59L, 6L, 6L, 6L, 6L, 6L, 7L,
                                          7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L,
                                          9L), ID_Arrive = c(2L, 3L, 11L, 14L, 15L, 16L, 14L, 15L, 16L,
                                                             13L, 20L, 40L, 20L, 26L, 40L, 45L, 15L, 16L, 30L, 23L, 25L, 46L,
                                                             49L, 50L, 51L, 52L, 21L, 25L, 43L, 44L, 56L, 58L, 24L, 25L, 30L,
                                                             36L, 40L, 42L, 45L, 44L, 51L, 57L, 26L, 25L, 46L, 49L, 50L, 51L,
                                                             52L, 25L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 31L,
                                                             35L, 43L, 46L, 47L, 48L, 52L, 56L, 58L, 27L, 34L, 44L, 46L, 31L,
                                                             32L, 33L, 34L, 35L, 36L, 38L, 44L, 54L, 59L, 29L, 31L, 32L, 33L,
                                                             34L, 35L, 36L, 31L, 32L, 33L, 34L, 35L, 21L, 44L, 51L, 57L, 7L,
                                                             34L, 36L, 32L, 33L, 34L, 35L, 33L, 34L, 35L, 34L, 35L, 36L, 35L,
                                                             36L, 57L, 58L, 40L, 41L, 39L, 44L, 44L, 10L, 11L, 14L, 15L, 16L,
                                                             18L, 6L, 41L, 42L, 45L, 42L, 45L, 45L, 51L, 56L, 58L, 51L, 53L,
                                                             54L, 55L, 57L, 59L, 61L, 47L, 49L, 52L, 48L, 51L, 52L, 52L, 50L,
                                                             52L, 12L, 13L, 20L, 51L, 52L, 52L, 57L, 56L, 54L, 55L, 55L, 59L,
                                                             61L, 59L, 61L, 58L, 61L, 10L, 11L, 14L, 15L, 16L, 21L, 34L, 44L,
                                                             51L, 57L, 12L, 13L, 37L, 40L, 41L, 42L, 45L, 18L, 25L, 43L, 51L,
                                                             56L, 58L), Weight = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(NA, 193L), class = "data.frame"),
         L2 = structure(list(ID_Start = c(12L, 12L, 13L, 13L, 13L,
                                          13L, 13L, 13L, 15L, 15L, 15L, 17L, 17L, 17L, 17L, 19L, 19L,
                                          19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 21L, 21L,
                                          21L, 21L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L,
                                          24L, 24L, 24L, 24L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
                                          26L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L, 30L, 30L,
                                          30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 33L, 33L, 33L, 34L,
                                          34L, 34L, 37L, 37L, 39L, 39L, 39L, 4L, 4L, 4L, 4L, 4L, 4L,
                                          4L, 44L, 44L, 46L, 46L, 47L, 47L, 47L, 5L, 5L, 5L, 5L, 50L,
                                          51L, 51L, 56L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
                                          8L, 8L, 8L, 8L, 9L, 9L, 9L),
                             ID_Arrive = c(13L, 23L, 21L,
                                           23L, 26L, 44L, 46L, 51L, 16L, 34L, 39L, 23L, 46L, 47L, 51L,
                                           23L, 24L, 26L, 27L, 28L, 29L, 30L, 31L, 33L, 34L, 56L, 58L,
                                           34L, 44L, 46L, 51L, 34L, 37L, 39L, 44L, 46L, 47L, 50L, 51L,
                                           56L, 28L, 30L, 31L, 33L, 34L, 27L, 28L, 29L, 30L, 33L, 34L,
                                           39L, 44L, 51L, 29L, 30L, 31L, 33L, 34L, 30L, 31L, 33L, 34L,
                                           31L, 33L, 34L, 39L, 44L, 33L, 34L, 37L, 39L, 44L, 34L, 44L,
                                           53L, 39L, 46L, 50L, 39L, 44L, 44L, 46L, 51L, 12L, 13L, 17L,
                                           5L, 7L, 8L, 9L, 46L, 51L, 47L, 51L, 50L, 51L, 56L, 12L, 13L,
                                           19L, 21L, 53L, 56L, 58L, 58L, 13L, 15L, 16L, 21L, 23L, 26L,
                                           30L, 39L, 44L, 51L, 12L, 13L, 21L, 34L, 37L, 51L, 56L, 58L
                             ),
                             Weight = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = 194:317, class = "data.frame"),
         L3 = structure(list(ID_Start = c(10L, 12L, 18L, 23L, 23L,
                                          23L, 26L, 26L, 26L, 26L, 26L, 28L, 30L, 38L, 39L, 4L, 46L,
                                          46L, 49L, 6L, 8L),
                             ID_Arrive = c(11L, 13L, 46L, 46L, 49L,
                                           52L, 27L, 28L, 30L, 33L, 36L, 33L, 36L, 54L, 55L, 6L, 48L,
                                           49L, 52L, 14L, 37L),
                             Weight = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = 318:338, class = "data.frame"),
         L4 = structure(list(ID_Start = c(10L, 12L, 12L, 15L, 15L,
                                          15L, 17L, 17L, 17L, 17L, 19L, 19L, 19L, 19L, 20L, 20L, 20L,
                                          20L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L,
                                          24L, 24L, 24L, 25L, 25L, 25L, 25L, 28L, 28L, 28L, 29L, 29L,
                                          30L, 31L, 31L, 31L, 31L, 32L, 33L, 33L, 34L, 34L, 34L, 34L,
                                          35L, 37L, 37L, 37L, 37L, 39L, 39L, 4L, 4L, 40L, 40L, 40L,
                                          41L, 41L, 42L, 44L, 46L, 46L, 46L, 46L, 47L, 47L, 48L, 5L,
                                          5L, 5L, 50L, 55L, 6L, 8L, 8L, 8L, 8L, 8L, 9L),
                             ID_Arrive = c(15L,
                                           13L, 25L, 16L, 34L, 39L, 23L, 46L, 50L, 52L, 23L, 25L, 28L,
                                           36L, 23L, 40L, 42L, 45L, 25L, 31L, 35L, 46L, 47L, 48L, 49L,
                                           50L, 52L, 56L, 25L, 31L, 33L, 35L, 31L, 35L, 46L, 56L, 32L,
                                           33L, 36L, 31L, 35L, 33L, 33L, 34L, 35L, 37L, 33L, 34L, 35L,
                                           35L, 36L, 45L, 50L, 58L, 38L, 39L, 43L, 45L, 43L, 55L, 14L,
                                           6L, 41L, 42L, 45L, 42L, 45L, 45L, 51L, 47L, 48L, 50L, 51L,
                                           48L, 50L, 52L, 13L, 20L, 23L, 52L, 61L, 14L, 11L, 37L, 40L,
                                           42L, 45L, 25L),
                             Weight = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L)), row.names = 339:426, class = "data.frame"),
         L5 = structure(list(ID_Start = c(10L, 10L, 10L, 10L, 11L,
                                          11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 13L, 13L,
                                          13L, 13L, 13L, 13L, 13L, 15L, 17L, 17L, 17L, 18L, 18L, 18L,
                                          19L, 19L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 20L,
                                          20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 23L, 23L,
                                          23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 25L, 25L, 25L, 26L,
                                          26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 27L, 27L, 28L,
                                          29L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 30L, 30L, 31L, 31L,
                                          31L, 31L, 31L, 32L, 33L, 34L, 34L, 34L, 35L, 36L, 37L, 37L,
                                          37L, 37L, 38L, 38L, 38L, 39L, 39L, 4L, 4L, 4L, 4L, 40L, 40L,
                                          40L, 41L, 41L, 42L, 43L, 44L, 44L, 44L, 44L, 44L, 44L, 44L,
                                          46L, 46L, 46L, 46L, 46L, 47L, 47L, 48L, 5L, 5L, 5L, 5L, 5L,
                                          50L, 50L, 51L, 51L, 51L, 51L, 53L, 54L, 55L, 6L, 6L, 6L,
                                          6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
                                          7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
                                          8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L),
                             ID_Arrive = c(11L, 14L,
                                           15L, 16L, 13L, 15L, 16L, 18L, 21L, 26L, 34L, 46L, 60L, 13L,
                                           20L, 18L, 20L, 22L, 26L, 40L, 45L, 46L, 34L, 23L, 46L, 51L,
                                           21L, 44L, 51L, 26L, 44L, 11L, 12L, 18L, 21L, 37L, 40L, 41L,
                                           5L, 51L, 7L, 8L, 40L, 42L, 45L, 40L, 41L, 44L, 46L, 51L,
                                           57L, 58L, 60L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 26L, 31L,
                                           32L, 46L, 51L, 56L, 27L, 28L, 30L, 31L, 32L, 33L, 34L, 36L,
                                           37L, 44L, 36L, 44L, 32L, 35L, 11L, 21L, 44L, 46L, 51L, 57L,
                                           6L, 7L, 9L, 32L, 36L, 34L, 35L, 38L, 41L, 44L, 33L, 44L,
                                           36L, 44L, 46L, 44L, 44L, 40L, 41L, 42L, 45L, 44L, 54L, 59L,
                                           44L, 55L, 11L, 14L, 6L, 7L, 41L, 42L, 45L, 42L, 45L, 45L,
                                           51L, 51L, 53L, 54L, 55L, 57L, 59L, 61L, 47L, 48L, 49L, 51L,
                                           52L, 48L, 51L, 51L, 12L, 13L, 20L, 21L, 22L, 51L, 52L, 52L,
                                           56L, 57L, 58L, 55L, 56L, 61L, 11L, 14L, 18L, 21L, 35L, 51L,
                                           7L, 10L, 11L, 15L, 16L, 18L, 19L, 20L, 21L, 26L, 28L, 29L,
                                           30L, 31L, 32L, 33L, 34L, 35L, 36L, 39L, 44L, 46L, 51L, 57L,
                                           11L, 13L, 19L, 21L, 26L, 34L, 37L, 40L, 41L, 42L, 45L, 51L
                             ),
                             Weight = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                        1L, 1L, 1L)), row.names = 427:620, class = "data.frame"))

  lunch <- graph_from_edgelist(as.matrix(multiplex_mplex[[3]][,1:2]))
  V(lunch)$name <- as.character(multiplex_mplex[[1]][,2])
  facebook <- graph_from_edgelist(as.matrix(multiplex_mplex[[4]][,1:2]))
  facebook <- graph_from_edgelist(
    rbind(as.matrix(multiplex_mplex[[4]][,1:2]),
          cbind(setdiff(as.integer(V(lunch)), as.integer(V(facebook))),
                           setdiff(as.integer(V(lunch)), as.integer(V(facebook))))
    )
  )
  V(facebook)$name <- as.character(multiplex_mplex[[1]][,2])
  coauthor <- graph_from_edgelist(as.matrix(multiplex_mplex[[5]][,1:2]))
  coauthor <- graph_from_edgelist(
    rbind(as_edgelist(coauthor),
          cbind(setdiff(as.integer(V(lunch)), as.integer(V(coauthor))),
                setdiff(as.integer(V(lunch)), as.integer(V(coauthor))))
    )
  )
  leisure <- graph_from_edgelist(as.matrix(multiplex_mplex[[6]][,1:2]))
  V(leisure)$name <- as.character(multiplex_mplex[[1]][,2])
  work <- graph_from_edgelist(as.matrix(multiplex_mplex[[7]][,1:2]))
  V(work)$name <- as.character(multiplex_mplex[[1]][,2])

  multiplex_graph <- list(lunch, facebook, coauthor, leisure, work)
  multiplex_graph <- lapply(multiplex_graph, upgrade_graph)
  names(multiplex_graph) <- c("lunch", "facebook", "coauthor", "leisure", "work")
}))
multiplex_graph
```


## Usage

### Multiplex graph objects

Now we have a multiplex graph structure. This multiplex graph is a list of different igraph object, in the case representing different relationships between the sample people.

```{r}
names(multiplex_graph)
```

Here we import a plotting function to display these 5 groups.

```{r, warning=FALSE, message=FALSE, fig.align='center', out.width="80%",fig.height = 6, fig.width = 6, fig.retina=1.5}
library("graphsim")
library("RColorBrewer")
par(mfrow = c(2, 3))
plot_directed(multiplex_graph$lunch, main = "lunch", col.arrow = brewer.pal(5, "Pastel1")[1], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$work, main = "work", col.arrow = brewer.pal(5, "Pastel1")[2], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$facebook, main = "facebook", col.arrow = brewer.pal(5, "Pastel1")[3], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$leisure, main = "leisure", col.arrow = brewer.pal(5, "Pastel1")[4], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$coauthor, main = "coauthor", col.arrow = brewer.pal(5, "Pastel1")[5], layout = layout.kamada.kawai)
```

This data can also be represented by an adjacency matrix derived from a graph object.

```{r}
library("igraph")
multiplex_matrix <- lapply(multiplex_graph, igraph::as_adjacency_matrix)
multiplex_matrix 
```

### Running the Leiden algorithm in R

Then the Leiden algorithm can be run on the adjacency matrix using a partition type for Multiplex graphs. Here the types are computed automatically.

```{r, eval=module}
partition <- leiden(multiplex_graph,
                       partition_type = "CPMVertexPartition",
                       resolution_parameter = 0.025,
                       seed = 9001)
```

```{r, eval=!module,echo=FALSE, message=FALSE, warning=FALSE, results="hide"}
partition <- c(4, 2, 1, 3, 2, 3, 1, 2, 1, 3, 3, 2, 2, 3, 3, 3, 1, 1, 1, 2, 
               1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 
              2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1)
```

```{r}
table(partition)
```

Here we can see that the partitions are defined across all graphs in the list.


```{r, warning=FALSE, message=FALSE, fig.align='center', out.width="80%",fig.height = 6, fig.width = 6, fig.retina=1.5}
library("graphsim")
library("RColorBrewer")
node.cols <- brewer.pal(max(c(10, partition)),"Pastel1")[partition]
par(mfrow = c(2, 3))
plot_directed(multiplex_graph$lunch, main = "lunch", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[1], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$work, main = "work", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[2], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$facebook, main = "facebook", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[3], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$leisure, main = "leisure", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[4], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$coauthor, main = "coauthor", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[5], layout = layout.kamada.kawai)
```

This can also be run on a list of adjacency matrices giving the same results.

```{r, eval=module}
partition <- leiden(multiplex_matrix,
                       partition_type = "CPMVertexPartition",
                       resolution_parameter = 0.025,
                       seed = 9001)
```

```{r, eval=!module,echo=FALSE, message=FALSE, warning=FALSE, results="hide"}
partition <- c(4, 2, 1, 3, 2, 3, 1, 2, 1, 3, 3, 2, 2, 3, 3, 3, 1, 1, 1, 2, 
               1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 
              2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1)
```

```{r}
table(partition)
```

### Fine-tuning with resolution

The resolution parameter applies on multiplex graphs to fine-tuning how many clusters are detected.

```{r, eval=module}
partition <- leiden(multiplex_graph, partition_type = "CPMVertexPartition", resolution_parameter = 0.1, seed = 42)
```

```{r, eval=!module,echo=FALSE, message=FALSE, warning=FALSE, results="hide"}
partition <- c(8, 2, 6, 4, 2, 4, 6, 2, 7, 4, 4, 2, 2, 4, 4, 4, 3, 6, 1, 2, 
               6, 10, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 5, 5, 2, 
              2, 2, 7, 6, 2, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 3, 6, 7, 5, 9, 5)
```

```{r}
table(partition)
```

```{r, warning=FALSE, message=FALSE, fig.align='center', out.width="80%",fig.height = 6, fig.width = 6, fig.retina=1.5}
library("graphsim")
library("RColorBrewer")
node.cols <- brewer.pal(max(c(10, partition)),"Pastel1")[partition]
par(mfrow = c(2, 3))
plot_directed(multiplex_graph$lunch, main = "lunch", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[1], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$work, main = "work", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[2], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$facebook, main = "facebook", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[3], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$leisure, main = "leisure", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[4], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$coauthor, main = "coauthor", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[5], layout = layout.kamada.kawai)
```

### Fine-tuning with maximum community size

The `resolution`max_comm_size` parameter applies on multiplex graphs to fine-tuning the size of clusters detected.

```{r, eval=module}
partition <- leiden(multiplex_graph, partition_type = "CPMVertexPartition", max_comm_size = 8, resolution_parameter = 0.1, seed = 42)
```

```{r, eval=!module,echo=FALSE, message=FALSE, warning=FALSE, results="hide"}
partition <- c(11, 11, 5, 4, 8, 4, 5, 3, 6, 4, 4, 8, 8, 4, 4, 4, 1, 5, 7, 
               3, 5, 8, 1, 2, 6, 7, 7, 2, 2, 7, 2, 2, 2, 2, 2, 7, 3, 10, 9, 
               3, 3, 3, 6, 5, 3, 1, 1, 13, 1, 1, 1, 1, 9, 10, 9, 6, 5, 6, 10, 
               12, 9)
```

```{r}
table(partition)
```

```{r, warning=FALSE, message=FALSE, fig.align='center', out.width="80%",fig.height = 6, fig.width = 6, fig.retina=1.5}
library("graphsim")
library("RColorBrewer")
node.cols <- brewer.pal(max(c(10, partition)),"Pastel1")[partition]
par(mfrow = c(2, 3))
plot_directed(multiplex_graph$lunch, main = "lunch", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[1], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$work, main = "work", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[2], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$facebook, main = "facebook", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[3], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$leisure, main = "leisure", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[4], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$coauthor, main = "coauthor", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[5], layout = layout.kamada.kawai)
```

### Multiplex cost functions

Any defined cost function is supported for multiplex graphs.
For example, the Modularity Vertex Partition is also supported.

```{r, eval=module}
partition <- leiden(multiplex_graph, partition_type = "ModularityVertexPartition", resolution_parameter = 0.02, seed = 42)
```

```{r, eval=!module,echo=FALSE, message=FALSE, warning=FALSE, results="hide"}
partition <- c(4, 3, 4, 4, 3, 4, 4, 3, 1, 4, 4, 3, 3, 4, 4, 4, 1, 1, 2, 3, 
              3, 3, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 5, 5, 3, 3, 
              3, 1, 5, 3, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 1, 4, 1, 5, 6, 5)
```

```{r}
table(partition)
```

Here we can see partitions in the plotted results are different to as those computed above.

```{r, warning=FALSE, message=FALSE, fig.align='center', out.width="80%",fig.height = 6, fig.width = 6, fig.retina=1.5}
library("graphsim")
library("RColorBrewer")
node.cols <- brewer.pal(max(c(8, partition)),"Pastel1")[partition]
par(mfrow = c(2, 3))
plot_directed(multiplex_graph$lunch, main = "lunch", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[1], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$work, main = "work", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[2], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$facebook, main = "facebook", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[3], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$leisure, main = "leisure", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[4], layout = layout.kamada.kawai)
plot_directed(multiplex_graph$coauthor, main = "coauthor", col.label  = node.cols, col.arrow = brewer.pal(5, "Pastel1")[5], layout = layout.kamada.kawai)
```

