18
$\begingroup$

What is the most convenient way to change options such as VertexLabels in existing Graph objects? (Version 7 users note: Graph is new in Mathematica 8.)


With graphics, we can use Show for this, not matter what function was originally used to produce the graphic:

g = Plot[Sin[x], {x, 0, 10}]
Show[g, Axes -> False, Frame -> True]

Is there an analogous function for Graphs? Suppose we already have a graph, and now we need to show vertex labels in a different way.

Here's a workaround using HighlightGraph:

g = Graph[{1 -> 2}]
HighlightGraph[g, {}, VertexLabels -> "Name"]

While it works, this is not really what HighlightGraph is meant for.


Please note that Graph objects are atomic before trying to take them apart. Also, I'm looking for the safest and most robust solution. I'm hoping there's a function I overlooked.

$\endgroup$

4 Answers 4

15
$\begingroup$

You could use SetProperty. For example

g = Graph[{1 -> 2, 2 -> 4, 3 -> 4}]

SetProperty[g, VertexLabels -> {"Name", 2 -> "Two"}]

Mathematica graphics Mathematica graphics

$\endgroup$
1
  • $\begingroup$ @Szabolcs, Heike Unfortunately (and unlike Show), SetProperty does not work if there is no second argument, which is a PITA if ypou want to make a wrapper function and call it with no options: Options[plotGraph] = Options@Graph; plotGraph[g_Graph, opts:OptionsPattern[]] := SetProperty[g, opts]; g = Graph[{1 -> 2, 2 -> 3}];plotGraph[g]. So one has to check whether opts is empty or not... $\endgroup$ Commented Nov 25, 2013 at 11:06
15
$\begingroup$

You can use Graph itself much like Show. However, you pass the essential data from the graph as the first argument, rather than the graph itself. (Graph[g,....] will not work.)

g = Graph[{1 -> 8, 1 -> 11, 1 -> 18, 1 -> 19, 1 -> 21, 1 -> 25, 1 -> 26}, 
    VertexLabels -> "Name", 
    ImagePadding -> 10]

Graph[EdgeList[g], VertexShapeFunction -> "Diamond", VertexSize -> Medium]

graphs

Special types of graphs, e.g. TreeGraph, AdjacencyGraph, IncidenceGraph, etc. should work in a similar fashion.


Edit

Heike's solution has the advantage of maintaining the original options of the graph:

SetProperty[g, VertexShapeFunction -> "Diamond"]

graph2

It's nice that the VertexNames are displayed as they were initially in g (in contrast to the second graph shown above).

$\endgroup$
4
  • $\begingroup$ Strange, on my version of Mathematica setting VertexSize works fine, e.g. SetProperty[g, {VertexShapeFunction -> "Diamond", VertexSize -> Large}]. $\endgroup$
    – Heike
    Commented Jan 15, 2012 at 21:19
  • $\begingroup$ When I cut and paste your code, SetProperty[g, {VertexShapeFunction -> "Diamond", VertexSize -> Large}], into Mathematica, it works fine. However, when I type it into my notebook, it does not. Seems like there is some sort of corruption in my notebook. $\endgroup$
    – DavidC
    Commented Jan 15, 2012 at 21:47
  • $\begingroup$ @David, is it possible that you forgot to put the options into a list when typing SetProperty[g, {VertexShapeFunction -> "Diamond", VertexSize -> Large}] manually? I made that mistake when I first typed it. $\endgroup$
    – Szabolcs
    Commented Jan 16, 2012 at 7:39
  • $\begingroup$ @Szabolcs Yes. I forgot the braces. (Actually, I didn't know that SetProperty required a list for multiple properties. $\endgroup$
    – DavidC
    Commented Jan 16, 2012 at 12:58
3
$\begingroup$

Since version 10, the following works:

Graph[g, VertexLabels -> "Name"]

Here g is already a graph object.

This syntax is important because it also allows converting between Graph and Graph3D, both of which are graphs, but the latter is displayed in 3D by default.

$\endgroup$
2
$\begingroup$

I have encountered a few cases where SetProperty is useless, for example one cannot set GraphLayout (as discovered by Szabolcs here) or VertexLabels, and possibly more:

SetProperty[Graph[{1 -> 2}, VertexLabels -> {1 -> "A"}], VertexLabels -> None]

Mathematica graphics

Solution

I offer a more robust solution that explicitly replaces options. It keeps properties, keeps and updates existing options and adds new options. It might not be the fastest when it comes to rebuilding huge graphs, as it has to extract vertices and edges.

Options[showGraph] = Options@Graph;
showGraph[g_] := g;
showGraph[g_Graph, {new___}] := showGraph[g, new];
showGraph[g_Graph, new : OptionsPattern[]] := Module[{old = Options@g, opts},
   opts = DeleteDuplicates[First /@ Join[old, {new}]];
   Graph[VertexList@g, EdgeList@g, Thread[opts -> (opts /. {new} /. old)]]];

Testing

{g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}, ImageSize -> 120, ImagePadding -> 15, 
   VertexSize -> {2 -> Large}, VertexLabels -> {1 -> "A", 2 -> "B", 3 -> "C"},
    GraphStyle -> "DiagramGold", EdgeStyle -> {(1 -> 2) -> Red}, 
   GraphLayout -> "SpiralEmbedding"],
 showGraph[g, EdgeStyle -> {(1 -> 2) -> Green}, VertexLabels -> None, 
  GraphLayout -> "CircularEmbedding"],
 showGraph[g],
 showGraph[g, VertexLabels -> "Name"]
 }

Mathematica graphics

Unfortunately, something like showGraph[Graph[v_, e_, old_]]:=Graph[v, e, ...] won't work, as:

MatchQ[Graph[{1, 2}, {1 -> 2}], _[_, _, ___]]   (* ==> False *)
MatchQ[graph[{1, 2}, {1 -> 2}], _[_, _, ___]]   (* ==> True  *)

One of the million annoying features of the atomistic representation of Graph-s.

$\endgroup$

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