thomasp85 / ggraph

Grammar of Graph Graphics
https://ggraph.data-imaginist.com
Other
1.08k stars 116 forks source link

`sort.by` option collapses all nodes into one when using linear layout with circular #347

Closed cpauvert closed 1 year ago

cpauvert commented 1 year ago

Hi!

First, thanks a lot for developing and maintaining this package, I've used it a lot and this is great for visualisation!

Unfortunately, one feature (sort.by) that I liked is not working anymore. It seems now that all the nodes are put in the same position (see reprex below). I observed this using any node attributes in the graph.

library(ggraph)
#> Loading required package: ggplot2
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter

# Use Karate network
karate <- tidygraph::create_notable("Zachary") %>% 
  activate(nodes) %>% 
  mutate(
    name = 1:n(),
    # Odd nodes are labelled A and even nodes B
    class = rep_len(c("A","B"), n())
  )

# Unsorted layout alternate A and B nodes
ggraph(karate, layout = "linear", circular = TRUE)+
  geom_edge_arc()+
  geom_node_label(aes(label = name, fill = class))+
  coord_fixed()+
  theme_void()


# If we want to group A nodes and B nodes, use sort.by
# but the entire layout is destroyed.
ggraph(karate, layout = "linear", circular = TRUE, sort.by = "class")+
  geom_edge_arc()+
  geom_node_label(aes(label = name, fill = class))+
  coord_fixed()+
  theme_void()

It seems all the nodes get the same position when using the sort.by option.


create_layout(karate, layout = "linear",
              circular = TRUE) %>% head
#>            x         y name class .ggraph.orig_index circular .ggraph.index
#> 1 0.09226836 0.9957342    1     A                  1     TRUE             1
#> 2 0.27366299 0.9618256    2     B                  2     TRUE             2
#> 3 0.44573836 0.8951633    3     A                  3     TRUE             3
#> 4 0.60263464 0.7980172    4     B                  4     TRUE             4
#> 5 0.73900892 0.6736956    5     A                  5     TRUE             5
#> 6 0.85021714 0.5264322    6     B                  6     TRUE             6

create_layout(karate, layout = "linear",
              circular = TRUE, sort.by = "class") %>% head
#>              x  y name class .ggraph.orig_index circular .ggraph.index
#> 1 6.123234e-17 -1    1     A                  1     TRUE             1
#> 2 6.123234e-17 -1    2     B                  2     TRUE             2
#> 3 6.123234e-17 -1    3     A                  3     TRUE             3
#> 4 6.123234e-17 -1    4     B                  4     TRUE             4
#> 5 6.123234e-17 -1    5     A                  5     TRUE             5
#> 6 6.123234e-17 -1    6     B                  6     TRUE             6

Created on 2023-08-05 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.2 (2022-10-31) #> os Ubuntu 22.04.1 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Etc/UTC #> date 2023-08-05 #> pandoc 2.19.2 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] RSPM (R 4.2.0) #> colorspace 2.1-0 2023-01-23 [1] RSPM (R 4.2.0) #> crayon 1.5.2 2022-09-29 [1] RSPM (R 4.2.0) #> curl 4.3.3 2022-10-06 [1] RSPM (R 4.2.0) #> digest 0.6.33 2023-07-07 [1] RSPM (R 4.2.0) #> dplyr 1.1.2 2023-04-20 [1] RSPM (R 4.2.0) #> evaluate 0.21 2023-05-05 [1] RSPM (R 4.2.0) #> fansi 1.0.4 2023-01-22 [1] RSPM (R 4.2.0) #> farver 2.1.1 2022-07-06 [1] RSPM (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] RSPM (R 4.2.0) #> fs 1.6.3 2023-07-20 [1] RSPM (R 4.2.0) #> generics 0.1.3 2022-07-05 [1] RSPM (R 4.2.0) #> ggforce 0.4.1 2022-10-04 [1] RSPM (R 4.2.0) #> ggplot2 * 3.4.2 2023-04-03 [1] RSPM (R 4.2.0) #> ggraph * 2.1.0 2022-10-09 [1] RSPM (R 4.2.0) #> ggrepel 0.9.3 2023-02-03 [1] RSPM (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] RSPM (R 4.2.0) #> graphlayouts 1.0.0 2023-05-01 [1] RSPM (R 4.2.0) #> gridExtra 2.3 2017-09-09 [1] RSPM (R 4.2.0) #> gtable 0.3.3 2023-03-21 [1] RSPM (R 4.2.0) #> highr 0.9 2021-04-16 [1] RSPM (R 4.2.0) #> htmltools 0.5.4 2022-12-07 [1] RSPM (R 4.2.0) #> httr 1.4.4 2022-08-17 [1] RSPM (R 4.2.0) #> igraph 1.5.0.1 2023-07-23 [1] RSPM (R 4.2.0) #> knitr 1.41 2022-11-18 [1] RSPM (R 4.2.0) #> labeling 0.4.2 2020-10-20 [1] RSPM (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] RSPM (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] RSPM (R 4.2.0) #> MASS 7.3-58.1 2022-08-03 [2] CRAN (R 4.2.2) #> mime 0.12 2021-09-28 [1] RSPM (R 4.2.0) #> munsell 0.5.0 2018-06-12 [1] RSPM (R 4.2.0) #> pillar 1.9.0 2023-03-22 [1] RSPM (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.2.0) #> polyclip 1.10-4 2022-10-20 [1] RSPM (R 4.2.0) #> purrr 1.0.1 2023-01-10 [1] RSPM (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] RSPM (R 4.2.0) #> Rcpp 1.0.11 2023-07-06 [1] RSPM (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] RSPM (R 4.2.0) #> rlang 1.1.1 2023-04-28 [1] RSPM (R 4.2.0) #> rmarkdown 2.19 2022-12-15 [1] RSPM (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] RSPM (R 4.2.0) #> scales 1.2.1 2022-08-20 [1] RSPM (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] RSPM (R 4.2.0) #> stringi 1.7.12 2023-01-11 [1] RSPM (R 4.2.0) #> stringr 1.5.0 2022-12-02 [1] RSPM (R 4.2.0) #> tibble 3.2.1 2023-03-20 [1] RSPM (R 4.2.0) #> tidygraph * 1.2.3 2023-02-01 [1] RSPM (R 4.2.0) #> tidyr 1.3.0 2023-01-24 [1] RSPM (R 4.2.0) #> tidyselect 1.2.0 2022-10-10 [1] RSPM (R 4.2.0) #> tweenr 2.0.2 2022-09-06 [1] RSPM (R 4.2.0) #> utf8 1.2.3 2023-01-31 [1] RSPM (R 4.2.0) #> vctrs 0.6.3 2023-06-14 [1] RSPM (R 4.2.0) #> viridis 0.6.4 2023-07-22 [1] RSPM (R 4.2.0) #> viridisLite 0.4.2 2023-05-02 [1] RSPM (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] RSPM (R 4.2.0) #> xfun 0.35 2022-11-16 [1] RSPM (R 4.2.0) #> xml2 1.3.3 2021-11-30 [1] RSPM (R 4.2.0) #> yaml 2.3.6 2022-10-18 [1] RSPM (R 4.2.0) #> #> [1] /usr/local/lib/R/site-library #> [2] /usr/local/lib/R/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

Let me know how and where I can help you help me! Best, Charlie

cpauvert commented 1 year ago

Oops ok, I solved it. If we want to group A nodes and B nodes, use sort.by but WITHOUT QUOTES!

ggraph(karate, layout = "linear", circular = TRUE, sort.by = class)+
  geom_edge_arc()+
  geom_node_label(aes(label = name, fill = class))+
  coord_fixed()+
  theme_void()