5
$\begingroup$

Because my network nodes have real world names more than a few characters long, I'd like to label them with a shortcut, e.g., n1, n2, n3, ..., and put a legend on the side something like this: n1 - fooLongName, n2 - barLongName, n3 - bazLongName.

Any ideas for how to do this?

$\endgroup$
1
  • $\begingroup$ Thanks to kguler for the answer below. Unfortunately I should have been more specific in my question. What I have is a directed network. My fiddling to fit the code structure below with GraphPlot hasn't worked. But I confess to having only tried quick/dirty, not really paying my dues. $\endgroup$
    – oldlab
    Commented Sep 24, 2014 at 0:10

1 Answer 1

7
$\begingroup$
labels = Table[i -> Style[Subscript[v, i], 20], {i, 3}]
names = Table[i -> "longname_" <> "v_" <> ToString@i, {i, 3}];
colors = {1 -> Blue, 2 -> Red, 3 -> Green}; 

legend = SwatchLegend[Last /@ colors, 
      Row[{#[[1]], ": ", #[[2]]}] & /@ Transpose[{labels, names}][[All, All, -1]],
      LegendMarkers -> "Bubble"];

You can use Legended, Epilog or Row to add legends to a Graph:

Legended[Graph[{1 <-> 2, 2 <-> 3,  3 <-> 1},  VertexSize -> 0.1,
                VertexStyle -> colors, VertexLabels -> labels, ImagePadding -> 20], 
         legend]

enter image description here

Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1},
    VertexStyle -> colors, VertexSize -> 0.1, VertexLabels -> labels, 
    ImagePadding -> 20, 
 Epilog -> Inset[legend, {.75, .75}]]

enter image description here

Row[{Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, VertexStyle -> colors, VertexSize -> 0.1, 
            VertexLabels -> labels, ImagePadding -> 20, ImageSize -> 300], 
    legend}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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