ggraph 中的图例线粗细

Legend line thickness in ggraph

使用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))) 

我的首选输出看起来更像这样:

我认为你真的想调整 width 审美而不是 size,所以这是一个小修复。

但棘手的部分(至少对我而言)是因为 ggraph 会自动扩展美学名称,例如宽度 >>> edge_width,因此在尝试覆盖 guide_legend().

中的美学时需要使用 edge_x 格式

所以你最终得到这样的结果:

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)