我有一个网络图,我想为边缘着色以匹配它们各自的节点。这在情节中相当直截了当igraph
,但我更喜欢这样做,ggraph
因为我喜欢包装提供的其他美学。
似乎对ggraph
;中的节点颜色几乎没有控制。而边缘颜色被广泛覆盖。
我的问题是:如何将我的边缘与使用自定义函数着色的节点匹配,以便“离开”节点的每个边缘的颜色与其节点相同。这应该可以帮助人们更轻松地跟踪通过网络的流程。一个更普遍的问题是:ggraph 如何在美学论点之外分配颜色。我的问题类似于我之前在这里提出的另一个问题,但反过来(将边缘与节点匹配),在此处找到。
这是一个可重现的示例:
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()