Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a network and want to plot it. I use LayeredGraphPlot, and the network is described as:

NetList={"A1" -> "a1", "A1" -> "b1", "b1" -> "A2", "a1" -> "a2", "A2" -> "b2",
"A2" -> "c2", "a2" -> "B3", "b2" -> "B3", "c2" -> "c3", "B3" -> "a3",
"B3" -> "b3", "a3" -> "A4", "b3" -> "A4", "c3" -> "c4", "A4" -> "a4",
"A4" -> "b4"};
LayeredGraphPlot[NetList, Bottom, VertexLabeling -> True]

Result:

GraphPlot

I have two question:

1) Main question: How can I tell LayeredGraphPlot to put all a1, a2, a3, a4 in one vertical line, and b1, b2, b3, b4 in one line, and c2, c3, c4 in one vertical line? (the c-elements are already in a vertical line coincidencely for this network).

2) Minor question: If I only want to have labeling for certain elements (such as the upper-case Elements A1, A2, B3, A4), how can I modify the LayeredGraphPlot-instruction?

Update

ad1) One methode to do this is VertexCoordinateRules - i'm afraid it doesnt get much easier than that.

Update2

With the nice solution of hftf (below), the example above works perfectly. However, there is a problem with different networks.

When I have:

NetList={"A1" -> "a1", "A1" -> "b1", "b1" -> "A2", "a1" -> "a2", "A2" -> "b2",
    "A2" -> "c2", "a2" -> "B3", "c2" -> "B3", "b2" -> "b3", "B3" -> "a3",
    "B3" -> "c3", "a3" -> "A4", "c3" -> "A4", "b3" -> "b4", "A4" -> "a4",
    "A4" -> "c4"};
GraphPlot[NetList, VertexLabeling -> True, VertexCoordinateRules -> coords, 
    DirectedEdges -> True]

I get the following:

GraphPlot2

The problem is, that b2 -> B3 and b3 -> A4, even though there is no rule in NetList for that. It happens because B3 and A4 are somehow in the way of b. In the documentary of LayeredGraphPlot, there are several examples how edges go around vertices that are in the way. But I could not get that work for my example. The error it tells me is

LayeredGraphPlot::vprmtd: Warning: the VertexCoordinateRules option with rule list value is only supported for the SpringElectricalEmbedding method. Switching to the SpringElectricalEmbedding method.

If I try using SpringElectricalEmbedding, it complains about the VertexCoordinateRules.

Is there a simple way around these hurdles? On solution would be to define the positions of As and Bs at some fractional position. But maybe there is some nicer way, where the edges go around the vertices in the way?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

To answer your main question, I generated the coords as specified:

coords = Flatten[
  Array[(Characters@"abc")[[#]] <> ToString@#2 -> {#, #2} &,
    {3, 4}
  ], 1]

{"a1" -> {1, 1}, "a2" -> {1, 2}, "a3" -> {1, 3}, "a4" -> {1, 4}, 
 "b1" -> {2, 1}, "b2" -> {2, 2}, "b3" -> {2, 3}, "b4" -> {2, 4}, 
 "c1" -> {3, 1}, "c2" -> {3, 2}, "c3" -> {3, 3}, "c4" -> {3, 4}}

and passed them to VertexCoordinateRules in a normal GraphPlot:

netList = {"A1" -> "a1", "A1" -> "b1", "b1" -> "A2", "a1" -> "a2", 
   "A2" -> "b2", "A2" -> "c2", "a2" -> "B3", "b2" -> "B3", 
   "c2" -> "c3", "B3" -> "a3", "B3" -> "b3", "a3" -> "A4", 
   "b3" -> "A4", "c3" -> "c4", "A4" -> "a4", "A4" -> "b4"};
GraphPlot[netList,
  VertexLabeling -> True,
  VertexCoordinateRules -> coords,
  DirectedEdges -> True
]

share|improve this answer
2  
Nice!! (+1) for the vertex labels piece you can use something like VertexRenderingFunction -> ({Yellow, EdgeForm[Gray], Rectangle[# - {.2, .2}, {.2, .2} + #], Black, Text[If[LowerCaseQ[StringTake[#2, 1]], "", #2], #1]} &) –  kguler Apr 13 at 22:47
1  
Wonderful solution, much easier than I expected it to become. Thanks alot! :) –  NicoDean Apr 14 at 6:05
    
@kguler: Thanks, I didnt know one can use If in the VertexReneringFunction. This also works for the other arguments - especially for the first one, so it's pretty useful. Thank you! –  NicoDean Apr 14 at 6:32
1  
I used this method for a different network, and observed a problem. I've explained it in detail in Update2 above. –  NicoDean Apr 14 at 7:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.