document ability to pass x,y locations to gplot
Inspired by a stackoverflow question.
It would be nice to allow the vertices' positions to be specified by, say, a vector of (x, y) tuples, which are then normalized to whatever canvas is being used and plotted.
Turns out this functionality is present but it should really be in README.md.
To get the locations you can use:
locs_x, locs_y = spring_layout(g)
(or any other layout) and then you can change the positions (they are normalized between -1, 1) or use entirely your own (they will be normalized internally) with:
gplot(g, locs_x, locs_y)
Hope that helps someone.
Here you go:
# Load the adjacency matrix and graph attributes
mat_arcTwoNodes = [0 1 0 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 0 0 1;
1 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0;
1 0 0 1 0 0 0 1 0 0;
0 1 1 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 1 0;
0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 1 1 0 1 0]
vec_xNode = [1 3 8 2 4 5 6 7 9 9]
vec_yNode = [1 2 1 7 9 5 3 7 9 4]
# Load the packages
using LightGraphs, GraphPlot, MetaGraphs, GraphRecipes, Plots
# Create a generic graph
gr = SimpleDiGraph()
add_vertices!(gr, size(mat_arcTwoNodes, 1))
# Add edges based on the adjacency matrix
for i in 1:size(mat_arcTwoNodes, 1), j in 1:size(mat_arcTwoNodes, 2)
if mat_arcTwoNodes[i,j] != 0
add_edge!(gr, i, j)
else
nothing
end
end
# Attach the metagraph to the original graph
mgr = MetaDiGraph(gr)
# Add the attribute of nodes
for i in 1:size(mat_arcTwoNodes,1)
set_props!(mgr, i, Dict(
Symbol("vec_xNode") => vec_xNode[i],
Symbol("vec_yNode") => vec_yNode[i]
))
end
# Plot the graph
graphplot(gr, x=vec_xNode, y=vec_yNode)
The result is going to be as follow:

You may add other attributes to the plot (ask graphplot). I answered this question on stack overflow: https://stackoverflow.com/questions/55228773/plot-the-directed-graph-in-julia-with-vertices-in-fixed-positions-by-x-y-coordin/69485233#69485233
I think we can close this issue