# Mathematica: Label specific vertices in GraphPlot

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. 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 = #[] -> #[] & /@ Partition[RandomInteger[10, 100], 2]


Plot the Graph:

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


Gives something like: 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] 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.

## migrated from stackoverflow.comFeb 11 '12 at 15:54

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

• 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
• Please recheck your understanding of "sexy" with a good doctor :). – Dr. belisarius Feb 10 '12 at 15:15
• 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
• Have you tried VertexRenderingFunction? – Sjoerd C. de Vries Feb 10 '12 at 23:07

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]}] &)] • 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
• How can you keep your Tooltip labeling using an Vertex Rendering Functinon? – SumNeuron Sep 2 '16 at 17:22