正如标题所说。我有一个使用创建的图形对象igraph::sample_grg()
,我想使用它来绘制它ggraph
,节点根据节点属性x
和y
. 我试过的:
创建图表:
set.seed(1234)
library(igraph)
library(ggraph)
library(tidygraph)
g <- sample_grg(5, 0.4, torus = FALSE, coords = TRUE)
图表带有x
和y
属性,应该是节点位置
vertex.attributes(g)
$x
[1] 0.009495756 0.113703411 0.609274733 0.666083758 0.860915384
$y
[1] 0.6222994 0.6233794 0.6403106 0.2325505 0.5142511
尝试 1
ggraph(g) +
geom_edge_link() +
geom_node_point()
Error in graph_to_tree(graph, mode = direction) : Graph must be directed
我没有或不想要有向图。
尝试 2
l1 = data.frame(x = V(g)$x, y = V(g)$y)
ggraph(g, layout = l1) +
geom_edge_link() +
geom_node_point()
Error: `data` must be uniquely named but has duplicate columns
Run `rlang::last_error()` to see where the error occurred.
尝试 3
l2 <- create_layout(g, layout = l1)
l2 <- l2[,-c(1:2)]
ggraph(l2, layout = l2) +
geom_edge_link() +
geom_node_point()
Error in .register_graph_context(attr(plot$data, "graph"), free = TRUE) :
is.tbl_graph(graph) is not TRUE
尝试 4
g2 <- as_tbl_graph(g)
ggraph(g2, layout = "manual") +
geom_edge_link() +
geom_node_point()
Error in eval_tidy(x, .N()) : object '' not found
在这个问题之后尝试5 :
ggraph(g, layout = "manual", circular = FALSE, node.positions = l1) +
geom_edge_link() +
geom_node_point()
Error in layout_fun(graph, circular = circular, ...) :
unused argument (node.positions = list(c(0.0612166156060994, 0.0649281670339406, 0.250601968728006, 0.726055516628549, 0.916498943232), c(0.979841426480561, 0.450912220636383, 0.283102283952758, 0.782102265860885, 0.663251199992374)))
我觉得我错过了一些非常明显的东西,任何建议都会非常感激。