Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem on edge colour geom_edge_link colour and node colour geom_node_point #372

Open
DanielIAvila opened this issue May 30, 2024 · 1 comment

Comments

@DanielIAvila
Copy link

I am experiencing this exact same issue. In my graph I have specified a color under E(g)$color that I want to use, and it is not working. It is taking the default colors used in ggplot2.

I have tried both the combination and stand-alone version of what is described here and it is not working. I have tried it for the nodes, and it is not working either. The graph g has attributes V(g)$year, V(g)$type, V(g)$size, V(g)$color, E(g)$color .

I need to color the nodes and the edges according to the specified attributes.

ggraph(g, layout = layout_matrix) +
  geom_edge_link(aes(edge_colour=factor(E(g)$colour), colour=factor(E(g)$colour)), show.legend = FALSE) +
  geom_node_point(aes(size = size, colour = color), show.legend = FALSE)

Below my R session:

sessionInfo()

R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

other attached packages:
 [1] lubridate_1.9.3    forcats_1.0.0      stringr_1.5.1      purrr_1.0.2       
 [5] readr_2.1.5        tidyr_1.3.1        tibble_3.2.1       tidyverse_2.0.0   
 [9] RColorBrewer_1.1-3 ggraph_2.2.1       ggplot2_3.5.0      igraph_2.0.3      
[13] jsonlite_1.8.8     readxl_1.4.3       dplyr_1.1.4        openalexR_1.3.1   

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.1   viridisLite_0.4.2  farver_2.1.1       viridis_0.6.5     
 [5] fastmap_1.1.1      tweenr_2.0.3       timechange_0.3.0   lifecycle_1.0.4   
 [9] sf_1.0-16          magrittr_2.0.3     compiler_4.3.3     rlang_1.1.3       
[13] progress_1.2.3     tools_4.3.3        utf8_1.2.4         ggsignif_0.6.4    
[17] prettyunits_1.2.0  labeling_0.4.3     graphlayouts_1.1.1 bit_4.0.5         
[21] classInt_0.4-10    curl_5.2.1         abind_1.4-5        KernSmooth_2.23-22
[25] withr_3.0.0        grid_4.3.3         polyclip_1.10-6    fansi_1.0.6       
[29] ggpubr_0.6.0       e1071_1.7-14       colorspace_2.1-0   scales_1.3.0      
[33] MASS_7.3-60.0.1    cli_3.6.2          crayon_1.5.2       generics_0.1.3    
[37] rstudioapi_0.15.0  httr_1.4.7         tzdb_0.4.0         DBI_1.2.2         
[41] cachem_1.0.8       ggforce_0.4.2      proxy_0.4-27       parallel_4.3.3    
[45] cellranger_1.1.0   vctrs_0.6.5        carData_3.0-5      car_3.1-2         
[49] hms_1.1.3          bit64_4.0.5        rstatix_0.7.2      ggrepel_0.9.5     
[53] units_0.8-5        glue_1.7.0         stringi_1.8.3      gtable_0.3.4      
[57] munsell_0.5.0      pillar_1.9.0       R6_2.5.1           tidygraph_1.3.1   
[61] vroom_1.6.5        backports_1.4.1    memoise_2.0.1      broom_1.0.5       
[65] class_7.3-22       Rcpp_1.0.12        gridExtra_2.3      pkgconfig_2.0.3
@schochastics
Copy link
Contributor

No need to turn the colours to a factor. You can use the I() function to use the colour that is explicitely saved as an attribute

library(ggraph)
library(igraph)
g <- make_graph("zachary")
E(g)$colour <- "red"
E(g)$colour[incident(g, 1)] <- "blue"
layout_matrix <- graphlayouts::layout_with_stress(g)
V(g)$size <- degree(g)
V(g)$color <- as.character(membership(cluster_louvain(g)))

#not working
ggraph(g, layout = layout_matrix) +
    geom_edge_link(aes(edge_colour = factor(E(g)$colour), colour = factor(E(g)$colour)), show.legend = FALSE) +
    geom_node_point(aes(size = size, colour = color), show.legend = FALSE)

#working
ggraph(g, layout = layout_matrix) +
    geom_edge_link(aes(edge_colour = I(E(g)$colour), colour = I(E(g)$colour)), show.legend = FALSE) +
    geom_node_point(aes(size = size, colour = color), show.legend = FALSE)

Created on 2024-05-30 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants