# What is the most convenient way to change options for Graph[] objects?

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.

-

## migrated from stackoverflow.comApr 20 '12 at 13:05

You could use SetProperty. For example

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

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


-

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",

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


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"]


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

-
 Strange, on my version of Mathematica setting VertexSize works fine, e.g. SetProperty[g, {VertexShapeFunction -> "Diamond", VertexSize -> Large}]. – Heike Jan 15 '12 at 21:19 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. – David Carraher Jan 15 '12 at 21:47 @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. – Szabolcs Jan 16 '12 at 7:39 @Szabolcs Yes. I forgot the braces. (Actually, I didn't know that SetProperty required a list for multiple properties. – David Carraher Jan 16 '12 at 12:58