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.

How can I label particular vertices in GraphPlot? And have the arrows space out accordingly, for the width of the label?

I have Mathematica 7, but I think this question applies to all versions.

Mathematica Graph Plot

Edit: I posted the above graph so you can see label overlap might kill whatever information the graph is displaying.

For simplicity, lets do a mock graph:

Generate the Data:

data = #[[1]] -> #[[2]] & /@ Partition[RandomInteger[10, 100], 2]

Plot the Graph:

GraphPlot[data, EdgeRenderingFunction -> ({Red, {Arrowheads[Small], Arrow[#1, .07]}} &)]

Gives something like:

Random GraphPlot

If I wanted to explicitly label "1" and "5", this is where I'm completely lost:

I'm happy with the ugly yellow boxes for now:

GraphPlot[data,EdgeRenderingFunction->({Red,{Arrowheads[Small],Arrow[#1,.07]}}&), VertexLabeling->True]

yellow box, on all labeling

But you can see it can be problematic if I labeled every point in the first graph. I wish the VertexLabelingFunction help pages was a bit more thorough, all it seem it do is some sort of rule based, rather than explicit labels. I'm thinking it'll be some VertexLabelingFunction -> (If[#1 == "1" || #1 == "5", Text[#1]])& But that always gives me an error.

share|improve this question
3  
Are you sure you want a mathematica solution and not one in ruby? ;) –  yoda Feb 10 '12 at 1:49
    
Write custom vertex and edge rendering functions and pass them to GraphPlot. There are significant differences in the arguments to the VertexRenderingFunction and GraphRenderingFunction between Mathematica 7 and 8. Do you prefer an answer for 7 or for 8? –  cah Feb 10 '12 at 3:37
4  
Please recheck your understanding of "sexy" with a good doctor :). –  belisarius Feb 10 '12 at 15:15
1  
Perhaps you should include the code for a simple graph as an example. The GraphPlot above cannot be used by answerers and is therefore less useful as an illustration. By the way, a considerable proportion of the Mathematica community that stalked this tag has moved to the dedicated Mathematica site at Mathematica.stackexchange.com. You might want to consider moving as well. –  Sjoerd C. de Vries Feb 10 '12 at 22:57
1  
Have you tried VertexRenderingFunction? –  Sjoerd C. de Vries Feb 10 '12 at 23:07
show 2 more comments

migrated from stackoverflow.com Feb 11 '12 at 15:54

This question came from our site for professional and enthusiast programmers.

1 Answer

You could do something like this. It's an adaptation of the first example in the documentation for VertexLabelingFunction where I used an If statement to determine whether a vertex should be labeled or not. The function offset is just a helper function to determine by what amount the arrows should be shortened based on whether they end or start at a labeled vertex or not and lblLst is the list of vertices you want to label:

lblLst = {1, 5, 10};

offset[lblLst_, edge : {e1_, e2_}] := If[MemberQ[lblLst, #], .13, .07] & /@ edge

GraphPlot[data, 
 EdgeRenderingFunction ->
  ({Red, Arrowheads[Small], Arrow[#1, offset[lblLst, #2]]} &),

 VertexRenderingFunction -> (If[MemberQ[lblLst, #2],
     {White, EdgeForm[Black], Disk[#, .1], Black, Text[#2, #1]},
     {Blue, Point[#1]}] &)]

Mathematica graphics

share|improve this answer
    
try this data(juggernautmotorsports.ca/work/code/mathematica/data) and you'll see what I mean. With lblLst = {"jshamess", "BuzzBuzzHome", "Urban_Toronto", "RealNet_Canada", "Urbanation", "BrianPersaud"}. To load into file, DL the data from link, then GraphPlot[<< "~/data", EdgeRe... if data is in your home dir, or <<"~/Downloads/data" if it's in your Downloads folder. Thanks so much though, it's a huge leap forward. –  Mr. Demetrius Michael Feb 11 '12 at 19: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.