ggraph中的图例行粗细

时间:2019-06-07 14:25:14

标签: r 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))) 

Can barely see the legend lines

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

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为您真的想调整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) 

enter image description here