igraph解析节点大小为r的重叠节点

时间:2018-12-20 14:01:13

标签: r igraph r-qgraph

我正在使用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")

2 个答案:

答案 0 :(得分:0)

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

有一个新的软件包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

您可以调整缩放参数,以确保没有重叠。这并不完美,但是对于静态图形,这可以使您更加接近。

答案 1 :(得分:0)

这似乎与 following post 相关,它提到了 qgraph 包的布局功能,它有一个 repulse.rad 的参数。 要使用它,您首先需要通过

将 igraph-network 对象 g 转换为边列表
## Transform to edgelist with names = FALSE, otherwise it crashed for me
e <- get.edgelist(g, names = FALSE)

layout <- qgraph.layout.fruchtermanreingold(e, vcount = vcount(g),
  area = 6*(vcount(g)^2), repulse.rad = vcount(g)^3)

## Plot via igraph
plot.igraph(network, vertex.label = NA, layout = fixedLayout)

## Or plot via ggraph
ggraph(network, layout = fixedLayout) +
    geom_edge_link(color = "lightgrey", width = 0.3) +
    geom_node_point(size = 3) +
    theme_void()

您可以调整 arearepulse.rad 参数以满足您的需求。

我认为修改布局应该是真正保证没有重叠的最好方法,而我必须承认有一些重叠的“烟花”风格也很不错!