3
votes

I am trying to manually set the vertex coordinates of an igraph plot. The coordinates represent latitude and longitude values. I keep getting the error:

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

Here are the objects/variables I'm using:

graph object-

    > 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

with the following vertex attributes -

 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"

spatial coordinates for positions of vertices:

 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

1 Answers

-1
votes

I can't really answer your question but maybe the things I tried can help you.

I have the same problem since I try to have different vertex shapes than circle for some vertices. Having different vertex shapes and not specifying a layout matrix works fine. And having all vertex.shape = "circle" and specifying a layout matrix works as well. Setting vertex.shape = "square" and trying to plot with a layout matrix with this - not reproducible - line:

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))

gives this error:

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

As you can see, I already try to force the sizes and layout to be numeric (allthough they are set as numeric values from the beginning). You can also replace vertex.shape = "circle" with vertex.shape = "rectangle", so that .size2 actually makes sense. The error will be the same.

Therefore, I assume the problems are with positioning vertices of shape "square" or "rectangle".

In the documentation there is only one example of plotting with shapes and a defined layout which didn't really help me.