简体   繁体   English

在线性 ggraph arcplot 中控制弧 position(向上或向下)

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

Background背景

I have a network of nodes and edges which I would like to visualize as a linear arcplot.我有一个节点和边网络,我想将其可视化为线性弧图。 Based on my limited knowledge, I believe that {ggraph} is a good tool for this (especially given my familiarity with {ggplot2} ) so that's what I'm trying here, but if theres's a compelling alternative I'm open to it.根据我有限的知识,我相信{ggraph}是一个很好的工具(特别是考虑到我对{ggplot2}的熟悉)所以这就是我在这里尝试的,但如果有一个引人注目的替代方案,我愿意接受。

Problem问题

I know it can render arcs above the line of nodes (as shown in my example below) but can also sometimes render them below.我知道它可以在节点线上方渲染弧线(如下面的示例所示),但有时也可以在下方渲染它们。 It looks like this can be controled by the strength argument.看起来这可以通过strength参数来控制。 This works when I put strength outside aes for the arc (eg strength = -1 flips them all down), but gives the warning Ignoring unknown aesthetics: strength when applied inside aes as shown below.当我将strength放在aes之外时,这有效(例如, strength = -1将它们全部向下翻转),但会发出警告Ignoring unknown aesthetics: strengthaes内应用时的力量,如下所示。

Current effort目前的努力

Below is a simple example to illustrate what I've tried so far and the resulting plot.下面是一个简单的示例来说明我到目前为止所尝试的内容以及生成的 plot。 I have searched StackOverflow and the ggraph documentation but can't seem to find the answer there.我搜索了 StackOverflow 和 ggraph 文档,但似乎无法在那里找到答案。 I have also tried the variants geom_edge_arc2 and geom_edge_arc0 without success.我也尝试过geom_edge_arc2geom_edge_arc0变体但没有成功。 As a workaround, I could make a vector of values to feed to strength outside aes , but ideally I could do something inside aes with the data already provided to the function.作为一种解决方法,我可以制作一个值向量来馈送到aes之外的strength ,但理想情况下,我可以使用已经提供给 function 的数据在aes内部做一些事情。 Am I misunderstanding the intended syntax or being too picky?我是否误解了预期的语法或过于挑剔?

Desired output所需 output

I would like to be able to control the direction (above or below) of each arc.我希望能够控制每个弧的方向(上方或下方)。 For example, blue arcs above and red below based on the sign of edge_width (ie strength = sign(edge_width) ).例如,上方的蓝色弧线和下方的红色弧线基于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

Created on 2021-02-26 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 2 月 26 日创建


Session info Session 信息

In case it matters:万一这很重要:

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

Created on 2021-02-26 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 2 月 26 日创建

It's been almost a year so until a better answer arrives, I'll post my workaround as a solution in case anyone else has this issue.已经快一年了,所以在更好的答案到来之前,我会发布我的解决方法作为解决方案,以防其他人遇到这个问题。

As proposed in the question, a workaround is to separately pull a vector of strength values and then feed that to the geom_edge_arc call outside of aes .正如问题中提出的那样,一种解决方法是单独提取strength值向量,然后将其提供给aes之外的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)

Created on 2021-12-30 by the reprex package (v2.0.1)reprex package (v2.0.1) 于 2021 年 12 月 30 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM