【问题标题】:Problems using lat/long data for a layout in ggraph在 ggraph 中使用 lat/long 数据进行布局的问题
【发布时间】:2017-10-16 23:35:28
【问题描述】:

我试图弄清楚如何在ggraph 中使用纬度/经度布局,但似乎无法通过语法来解决问题。考虑使用从iris 数据集修改的一些数据的这个表示:

data <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9, 5.4, 4.8), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 
3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.4), Lon = c(-122.683, -122.688, 
-122.686, -122.683, -122.678, -122.675, -122.674, -122.673, -122.676, 
-122.674, -122.677, -122.68), Lat = c(45.523, 45.52, 45.514, 
45.515, 45.513, 45.514, 45.517, 45.519, 45.519, 45.522, 45.524, 
45.521)), class = "data.frame", .Names = c("Sepal.Length", "Sepal.Width", 
"Lon", "Lat"), row.names = c(NA, -12L))

library(ggplot2)
library(igraph)
library(ggraph)

坐标位置的简单图如下所示:

ggplot(data, aes(x = Lon, y = Lat)) +
  geom_point(size = 6)

layout 接受一个矩阵,所以我将在这里提取坐标并将其转换为矩阵:

spatial_layout <- layout.norm(as.matrix(data[,c(3,4)]))

然后把data变成一个igraph对象:

igraph_data <- graph_from_data_frame(data)

如果我使用基本的plot.igraph 进行绘图,则布局符合预期;每个点的空间坐标:

plot.igraph(igraph_data, layout = spatial_layout)

现在这是我遇到问题的地方。我宁愿使用ggraph 来利用ggplot2 的强大功能。但是我不确定如何让它接受空间布局。这不起作用:

ggraph(igraph_data, spatial_layout) +
  geom_edge_link() +
  geom_node_point(color = "black", size = 9, pch = 1) +
  geom_node_text(aes(label = name))

create_layout.igraph(graph, layout, ...) 中的错误:未知布局

也没有尝试创建自定义布局:

create_layout(data, layout = "spatial_layout")

create_layout.default(data, layout = "spatial_layout") 中的错误:
没有为 data.frame 类的对象定义布局函数

手动将纬度/经度数据添加到 igraph 对象似乎也不起作用:

igraph_data$layout=cbind(E(igraph_data)$Lon,E(igraph_data)$Lat)
ggraph(igraph_data) +
  geom_edge_link() +
  geom_node_point(color = "black", size = 9, pch = 1) +
  geom_node_text(aes(label = name))

使用nicely 作为默认布局 data.frame(..., check.names) 中的错误 = FALSE) :参数暗示不同的行数:12、17

所以最终我的问题是如何将空间数据添加到ggraph 布局?显然我在这里遗漏了一些东西,但我似乎无法找出正确的方法。

【问题讨论】:

    标签: r igraph ggraph


    【解决方案1】:

    我们可以使用ggraph::create_layout 进行等效的手动布局。但首先,我们需要使空间坐标的数量与我们正在传递的图中的顶点数量相匹配。看起来plot.igraph 正在默默地回收您的原始布局。

    # must have columns named x and y
    data2 <-  data.frame(x = rep(spatial_layout[,1], length.out = 17),
                         y = rep(spatial_layout[,2], length.out = 17))
    

    然后使用?layout_igraph_manual 的帮助页面作为后续参数名称的指南。

    manual_layout <- create_layout(graph = igraph_data,
                  layout = "manual", node.positions = data2)
    

    注意:当我直接调用函数时,我会发出警告Error: 'layout_igraph_manual' is not an exported object from 'namespace:ggraph'

    那我们就可以把它插到原来的里面了:

    ggraph(manual_layout) + 
        geom_edge_link() +
        geom_node_point(color = "black", size = 9, pch = 1) +
        geom_node_text(aes(label = name))
    

    默认情况下轴限制不一样,但这是一个小的 + xlim(-2.5,2.5)#ish 调整。

    【讨论】:

    • 谢谢!无论如何,您可以看到可以对此应用投影吗?我把它变成了一个 sf 对象,但 ggraph 不会绘制它。
    • 不是我的头顶,通过空间绘图包而不是ggraph 构建多边形或线可能更容易
    • 是的,这就是我采用的方法。在布局步骤之前使用与多边形相同的投影处理节点坐标,然后使用 geom_polygon 绘制多边形。效果很好。
    • 这种方法可用于将任何非 ggraph 布局转换为 ggraph 布局,非常有用(我用它来获取 layout_as_star(),它比一个在 ggraph 中,进入 gggraph。)
    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多