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

r - ggraph中的图例行粗细-LMLPHP

我的首选输出将更像这样:

r - ggraph中的图例行粗细-LMLPHP

最佳答案

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

r - ggraph中的图例行粗细-LMLPHP

关于r - ggraph中的图例行粗细,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56496168/

10-09 17:22