控制线性图圆弧图中的圆弧位置(向上或向下)

Controling arc position (up or down) in linear ggraph arcplot

背景

我有一个节点和边的网络,我想将其可视化为线性弧线图。根据我有限的知识,我相信 {ggraph} 是一个很好的工具(特别是考虑到我对 {ggplot2} 的熟悉程度)所以这就是我在这里尝试的,但如果有一个令人信服的选择我是敞开心扉。

问题

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

当前努力

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

期望的输出

我希望能够控制每个圆弧的方向(上方或下方)。例如,基于 edge_width 符号(即 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 package (v1.0.0)

于 2021-02-26 创建

Session 信息

以防万一:

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 package (v1.0.0)

于 2021-02-26 创建

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

如问题中所提出的,解决方法是单独提取 strength 值的向量,然后将其提供给 aes.[=16= 之外的 geom_edge_arc 调用]

# 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 package (v2.0.1)

于 2021-12-30 创建