1

背景

我有一个节点和边网络,我想将其可视化为线性弧图。根据我有限的知识,我相信这{ggraph}是一个很好的工具(特别是考虑到我对 的熟悉{ggplot2})所以这就是我在这里尝试的,但如果有一个引人注目的替代方案,我愿意接受。

问题

我知道它可以在节点线上方渲染弧线(如下面的示例所示),但有时也可以在下方渲染它们。看起来这可以由strength参数控制。当我将弧线放在strength外面时aes(例如strength = -1将它们全部向下翻转),这有效,但Ignoring unknown aesthetics: strength在内部应用时会发出警告aes,如下所示。

目前的努力

下面是一个简单的例子来说明我到目前为止所尝试的内容和结果图。我搜索了 StackOverflow 和 ggraph 文档,但似乎无法在那里找到答案。我也尝试了这些变体geom_edge_arc2geom_edge_arc0但没有成功。作为一种解决方法,我可以制作一个值向量来提供给strengthoutside aes,但理想情况下,我可以aes使用已经提供给函数的数据在内部做一些事情。我是否误解了预期的语法或过于挑剔?

期望的输出

我希望能够控制每个弧的方向(上方或下方)。例如,上方的蓝色弧线和下方的红色弧线基于edge_width(ie strength = sign(edge_width)) 的符号。


# load packages
library(tidyverse, warn.conflicts = FALSE)
library(tidygraph, warn.conflicts = FALSE)
library(igraph, warn.conflicts = FALSE)
library(ggraph, warn.conflicts = FALSE)

# make random sim data reproducible
set.seed(1)

# define nodes
nodes <- data.frame(node_name = paste0("node", 1:5))

# define edges
edges <- t(combn(nodes$node_name, 2)) %>%
  as_tibble(.name_repair = "universal") %>% 
  rename(from = 1, to = 2) %>% 
  mutate(edge_width = sample(x = -10:10, size = nrow(.), replace = T))
#> New names:
#> * `` -> ...1
#> * `` -> ...2

# build network from nodes and edges
network <- tbl_graph(edges = edges, nodes = nodes, directed = FALSE)

# visualize network as arcplot
network %>% 
  ggraph(layout = "linear") +
  geom_edge_arc(aes(color = edge_width >= 0,
                    width = abs(edge_width),
                    strength = sign(edge_width)),
                alpha = 0.65) +
  geom_node_label(aes(label = node_name), size = 3)
#> Warning: Ignoring unknown aesthetics: strength

reprex 包于 2021-02-26 创建(v1.0.0)


会话信息

万一这很重要:

sessionInfo()
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18363)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] ps_1.5.0          digest_0.6.27     assertthat_0.2.1  magrittr_2.0.1   
#>  [5] reprex_1.0.0      evaluate_0.14     highr_0.8         stringi_1.5.3    
#>  [9] rlang_0.4.10      cli_2.3.1         rstudioapi_0.13   fs_1.5.0         
#> [13] rmarkdown_2.7     tools_4.0.2       stringr_1.4.0     glue_1.4.2       
#> [17] xfun_0.21         yaml_2.2.1        compiler_4.0.2    htmltools_0.5.1.1
#> [21] knitr_1.31

reprex 包于 2021-02-26 创建(v1.0.0)

4

1 回答 1

0

已经快一年了,所以在更好的答案到来之前,我会发布我的解决方法作为解决方案,以防其他人遇到这个问题。

正如问题中提出的,一种解决方法是单独提取一个strength值向量,然后将其提供geom_edge_arcaes.

# load packages
library(tidyverse)
library(tidygraph)
library(igraph)
library(ggraph)

# make random sim data reproducible
set.seed(1)

# define nodes
nodes <- data.frame(node_name = paste0("node", 1:5))

# define edges
edges <- t(combn(nodes$node_name, 2)) %>%
  as_tibble(.name_repair = "universal") %>% 
  rename(from = 1, to = 2) %>% 
  mutate(edge_width = sample(x = -10:10, size = nrow(.), replace = T))

# extract vector of desired arc positions based on sign of edge width
arc_direction <- sign(edges$edge_width)

# build network from nodes and edges
network <- tbl_graph(edges = edges, nodes = nodes, directed = FALSE)

# visualize network as arcplot
network %>% 
  ggraph(layout = "linear") +
  geom_edge_arc(aes(color = edge_width >= 0,
                    width = abs(edge_width)),
                strength = arc_direction,
                alpha = 0.65) +
  geom_node_label(aes(label = node_name), size = 3)

reprex 包于 2021-12-30 创建(v2.0.1)

于 2021-12-30T14:08:22.067 回答