gpt4 book ai didi

r - igraph 解决具有不同节点大小 r 的重叠节点

转载 作者:行者123 更新时间:2023-12-02 19:15:16 25 4
gpt4 key购买 nike

我正在使用 R 中的 igraph 创建一个由大约 600 个节点包围的单个“中心”节点的网络可视化。

节点重叠,然后我可以使用 this question 的答案来解决这个问题(使用 qgraph)。

但是,此解决方案似乎仅适用于大小相同的节点。在我的网络中,节点大小是可变的。有没有办法通过在确定节点之间的距离时考虑节点大小来避免重叠?

示例代码如下:

# create network
net <- graph_from_data_frame(d=links, vertices=nodes, directed=T)

# set colors
colrs <- c("#8DD3C7", "#FFFFB3")
V(net)$color <- colrs[V(net)$type]
# no labels
V(net)$label <- NA

# create a network graph with non-overlapping nodes:
# using https://stackoverflow.com/questions/39290909/igraph-resolving-tight-overlapping-nodes
e <- get.edgelist(net,names = F)
l <- qgraph.layout.fruchtermanreingold(e,vcount=vcount(net))
plot(net,layout=l,vertex.size=4,edge.arrow.mode=0,vertex.label=NA)

这是结果:

non overlapping network - equal sized nodes

但是现在当我更改节点大小时:

# setting node size based on data
V(net)$size <- V(net)$nodesize;
# plot result
plot(net,layout=l,edge.arrow.mode=0,vertex.label=NA)

...节点重叠:

overlapping nodes - varying size nodes

感谢您的帮助!

* 已编辑 - 添加示例数据集:前 50 个节点 *

节点数据:

dput(head(nodes,50))

structure(list(id = c("s01", "s02", "s03", "s04", "s05", "s06", "s07", "s08",
"s09", "s10", "s11", "s12", "s13", "s14", "s15", "s16", "s17", "s18", "s19",
"s20", "s21", "s22", "s23", "s24", "s25", "s26", "s27", "s28", "s29", "s30",
"s31", "s32", "s33", "s34", "s35", "s36", "s37", "s38", "s39", "s40", "s41",
"s42", "s43", "s44", "s45", "s46", "s47", "s48", "s49", "s50"), nodesize =
c(50, 2.025, 2.025, 3.5, 1, 0.725, 2.875, 1.6, 0.175, 2.175, 0, 0.675, 0.5,
15.7, 1.4, 0.4, 1.375, 0.425, 0.55, 7, 10.375, 1.125, 0.325, 0.925, 3.6, 0.525,
0.9, 0.1, 0.5, 2.3, 1.825, 1.95, 0.325, 0.9, 3, 0.475, 0.1, 2.975, 6.1, 9.225,
0.65, 3.05, 2.925, 6.35, 0.7, 0.2, 0.6, 1.7, 1.675, 1.425), type = c(1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(NA, 50L), class =
"data.frame")

链接数据:

dput(head(links,49))

structure(list(from = c("s01", "s01", "s01", "s01", "s01", "s01",
"s01", "s01", "s01", "s01", "s01", "s01", "s01", "s01", "s01",
"s01", "s01", "s01", "s01", "s01", "s01", "s01", "s01", "s01",
"s01", "s01", "s01", "s01", "s01", "s01", "s01", "s01", "s01",
"s01", "s01", "s01", "s01", "s01", "s01", "s01", "s01", "s01",
"s01", "s01", "s01", "s01", "s01", "s01", "s01"), to = c("s02",
"s03", "s04", "s05", "s06", "s07", "s08", "s09", "s10", "s11",
"s12", "s13", "s14", "s15", "s16", "s17", "s18", "s19", "s20",
"s21", "s22", "s23", "s24", "s25", "s26", "s27", "s28", "s29",
"s30", "s31", "s32", "s33", "s34", "s35", "s36", "s37", "s38",
"s39", "s40", "s41", "s42", "s43", "s44", "s45", "s46", "s47",
"s48", "s49", "s50")), row.names = c(NA, 49L), class = "data.frame")

最佳答案

鉴于您的图表是“星”图,我认为您不太可能找到另一种布局算法。大多数会将连接最紧密的节点推到中心。

有一个新的包 graphlayouts,它的 layout_with_stress 可以产生大约相同的结果。一种可能有效的方法是调整节点大小的比例。执行此操作的一个简单方法是调整节点大小的比例,使它们不会重叠。 ggraph 库可以帮助解决这个问题。

library(tidyverse)
library(igraph)
library(ggraph)
library(tidygraph)
library(graphlayouts)
library(scales)


g <- make_star(600)

par(mar = rep(0,4))

V(g)$size <- sample(1:10, vcount(g), T)

plot(g,
vertex.label = NA,
layout = l)

Base Plot

(no_scale_g <- g %>% 
as_tbl_graph() %>%
activate(nodes) %>%
mutate(size = sample(1:10, vcount(g), replace = T)) %>%
ggraph(., layout = 'stress')+
geom_edge_fan(aes(alpha = ..index..),
show.legend = F,
check_overlap = T)+
geom_node_point(aes(size = size,
color = size))+
coord_equal())

ggraph no scale

(scale_g <- no_scale_g+
scale_size(range = c(1, 3)))

ggraph with node scaling

您可以调整缩放参数以确保没有重叠。它并不完美,但对于静态图来说,这已经非常接近了。

关于r - igraph 解决具有不同节点大小 r 的重叠节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53870170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com