如何在R中使用ggraph网络调整边缘宽度?

4

我需要使用R的ggraph生成一个网络图。我想要根据权重变量(或节点大小)调整边的宽度。有人知道我该如何做吗?非常感谢。

以下是示例代码:

library(ggraph)
library(igraph)
data=data.frame(w1=rep('like', 5), 
           w2 = c('apple', 'orange', 'pear','peach', 'banana'),
           weight= c(2,3,5,8, 15))
data %>% 
  graph_from_data_frame() %>% 
  ggraph(layout = "fr") +
  geom_edge_link(alpha = .25) +
  geom_node_point(color = "blue", size = 2) + 
  geom_node_text(aes(label = name),  repel = TRUE)

我认为这个链接可能会对您有所帮助:https://github.com/thomasp85/ggraph/issues/144 - which_command
@which_command,该问题似乎由于包冲突已经解决。该问题已在一年前关闭。 - camille
2
你看过 geom_edge_link 的文档吗?它们清楚地列出了 edge_width 作为可以与此 geom 一起使用的审美之一。 - camille
@camille 它在哪里?geom_edge_link(mapping = NULL, data = get_edges("short"), position = "identity", arrow = NULL, n = 100, lineend = "butt", linejoin = "round", linemitre = 1, label_colour = "black", label_alpha = 1, label_parse = FALSE, check_overlap = FALSE, angle_calc = "rot", force_flip = TRUE, label_dodge = NULL, label_push = NULL, show.legend = NA, ...) - zesla
Geom文档通常有一个标题,称为“美学”,列出了可接受的美学,即可以映射到aes中的数据并传递给该geom的mappinggeom_edge_link也不例外。 edge_width是其中之一的美学。 - camille
非常感谢,它有效了。 - zesla
1个回答

6

是的,你可以在大多数geom_edge_*中使用widthaes来实现这一点。此外,你可以使用scale_edge_width根据加权变量微调最小/最大宽度。请参见下面的两个示例。

另外,我认为这个美学问题与ggforce有关(这也给我带来了麻烦)。确保你已经更新到最新版本的ggraphggforce

library(ggraph)
library(igraph)
data=data.frame(w1=rep('like', 5), 
                w2 = c('apple', 'orange', 'pear','peach', 'banana'),
                weight= c(2,3,5,8, 15))

第一个使用默认权重

data %>% 
  graph_from_data_frame() %>% 
  ggraph(layout = "fr") +
  geom_edge_link(alpha = .25, 
                 aes(width = weight)) +
  geom_node_point(color = "blue", size = 2) + 
  geom_node_text(aes(label = name),  repel = TRUE)+
  theme_graph()+
  labs(title = 'Graph with weighted edges', 
       subtitle = 'No scaling')

enter image description here

使用scale_edges_width来设置范围。 注意-scale_edges*可以接受多个参数。
data %>% 
  graph_from_data_frame() %>% 
  ggraph(layout = "fr") +
  geom_edge_link(alpha = .25, 
                 aes(width = weight)) +
  geom_node_point(color = "blue", size = 2) + 
  geom_node_text(aes(label = name),  repel = TRUE)+
  scale_edge_width(range = c(1, 20))+ # control size
  theme_graph()+
  labs(title = 'Graph with weighted edges', 
       subtitle = 'Scaling add with scale_edge_width()')

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接