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.

What I'd like to do is to have the same label more than once in the plot. I.e. I do not want the edges to direct toward the first instance of that label! For example:

LayeredGraphPlot[{1 -> 2, 1 -> 3, 2 -> 3, 1 -> 4, 2 -> 4, 3 -> 2}, VertexLabeling -> True]

enter image description here

I want 3 -> 2 (a new 2)!

How can this be achieved?

share|improve this question
    
Welcome to Mathematica.SE! I edited your question a little bit to make it more readable. You can click the edit link above to see how you can do that yourself in the future. Regarding tags, please do not create new tags unless absolutely necessary. Try to find the best matching existing ones. –  Szabolcs Jan 20 '13 at 19:28
add comment

2 Answers

You have to identify the vertices uniquely, so I would call the second instance of 2 by the name 5 instead. But you can label them differently using this:

Clear[label];
label[5] = 2;

LayeredGraphPlot[{1 -> 2, 1 -> 3, 2 -> 3, 1 -> 4, 2 -> 4, 3 -> 5}, 
 VertexLabeling -> True, 
 VertexRenderingFunction -> 
  Function[{p, l}, 
   Text[Framed[If[ValueQ[label[l]], label[l], l], 
     Background -> RGBColor[1, 1, 0.8`], 
     FrameStyle -> RGBColor[0.94`, 0.85`, 0.36`]], p]]]

graph

The VertexRenderingFunction option contains the same style as the default vertex labels, but instead of the vertex number supplied to it as the second argument l, it outputs the value of label[l]. The latter can be defined for any vertex number that you want to appear with a different label. If label[x] isn't defined, then ValueQ returns False and the label is displayed in the default form as x.

share|improve this answer
1  
How about label[n_] := n; label[5] = 2 for simpler code? –  Szabolcs Jan 20 '13 at 19:29
    
@Szabolcs Sure, that works too. My rationale was to move the "default" state completely into the rendering function. –  Jens Jan 20 '13 at 19:33
add comment

Latest graph functionality works as well. You could define the graph structure you need :

g = Graph[{1 -> 2, 1 -> 3, 2 -> 3, 1 -> 4, 2 -> 4, 3 -> 5}, 
   GraphLayout -> "LayeredDigraphEmbedding", VertexLabels -> "Name"];

and then relabel the part you need to be different:

SetProperty[{g, 5}, VertexLabels -> 2]

enter image description here

share|improve this answer
    
However, this doesn't return a Graphics object as does LayeredGraphPlot. And of course the styling options are different for Graph, so it will take a bit more work to make this a drop-in replacement. –  Jens Jan 20 '13 at 22:39
    
@Jens I do not think he cared for some specific styling (besides layout which is reproduced). I think the fact that this is a Graph, not Graphics is an advantage. Aside all computational advantages - you can turn Graph into Graphics via right click menu, but you cannot do the opposite. –  Vitaliy Kaurov Jan 21 '13 at 0:00
add comment

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.