I'm trying to create network plot with user-defined coordinates in geomnet package for 11 nodes. After a quick research I've discovered that it can be done by setting layout.alg to NULL and passing x and y coordinates to the plot - but it's not specified whether I should use coordinates for 'from' or for 'to'. I've tried both approaches, but there's still an error:
Error in scale_apply(layer_data, x_vars, "train", SCALE_X, x_scales) :
Maybe I'm the one who do something wrong and it's very obvious, but, to be honest, I have no idea at all. I would be very grateful for any help with that.
My data:
# A tibble: 11 x 4
from to x y
<chr> <chr> <chr> <chr>
1 z1 z3 40 30
2 z2 z4 20 30
3 z3 z5 15 30
4 z4 z8 60 30
5 z5 z9 10 50
6 z6 z4 30 60
7 z7 z4 50 50
8 z8 z4 65 50
9 z9 z5 20 80
10 z10 z8 40 90
11 z11 z7 60 70
My code:
p <- ggplot(data=fromto, aes(from_id = from, to_id = to, x=x,y=y))
p + geom_net(directed = TRUE,
layout.alg = NULL,
size = 6, arrowsize = 0.5,
curvature = 0.05,
arrowgap = 0.02,
linewidth = 0.5)
When I'm using smaller dataset it seems to work quite fine.
from to x y
1 z1 z3 10 25
2 z2 z1 20 30
3 z3 z2 40 30
p <- ggplot(data=fromto, aes(from_id = from, to_id = to, x=x,y=y))
p + geom_net(directed = TRUE,
layout.alg = NULL,
size = 6, arrowsize = 0.5,
curvature = 0.05,
arrowgap = 0.02,
linewidth = 0.5)
After an accurate analysis of the
geom_net
code, I found that understanding howgeomnet:::StatNet$compute_network
works is fundamental for solving your problem.Typing
gives the (wrong) output:
Inside
geomnet:::StatNet$compute_network
an important step is the construction of theedgelist
matrix:The output is:
In the first row of the adjacency matrix we can see that
z1
andz3
are (correctly) connected but row-column labels have been ordered by character value, not taking account that labels contain embedded numbers which (in our mind) should be numerically sorted.The
sna::as.edgelist.sna
function relies on row-column positions and not on their labels, hence in the first row of the output it gives[1,] 1 5 1
. This is obviously wrong.These considerations suggest a possible solution: avoid vertix labels with embedded numbers and use (for example) only letters of the alphabet: