Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
改变iGraph中图顶点的X和Y坐标_R_Igraph_Graph Theory - Fatal编程技术网

改变iGraph中图顶点的X和Y坐标

改变iGraph中图顶点的X和Y坐标,r,igraph,graph-theory,R,Igraph,Graph Theory,我正在使用igraph包来绘制我在yEd中创建的图形。这是一个应该覆盖在地理地图上的图形,所以我想做的第一件事就是改变顶点坐标,使它们与它们所代表的经度/纬度相匹配。(简单线性变换) 我尝试使用GraphML格式,但iGraph无法读取由yEd创建的GraphML文件,因此,我将该图形保存为GML文件。顶点信息的结构如下所示: node [ id 0 label "" graphics [ x 2181.0043222386603

我正在使用
igraph
包来绘制我在yEd中创建的图形。这是一个应该覆盖在地理地图上的图形,所以我想做的第一件事就是改变顶点坐标,使它们与它们所代表的经度/纬度相匹配。(简单线性变换)

我尝试使用GraphML格式,但iGraph无法读取由yEd创建的GraphML文件,因此,我将该图形保存为GML文件。顶点信息的结构如下所示:

node [
    id  0
    label   ""
    graphics [
        x   2181.0043222386603
        y   1463.6747524664843
        w   5.2609883750396875
        h   5.1510372122625085
        type    "ellipse"
        raisedBorder    0
        fill    "#FFCC00"
        outline "#000000"
    ]
]
library(igraph)

g <- read_graph("graph.gml", format="gml") #create graph object

z <- readLines("graph.gml") #create character vector from which to grep out attributes

id <- trimws(gsub("\t\tid\t", " ", z[grep("\t\tid\t", z)])) #retrive node ids 
totid <- length(id) #retrieve number of nodes

#grep for and extract x and y attributes
xcor <- as.numeric(trimws(gsub("\t\t\tx\t", " ", z[grep("\t\t\tx\t", z)])))[1:totid]    
ycor <- as.numeric(trimws(gsub("\t\t\ty\t", " ", z[grep("\t\t\ty\t", z)])))[1:totid]

#edges will also have x and y coordinates, so we need to index by the number of nodes

#add attributes back into graph object
g <- set.vertex.attribute(g, "x", value=xcor) 
g <- set.vertex.attribute(g, "y", value=ycor)
如您所见,坐标保存在
graphics
属性中。这是有效的GML语法(至少是AFAIK)。但是,
load\u graph
无法正确处理此属性,并且iGraph不会保存坐标。当我尝试提取顶点属性时,我得到以下结果:

> vertex_attr(g) %>% glimpse()
# List of 3
#  $ id      : num [1:73] 0 1 2 3 4 5 6 7 8 9 ...
#  $ label   : chr [1:73] "" "" "" "" ...
#  $ graphics: chr [1:73] "qÑÜ\020#8" "qÑÜ\020#8" "qÑÜ\020#8" "qÑÜ\020#8" ...

如何正确加载图形,包括x和y坐标?

igraph
中,它指出其读取
GML
能力的限制之一是

仅使用节点和边属性,并且仅当它们具有简单的 类型:整数、实数或字符串。因此,如果属性是数组或 记录,则忽略它。如果只有 属性是复杂的

这可能就是为什么它在
图形
属性上遇到问题的原因
igraph
也应该能够读取
graphml
格式,但可能会遇到同样的障碍

幸运的是,如果您需要一种变通方法,
GML
是一种人类可读的格式,比较容易解析。例如,如果只需要节点的
x
y
坐标,可以执行以下操作:

node [
    id  0
    label   ""
    graphics [
        x   2181.0043222386603
        y   1463.6747524664843
        w   5.2609883750396875
        h   5.1510372122625085
        type    "ellipse"
        raisedBorder    0
        fill    "#FFCC00"
        outline "#000000"
    ]
]
library(igraph)

g <- read_graph("graph.gml", format="gml") #create graph object

z <- readLines("graph.gml") #create character vector from which to grep out attributes

id <- trimws(gsub("\t\tid\t", " ", z[grep("\t\tid\t", z)])) #retrive node ids 
totid <- length(id) #retrieve number of nodes

#grep for and extract x and y attributes
xcor <- as.numeric(trimws(gsub("\t\t\tx\t", " ", z[grep("\t\t\tx\t", z)])))[1:totid]    
ycor <- as.numeric(trimws(gsub("\t\t\ty\t", " ", z[grep("\t\t\ty\t", z)])))[1:totid]

#edges will also have x and y coordinates, so we need to index by the number of nodes

#add attributes back into graph object
g <- set.vertex.attribute(g, "x", value=xcor) 
g <- set.vertex.attribute(g, "y", value=ycor)
库(igraph)

g感谢提供文档链接。似乎编写自己的解析器将是这里唯一的解决方案。如果我要这样做,我将为GraphML文件编写一个解析器,因为它们基于XML结构。