使用时ggraph
,有没有办法加粗边缘颜色的图例线?我试图覆盖但无济于事。这是一个例子:
library(tidyverse)
library(igraph)
library(ggraph)
set.seed(20190607)
#create dummy data
Nodes <- tibble(source = sample(letters, 8))
Edges <- Nodes %>%
mutate(target = source) %>%
expand.grid() %>%
#assign a random weight & color
mutate(weight = runif(nrow(.)),
color = sample(LETTERS[1:5], nrow(.), replace = TRUE)) %>%
#limit to a subset of all combinations
filter(target != source,
weight > 0.7)
#make the plot
Edges %>%
graph_from_data_frame(vertices = Nodes) %>%
ggraph(layout = "kk") +
#link width and color are dynamic
geom_edge_link(alpha = 0.5, aes(width = weight, color = color)) +
geom_node_point(size = 10) +
theme_graph() +
#don't need a legend for edge width, but the color override doesn't work
guides(edge_width = FALSE,
edge_color = guide_legend(override.aes = list(size = 2)))
Run Code Online (Sandbox Code Playgroud)
我喜欢的输出看起来更像是这样的:
我认为你真的想调整width
审美而不是size
,所以这是一个小修复。
但棘手的部分(至少对我来说)是因为ggraph
自动扩展美学名称,例如 width >>> edge_width,因此在尝试覆盖guide_legend()
.
所以你最终会得到这样的结果:
Edges %>%
graph_from_data_frame(vertices = Nodes) %>%
ggraph(layout = "kk") +
geom_edge_link(alpha = 0.5, aes(width = weight, edge_color = color)) +
geom_node_point(size = 10) +
theme_graph() +
guides(edge_color = guide_legend(override.aes = list(edge_width = 5)),
edge_width = F)
Run Code Online (Sandbox Code Playgroud)