Retrieving node coordinates from ggraph network chart

跟風遠走 提交于 2019-12-02 17:28:23

问题


Let's say I produce this chart:

library(ggraph)
library(igraph)

my_chart <- graph_from_data_frame(highschool)
set.seed(2017)

ggraph(my_chart, layout = "nicely") + geom_edge_link() + geom_node_point()

How would one retrieve the x and y coordinates of the nodes from this chart?


回答1:


Using ggplot_build

library(ggraph)
library(igraph)

my_chart <- graph_from_data_frame(highschool)
set.seed(2017)

p <- ggraph(my_chart, layout = "nicely") + geom_edge_link() + geom_node_point()

pg <- ggplot_build(p)

lines are in pg[[1]][[1]]

ggplot(data= pg[[1]][[1]])+
  geom_line(aes(x=x,y=y, group=group), size = 0.1)

while points are in pg[[1]][[2]]

ggplot(data= pg[[1]][[1]])+
  geom_line(aes(x=x,y=y, group=group), size = 0.1)+
  geom_point(data= pg[[1]][[2]], aes(x=x,y=y, group=group), color = "red")




回答2:


You may use some of layout_ functions from igraph package. They return a matrix with vertex coordinates.



来源:https://stackoverflow.com/questions/46408841/retrieving-node-coordinates-from-ggraph-network-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!