thomasp85 / ggraph

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

Edge colours interpreted literally when ggraph is not loaded #281

Closed lazappi closed 10 months ago

lazappi commented 3 years ago

Hi

Possibly this is known behaviour but just thought I would report it in case it wasn't. When you try to colour edges using a numeric variable without loading ggraph the values are interpreted as literal colours rather than continuous numeric values.

Toy example graph:

library(tidygraph)
#> Warning: package 'tidygraph' was built under R version 4.0.2
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter

nodes <- data.frame(
    node = c(1, 2, 3, 4)
)

edges <- data.frame(
    from   = c(1, 2, 2),
    to     = c(2, 3, 4),
    weight = c(5, 3, 2)
)

graph <- tbl_graph(nodes = nodes, edges = edges)

If we plot this without loading ggraph we get three distinct colours for the edges:

ggraph::ggraph(graph, layout = "fr") +
    ggraph::geom_edge_link(ggplot2::aes(colour = weight)) +
    ggraph::geom_node_point()

I think they are being interpreted as literal colours rather than a factor because if you change the weights the colours change and if you give a negative weight you get his error:

ggraph::ggraph(graph, layout = "fr") +
    ggraph::geom_edge_link(ggplot2::aes(colour = -weight)) +
    ggraph::geom_node_point()
#> Error: colours encodes as numbers must be positive

If we load ggraph we get a continuous colour scale as expected:

library(ggraph)
#> Loading required package: ggplot2

ggraph(graph, layout = "fr") +
    geom_edge_link(aes(colour = weight)) +
    geom_node_point()

Created on 2021-02-01 by the reprex package (v0.3.0)

Session info ``` r sessionInfo() #> R version 4.0.0 (2020-04-24) #> Platform: x86_64-apple-darwin17.0 (64-bit) #> Running under: macOS Catalina 10.15.7 #> #> Matrix products: default #> BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib #> LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib #> #> locale: #> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 #> #> attached base packages: #> [1] stats graphics grDevices utils datasets methods base #> #> other attached packages: #> [1] ggraph_2.0.4 ggplot2_3.3.3 tidygraph_1.2.0 #> #> loaded via a namespace (and not attached): #> [1] Rcpp_1.0.5 pillar_1.4.7 compiler_4.0.0 highr_0.8 #> [5] viridis_0.5.1 tools_4.0.0 digest_0.6.27 viridisLite_0.3.0 #> [9] evaluate_0.14 lifecycle_0.2.0 tibble_3.0.4 gtable_0.3.0 #> [13] pkgconfig_2.0.3 rlang_0.4.10 igraph_1.2.6 curl_4.3 #> [17] ggrepel_0.9.0 yaml_2.2.1 xfun_0.20 gridExtra_2.3 #> [21] xml2_1.3.2 httr_1.4.2 withr_2.3.0 dplyr_1.0.2 #> [25] stringr_1.4.0 knitr_1.30 graphlayouts_0.7.1 generics_0.1.0 #> [29] vctrs_0.3.6 grid_4.0.0 tidyselect_1.1.0 glue_1.4.2 #> [33] R6_2.5.0 rmarkdown_2.6 polyclip_1.10-0 farver_2.0.3 #> [37] purrr_0.3.4 tidyr_1.1.2 tweenr_1.0.1 magrittr_2.0.1 #> [41] scales_1.1.1 ellipsis_0.3.1 htmltools_0.5.0 MASS_7.3-53 #> [45] mime_0.9 ggforce_0.3.2 colorspace_2.0-0 labeling_0.4.2 #> [49] stringi_1.5.3 munsell_0.5.0 crayon_1.3.4 ```
thomasp85 commented 10 months ago

This is a known issue that is caused by how ggplot2 figures out scales. Unfortunately there is nothing we can do from ggraphs side on this