ggraph树中节点的控制顺序
Control order of nodes in ggraph tree
是否可以控制 ggraph
树中节点的水平顺序?
- 以下代码创建一棵树:
library(ggraph)
library(igraph)
library(tidyverse)
mygraph <- tibble(from = c("A", "A", "C", "C"), to = c( "B", "C", "D", "E")) %>%
graph_from_data_frame()
V(mygraph)$node_label <- names(V(mygraph))
mygraph %>%
ggraph(layout = 'tree', circular = FALSE) +
geom_edge_diagonal(edge_width = 0.12) +
geom_node_label(aes(label=node_label), size = 10,
label.size = 0, label.padding = unit(0.1, "lines")) +
theme_void() +
coord_fixed()
- 结果树:
我希望能够将节点 B 作为左分支,将节点 C 及其后续节点作为右分支。
图表节点的顺序不正确,请先重新排序。 有一个简单的方法,我将在下面使用。
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(ggraph))
suppressPackageStartupMessages(library(igraph))
mygraph <- tibble(from = c("A", "A", "C", "C"), to = c( "B", "C", "D", "E")) %>%
graph_from_data_frame()
# out of order
names(V(mygraph))
#> [1] "A" "C" "B" "D" "E"
# order the nodes
s <- sort(names(V(mygraph)))
mygraph <- permute(mygraph, match(V(mygraph)$name, s))
由 reprex package (v2.0.1)
于 2022 年 3 月 12 日创建
现在分配节点标签并绘制图形。
V(mygraph)$node_label <- names(V(mygraph))
mygraph %>%
ggraph(layout = 'tree', circular = FALSE) +
geom_edge_diagonal(edge_width = 0.12) +
geom_node_label(aes(label=node_label), size = 10,
label.size = 0, label.padding = unit(0.1, "lines")) +
theme_void() +
coord_fixed()
由 reprex package (v2.0.1)
于 2022 年 3 月 12 日创建
是否可以控制 ggraph
树中节点的水平顺序?
- 以下代码创建一棵树:
library(ggraph)
library(igraph)
library(tidyverse)
mygraph <- tibble(from = c("A", "A", "C", "C"), to = c( "B", "C", "D", "E")) %>%
graph_from_data_frame()
V(mygraph)$node_label <- names(V(mygraph))
mygraph %>%
ggraph(layout = 'tree', circular = FALSE) +
geom_edge_diagonal(edge_width = 0.12) +
geom_node_label(aes(label=node_label), size = 10,
label.size = 0, label.padding = unit(0.1, "lines")) +
theme_void() +
coord_fixed()
- 结果树:
我希望能够将节点 B 作为左分支,将节点 C 及其后续节点作为右分支。
图表节点的顺序不正确,请先重新排序。
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(ggraph))
suppressPackageStartupMessages(library(igraph))
mygraph <- tibble(from = c("A", "A", "C", "C"), to = c( "B", "C", "D", "E")) %>%
graph_from_data_frame()
# out of order
names(V(mygraph))
#> [1] "A" "C" "B" "D" "E"
# order the nodes
s <- sort(names(V(mygraph)))
mygraph <- permute(mygraph, match(V(mygraph)$name, s))
由 reprex package (v2.0.1)
于 2022 年 3 月 12 日创建现在分配节点标签并绘制图形。
V(mygraph)$node_label <- names(V(mygraph))
mygraph %>%
ggraph(layout = 'tree', circular = FALSE) +
geom_edge_diagonal(edge_width = 0.12) +
geom_node_label(aes(label=node_label), size = 10,
label.size = 0, label.padding = unit(0.1, "lines")) +
theme_void() +
coord_fixed()
由 reprex package (v2.0.1)
于 2022 年 3 月 12 日创建