# Twisting a graph layout (by fixing the $x$ coordinate of all its vertices)

I would like to draw a given graph $G$ in a specific way and I would like to know whether Mathematica can do it in a (semi-)automated way.

My goal is to draw the graph $G$ in 2D, fixing all the $x$ coordinates of its vertices and letting the $y$ coordinates be arranged as they would be arranged by already existing layouts such as "SpringElectricalEmbedding" or "SpringEmbedding" (taking obviously in account the already fixed $x$ coordinates).

Q1) Is there a way to do this in Mathematica, possibly using one of the sub-options of "SpringElectricalEmbedding" or "SpringEmbedding" (e.g. "EnergyControl")?

Q2) If the answer to the first question is negative, how would one build a function that, given the graph $G$ and the $x$ coordinates of its vertices, computes the $y$ coordinates to obtain such force-directed layout?

• The other way around is possiblel (letting Mma set a layout and the re-setting the x coordinate), I think . However, if you want to first set the x coordinates I doubt you can get Mma to recalculate the ys – Dr. belisarius Dec 1 '14 at 17:46

You can use GraphLayout to arrange your Vertexes by SpringEmbedding or other function, then extract the full two-dimensional locations of the vertexes, then assign the x-coordinate of each vertex to your known or desired x coordinates and re-display the graph with these new vertex locations.

 g = CompleteGraph[20, GraphLayout -> "SpringElectricalEmbedding"]; myVertexCoordinates = VertexCoordinates /.
AbsoluteOptions[g, VertexCoordinates][];

myFixedXCoordinates = Table[RandomReal[],
{Length[myVertexCoordinates]}];

myNewVertexCoordinates =  Table[
{myFixedXCoordinates[[i]],
myVertexCoordinates[[i, 2]]},
{i, Length[myFixedXCoordinates]}];

Graph[g, VertexCoordinates -> myNewVertexCoordinates] 