NBK
NBK

Reputation: 905

r igraph - how does plot() read the layout matrix?

My question is related to this one here, which unfortunately has not been responded. I'm trying to automatically annotate text next to highlighted communities on a plot. An intermediate step is to understand how nodes are placed on a plot.

G <- make_graph('zachary')
l <- layout_with_fr(G)
l

A layout is a matrix with rows representing nodes and columns representing the x and y plot parameters.

           [,1]       [,2]
 [1,] 2.8510654 -2.2404898
 [2,] 2.7183497 -1.1815130
 [3,] 3.1429205  0.1117099
 [4,] 1.5585372 -1.0743325
 [5,] 2.2808632 -4.2035479
 [6,] 2.1698198 -5.0526766
 [7,] 1.4938068 -4.6975884
 [8,] 1.9710816 -1.4672218
 [9,] 3.5407035  0.5407852
[10,] 2.2222909  1.9079805
[11,] 3.0784642 -4.5828448
[12,] 4.4115351 -4.1057462
[13,] 0.6002378 -2.2432049
[14,] 2.5010525 -0.1563341
[15,] 4.8914673  4.1417759
[16,] 3.2053338  3.9212694
[17,] 1.1825200 -6.4099021
[18,] 3.7155897 -2.8354432
[19,] 3.8272351  4.2660906
[20,] 3.8636487 -0.5671906
[21,] 2.7302411  3.3998888
[22,] 1.6084374 -2.7407388
[23,] 4.3432855  3.8101278
[24,] 5.9392042  2.2364929
[25,] 6.9980077  0.2389222
[26,] 7.1608499  1.1360134
[27,] 6.0171481  4.0279067
[28,] 5.4996627  1.0367163
[29,] 4.4961257  0.9434659
[30,] 5.5987563  3.2314488
[31,] 2.9958404  1.2022317
[32,] 5.1188900  0.2919268
[33,] 4.1088296  2.5032294
[34,] 4.1686534  2.1339884

enter image description here

But the x, y coordinates of the plot go from -1 to 1, unlike the min-max coordinates in the layout matrix. So how is plot(G, layout = l) reading the layout matrix?

Upvotes: 0

Views: 166

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24838

The according to the source, the plot method for objects of class igraph simply rescales the matrix from -1 to 1.

library(igraph)
set.seed(3)
l <- layout_with_fr(G)
        [,1]   [,2]
 [1,] -2.283  0.658
 [2,] -1.289 -0.108
 [3,]  0.146  1.012
 [4,] -1.523  1.601
#... with 30 more rows.
plot(G,layout = l)

enter image description here

maxs <- apply(l, 2, max)
mins <- apply(l, 2, min)
ll <- scale(l, center=(maxs+mins)/2, scale=(maxs-mins)/2)
ll
         [,1]    [,2]
 [1,] -0.2422 -0.1051
 [2,] -0.0704 -0.3821
 [3,]  0.1775  0.0228
 [4,] -0.1108  0.2357
#... with 30 more rows.
plot(G,layout = ll)

enter image description here

Note that the actual rescaling is performed with igraph::norm_coords:

igraph::norm_coords(l)
         [,1]    [,2]
 [1,] -0.2422 -0.1051
 [2,] -0.0704 -0.3821
 [3,]  0.1775  0.0228
 [4,] -0.1108  0.2357
#... with 30 more rows.

Upvotes: 2

Related Questions