如题所述,我有一个使用
创建图形:
图表带有
第三次尝试
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.
第三次尝试
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)))
我感觉自己错过了一些非常显而易见的东西,如果有建议将不胜感激。
ggraph
不使用“内置”的坐标系,而我需要提供一个空的数据框。 - flee