luukvdmeer / sfnetworks

Tidy Geospatial Networks in R
https://luukvdmeer.github.io/sfnetworks/
Other
338 stars 20 forks source link

group_walktrap, one goup for each node #163

Closed LouisManiere closed 3 years ago

LouisManiere commented 3 years ago

Describe the bug The group_walktrap function create one group for each node.

I am not used to bug report I hope this issue is well done. And thank you for your work on for this really helpful package!

Reproducible example

# packages
library(sfnetworks)
library(sf)
library(tidygraph)
library(tidyverse)
library(igraph)

packageVersion("sfnetworks")

# ‘0.5.2’

# sfnetworks creation

(net <- roxel %>%
    as_sfnetwork(directed = FALSE))
net

# grouping with walktrap and Louvain functions

net <- net %>%
  activate ("nodes") %>%
  mutate(walk_c = group_walktrap(weights = NULL, steps = 5)) %>%
  mutate(louv_c = group_louvain(weights = NULL))

# nodes extraction in sf
nodes_sf <- net %>%
  activate("nodes") %>%
  st_as_sf()

nodes_sf 

Expected behavior We expect a real clustering with the walktrap method, In the Louvain clustering I have with this example 32 groups. I think it should be around this number of clusters. With my own data I tried the walktrap clustering with the cluster_walktrap from igraph packages which works well and I when I switch to sfnetworks I have done the same clustering with group_walktrap and I have this unexpect result. I tried others clustering methods with both igraph and sfnetworks (group_louvain, group_edge_betweenness, group_fast_greedy) and the results are the same. have just this problem with group_walktrap.

R Session Info R version 4.1.0 (2021-05-18) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale: [1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 LC_NUMERIC=C LC_TIME=French_France.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] shp2graph_0-5 rgdal_1.5-23 sp_1.4-5 igraph_1.2.6 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4 readr_1.4.0
[10] tidyr_1.1.3 tibble_3.1.2 ggplot2_3.3.5 tidyverse_1.3.1 tidygraph_1.2.0 sf_1.0-1 sfnetworks_0.5.2

loaded via a namespace (and not attached): [1] Rcpp_1.0.7 lubridate_1.7.10 lattice_0.20-44 class_7.3-19 assertthat_0.2.1 utf8_1.2.1 R6_2.5.0 cellranger_1.1.0
[9] backports_1.2.1 reprex_2.0.0 e1071_1.7-7 httr_1.4.2 pillar_1.6.1 rlang_0.4.11 readxl_1.3.1 rstudioapi_0.13
[17] Matrix_1.3-3 foreign_0.8-81 munsell_0.5.0 proxy_0.4-26 broom_0.7.8 compiler_4.1.0 modelr_0.1.8 pkgconfig_2.0.3
[25] tidyselect_1.1.1 fansi_0.5.0 crayon_1.4.1 dbplyr_2.1.1 withr_2.4.2 wk_0.5.0 grid_4.1.0 jsonlite_1.7.2
[33] lwgeom_0.2-6 gtable_0.3.0 lifecycle_1.0.0 DBI_1.1.1 magrittr_2.0.1 units_0.7-2 scales_1.1.1 KernSmooth_2.23-20 [41] cli_3.0.1 stringi_1.6.2 farver_2.1.0 fs_1.5.0 xml2_1.3.2 ellipsis_0.3.2 sfheaders_0.4.0 generics_0.1.0
[49] vctrs_0.3.8 s2_1.0.6 tools_4.1.0 glue_1.4.2 hms_1.1.0 colorspace_2.0-2 maptools_1.1-1 classInt_0.4-3
[57] rvest_1.0.0 haven_2.4.1

Robinlovelace commented 3 years ago

Thanks for the issue @LouisManiere. Small tip that may help: if you edit the post above and add this before the code, it will show as R code:

```r

You can end a code chunk like this:

```

No comment on the issue you describe, just thought that would be useful in terms of formatting.

LouisManiere commented 3 years ago

Dear @Robinlovelace Is that fine now?

Robinlovelace commented 3 years ago

Yes that looks much better :+1:

loreabad6 commented 3 years ago

Hi @LouisManiere, thanks for the issue and the example. This is indeed an open issue on tidygraph see here.

A workaround until this issue is solved would be:

net <- net %>%
  activate ("nodes") %>%
  mutate(walk_c = igraph::membership(igraph::cluster_walktrap(., weights = NULL, steps = 5))) %>%
  mutate(louv_c = group_louvain(weights = NULL))
LouisManiere commented 3 years ago

Hello @loreabad6 Thank you for this clarification and the provisional solution. So I will close this issue, the tidygraph correction will solve this one. Best,