我需要在 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)
最佳答案
是的,在大多数 geom_edge_*
中,您可以使用 width
aes
来做到这一点。此外,您可以使用 scale_edge_width
根据加权变量微调最小/最大宽度。请参阅下面的两个示例。
此外,我认为与 ggforce
有关的审美存在问题(这也给我带来了麻烦)。确保您已更新到最新版本的 ggraph
和 ggforce
。
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')
使用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()')
关于r - 如何在r中的ggraph网络中按权重调整边的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56173310/