r igraph图布局错误:矩阵上的下标数不正确

时间:2016-06-10 15:47:12

标签: r layout plot geospatial igraph

我正在尝试手动设置igraph图的顶点坐标。坐标表示纬度和经度值。我一直收到错误:

Error in ec[, 3:4] <- t(sapply(seq(length = nrow(el)), function(x) { : 
incorrect number of subscripts on matrix

以下是我使用的对象/变量:

图形对象 -

    > g_mat_cut
IGRAPH UNW- 28 88 -- 
+ attr: name (v/c), shape (v/c), color (v/c), weight (e/n)
+ edges (vertex names):
 [1] 010C10AB53--010C10CA44 010C10AB53--010C10D28B 010C10AB53--010C10D28E
 [4] 010C10AB53--010C10EA1E 010C10AB53--010C1108EB 010C10AB53--010C11169A
 [7] 010C10AB53--010C112145 010C10AB53--010C117822 010C10AB53--010C117E53
[10] 010C10AB53--010C1180A0 010C10AB53--010C11922B 010C10AB53--010C11B911
[13] 010C10AB53--010C11BBA6 010C10AB53--010C11BC8D 010C10AB53--010C11BEB7
[16] 010C10C3A5--010C116DC1 010C10CA44--010C10D28B 010C10CA44--010C114E81
[19] 010C10CA44--010C11B911 010C10CA44--010C11BBA6 010C10CBBD--010C117822
[22] 010C10D28B--010C10D28E 010C10D28B--010C1108EB 010C10D28B--010C11169A
+ ... omitted several edges

具有以下顶点属性 -

 vertex.attributes(g_mat_cut)
$name
 [1] "010C10AB53" "010C10BEA5" "010C10C3A5" "010C10CA44" "010C10CBBD"
 [6] "010C10D28B" "010C10D28E" "010C10EA1E" "010C10EFE8" "010C11059F"
[11] "010C1108EB" "010C1111B5" "010C111428" "010C11169A" "010C112145"
[16] "010C114E81" "010C115391" "010C116DC1" "010C117822" "010C117E53"
[21] "010C1180A0" "010C11922B" "010C119C7E" "010C11A61D" "010C11B911"
[26] "010C11BBA6" "010C11BC8D" "010C11BEB7"

$shape
 [1] "circle" "square" "square" "square" "square" "square" "square" "square"
 [9] "circle" "square" "square" "square" "square" "square" "square" "circle"
[17] "circle" "circle" "circle" "circle" "square" "square" "square" "square"
[25] "square" "circle" "square" "circle"

$color
 [1] "#99D594" "#3288BD" "#3288BD" "#3288BD" "#99D594" "#99D594" "#3288BD"
 [8] "#3288BD" "#D53E4F" "#D53E4F" "#99D594" "#3288BD" "#3288BD" "#3288BD"
[15] "#3288BD" "#3288BD" "#D53E4F" "#D53E4F" "#3288BD" "#3288BD" "#3288BD"
[22] "#99D594" "#3288BD" "#99D594" "#3288BD" "#3288BD" "#FEE08B" "#FEE08B"

顶点位置的空间坐标:

 coords
          [,1]      [,2]
 [1,] 50.19954 -5.169506
 [2,] 50.19866 -5.168653
 [3,] 50.19853 -5.169905
 [4,] 50.19856 -5.168397
 [5,] 50.19954 -5.169506
 [6,] 50.19794 -5.168535
 [7,] 50.19833 -5.169241
 [8,] 50.19897 -5.169436
 [9,] 50.19915 -5.169564
[10,] 50.19908 -5.169429
[11,] 50.19878 -5.169403
[12,] 50.19857 -5.170018
[13,] 50.19859 -5.168570
[14,] 50.19847 -5.169863
[15,] 50.19982 -5.171222
[16,] 50.19866 -5.168653
[17,] 50.19844 -5.168694
[18,] 50.19794 -5.168535
[19,] 50.19833 -5.169241
[20,] 50.19781 -5.168733
[21,] 50.19855 -5.168908
[22,] 50.19915 -5.169564
[23,] 50.20034 -5.170551
[24,] 50.19878 -5.169697
[25,] 50.19882 -5.170664
[26,] 50.19982 -5.171222
[27,] 50.19838 -5.170642
[28,] 50.19841 -5.169932

str(coords)
num [1:28, 1:2] 50.2 50.2 50.2 50.2 50.2 ...

attempt at plotting:

   plot(g_mat_cut,layout=coords,vertex.size=4,rescale=FALSE,xlim=c(50.19,50.21),ylim=c(-5.16,-5.18))
Error in ec[, 3:4] <- t(sapply(seq(length = nrow(el)), function(x) { : 
incorrect number of subscripts on matrix

1 个答案:

答案 0 :(得分:0)

我无法回答你的问题,但也许我尝试的事情可以帮到你。

我有同样的问题,因为我尝试为某些顶点设置不同于圆的顶点形状。 具有不同的顶点形状而不指定布局矩阵可以正常工作。拥有所有vertex.shape = "circle"并指定布局矩阵也可以。设置vertex.shape = "square"并尝试使用此布局矩阵进行绘图 - 不可重现 - 行:

plot(gBase, layout=matrix(c(as.numeric(V(gBase)$lon), as.numeric(V(gBase)$lat)), ncol=2), vertex.label=NA, vertex.shape = "square", vertex.size = as.numeric(V(gBase)$size), vertex.size2=as.numeric(V(gBase)$size2))

给出了这个错误:

Error in y1 - y2 : non-numeric argument to binary operator

正如您所看到的,我已经尝试将大小和布局强制为数字(尽管它们从头开始设置为数值)。您也可以将vertex.shape = "circle"替换为vertex.shape = "rectangle",以便.size2实际上有意义。错误将是相同的。

因此,我认为问题在于定位形状为“方形”或“矩形”的顶点。

documentation中,只有一个用形状和定义的布局绘图的例子对我没有帮助。