【问题标题】:Alter X and Y coordinates of graph vertices in iGraph在 iGraph 中更改图形顶点的 X 和 Y 坐标
【发布时间】:2018-06-20 12:56:02
【问题描述】:

我正在使用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"
    ]
]

如您所见,坐标保存在graphics 属性中。这是有效的 GML 语法(至少 AFAIK)。但是,load_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 坐标?

【问题讨论】:

    标签: r igraph graph-theory


    【解决方案1】:

    来自igraphdocumentation,它说它读取GML的能力的限制之一是

    仅使用节点和边属性,并且仅当它们具有简单的 类型:整数、实数或字符串。所以如果一个属性是一个数组或一个 记录,则忽略。如果只有一些值,这也是正确的 属性很复杂。

    这可能是graphics 属性出现问题的原因。 igraph 也应该能够读取 graphml 格式,但可能会遇到同样的障碍。

    幸运的是,如果您需要解决方法,GML 是一种相对容易解析的人类可读格式。例如,如果您只需要节点的xy 坐标,则可以执行以下操作:

    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)
    

    【讨论】:

    • 感谢文档链接。似乎编写我自己的解析器将是这里唯一的解决方案。如果我仍然这样做,我将为 GraphML 文件编写一个解析器,因为它们是基于 XML 结构的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多