简体   繁体   English

带日期轴的网络图

[英]Network Diagram with Date Axis

I want to create a directed network visualization with the X-Axis fixed in time.我想创建一个 X 轴及时固定的定向网络可视化。

Assuming I have the following data:假设我有以下数据:

Nodes节点

ID ID Label Label Date日期
1 1 a一个 2022-02-01 2022-02-01
2 2 b b 2022-03-13 2022-03-13
3 3 c c 2022-03-22 2022-03-22
4 4 d d 2022-04-20 2022-04-20

Edges边缘

From To
1 1 2 2
3 3 1 1
3 3 4 4

I want to create a visualization which looks like this:我想创建一个如下所示的可视化:

在此处输入图像描述

I tried using ggraph but I am having difficulty fixing the X-Axis based on the date.我尝试使用 ggraph,但我很难根据日期修复 X 轴。

Any advice is welcome!欢迎任何建议!

Below is some starter code:下面是一些启动代码:

library(tidyverse)
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(ggraph)

nodes <- tibble(id=1:4,label=letters[1:4], date=as.Date(c("2022-02-01", 
                                                          "2022-03-13", 
                                                          "2022-03-22", 
                                                          "2022-04-20")))
nodes
#> # A tibble: 4 × 3
#>      id label date      
#>   <int> <chr> <date>    
#> 1     1 a     2022-02-01
#> 2     2 b     2022-03-13
#> 3     3 c     2022-03-22
#> 4     4 d     2022-04-20
edges <- tibble(from = c(1L, 3L, 3L),
                  to = c(2L, 1L, 4L))
edges
#> # A tibble: 3 × 2
#>    from    to
#>   <int> <int>
#> 1     1     2
#> 2     3     1
#> 3     3     4

graph <- tbl_graph(nodes = nodes, edges = edges, directed=TRUE)
graph
#> # A tbl_graph: 4 nodes and 3 edges
#> #
#> # A rooted tree
#> #
#> # Node Data: 4 × 3 (active)
#>      id label date      
#>   <int> <chr> <date>    
#> 1     1 a     2022-02-01
#> 2     2 b     2022-03-13
#> 3     3 c     2022-03-22
#> 4     4 d     2022-04-20
#> #
#> # Edge Data: 3 × 2
#>    from    to
#>   <int> <int>
#> 1     1     2
#> 2     3     1
#> 3     3     4

ggraph(graph) + 
  geom_edge_link(end_cap = circle(3, 'mm'),
                 arrow = arrow(length = unit(4, 'mm'))) +  
  geom_node_label(aes(label = label))
#> Using `tree` as default layout

Created on 2022-09-07 with reprex v2.0.2使用reprex v2.0.2创建于 2022-09-07

You can use create_layout to get the data frame which contains the x and y positions.您可以使用create_layout获取包含 x 和 y 位置的数据框。 Simply copy the date column into the x column of the create_layout output.只需将date列复制到create_layout output 的x列中。 This object can be passed as the data object of ggraph :这个 object 可以作为 ggraph 的数据ggraph

my_layout <- create_layout(graph, "tree")
my_layout$x <- my_layout$date

ggraph(my_layout) + 
  geom_edge_link(end_cap = circle(3, 'mm'),
                 arrow = arrow(length = unit(3, 'mm'))) +  
  geom_node_label(aes(label = label), label.r = unit(4, "mm"), size = 8) +
  scale_x_date(limits = as.Date(c("2022-01-01", "2022-06-30")),
               date_breaks = "month", date_labels = "%b", name = NULL) +
  scale_y_reverse(name = NULL) +
  theme_classic(base_size = 20) +
  theme(axis.line.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank())

在此处输入图像描述

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

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