从ggraph网络图检索节点坐标 [英] Retrieving node coordinates from ggraph network chart

查看:106
本文介绍了从ggraph网络图检索节点坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我制作了这张图表:

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()

人们将如何从此图表中检索节点的x和y坐标?

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

推荐答案

使用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")

这篇关于从ggraph网络图检索节点坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆