使用igraph以R为坐标绘制节点

使用igraph以R为坐标绘制节点,r,plot,igraph,R,Plot,Igraph,我有一个图形main和一个元数据框架meta 这是一张图表 city <- data.frame(from = c("London", "Paris", "Beijing", "Berlin"), to = c("Beijing", "Berlin", "London", "Paris")) main <- graph_fro

我有一个图形main和一个元数据框架meta

这是一张图表

city <- data.frame(from = c("London", "Paris", "Beijing", "Berlin"), to = c("Beijing", "Berlin", "London", "Paris"))
main <- graph_from_data_frame(city)

city这是igraph绘图的一个特点。如果查看页面
帮助(igraph.plotting)
,您会发现:

rescale
Logical constant, whether to rescale the coordinates to the [-1,1]x[-1,1](x[-1,1])
  interval. This parameter is not implemented for tkplot. 
Defaults to TRUE, the layout will be rescaled.
因此,即使布局(位置)的范围为[39.9,52.3]x[0.1-116.3],默认形式的
plot
也会将plot更改为区域[-1,1]x[-1,1]。您可以使用
rescale
参数来抑制此行为,但igraph不会仍然使用plot区域[-1,1]x[-1,1]默认情况下,图形将不在绘图区域内。因此,您还必须指定适当的
xlim
ylim
。但是,默认情况下,IGRAPHE将强制该区域的纵横比为1。由于数据中的lat-long有些不平衡,您可能还需要调整
asp
ar古门特,把这些放在一起,我明白了

plot(main, vertex.label = NA, vertex.size = 5, edge.width = 0.5, 
    layout = location, rescale=FALSE, asp=0,
    xlim = range(V(main)$latitude), ylim = range(V(main)$longitude))

现在,顶点的坐标将与数据中的纬度匹配。

谢谢你的回答。我将原始数据简化为此版本。我可以问一下
asp
参数的作用吗?我尝试从
元数据设置顶点属性时是否正确,因为我注意到一些城市不在
gra中在我尝试了你的方法后,这些节点仍然处于原来的位置,唯一的变化是绘图变得更大和分散。它们可能出现在屏幕上类似的位置,但现在坐标与布局向量匹配。你可以使用
text
显示纬度经度。当我将纬度与经度交换时,纬度经度似乎是正确的我意识到在坐标系中,纬度的值是y轴,经度的值是x轴。
location <- matrix(c(V(main)$latitude, V(main)$longitude), ncol = 2)
plot(main, vertex.label = NA, vertex.size = 5, edge.width = 0.5, layout = location)
rescale
Logical constant, whether to rescale the coordinates to the [-1,1]x[-1,1](x[-1,1])
  interval. This parameter is not implemented for tkplot. 
Defaults to TRUE, the layout will be rescaled.
plot(main, vertex.label = NA, vertex.size = 5, edge.width = 0.5, 
    layout = location, rescale=FALSE, asp=0,
    xlim = range(V(main)$latitude), ylim = range(V(main)$longitude))