I have a network graph, and I would like to color the edges to match their respective nodes. This is rather straight forward in an igraph
plot, but I'd prefer to do in ggraph
as I like the other aesthetics offered by the package.
There seems to be little control over node color within the ggraph
; while edge color is covered extensively.
My question is this: how can I match my edges to their nodes that have been colored using a custom function, so that each edge that 'leaves' a node is colored the same as its node. This should help people follow flows through the network easier. A more generally question is this: how does ggraph assign colors outside of the aesthetic argument. My question is similar to another question I've asked here before, but the other way around (match edges to nodes), found here.
Here is a reproducible example:
library(tidyverse)
library(igraph)
library(ggraph)
library(tidygraph)
library(RColorBrewer)
## the custom function using Color Brewer
cols_f <- colorRampPalette(RColorBrewer::brewer.pal(11, 'Spectral'))
## make the graph
g <- erdos.renyi.game(50, .1)
# provide some names
V(g)$name <- 1:vcount(g)
#plot using ggraph
g %>%
as_tbl_graph() %>%
activate(nodes) %>%
mutate(degree = centrality_degree()) %>%
ggraph()+
geom_edge_fan(aes(color = as.factor(from),
alpha = ..index..),
show.legend = F)+
geom_node_point(aes(size = degree),
color = cols_f(vcount(g)), # custom function for node color
show.legend = F)+
scale_color_manual(values = cols_f(ecount(g)))+ # custom function for edge color
coord_equal()