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.

I have two graphs

g1 = Graph[{"1", "2", "3"}, {{2, 1}, {3, 1}},VertexLabels -> {"1" -> Placed["One", Tooltip], "2" -> Placed["Two", Tooltip], "3" -> Placed["Three", Tooltip]}]

and

g2 = Graph[{"5", "2", "3"}, {{2, 1}, {3, 1}}, VertexLabels -> {"5" -> Placed["Five", Tooltip], "2" -> Placed["Two", Tooltip], "3" -> Placed["Three", Tooltip]}]

Each of which displays the appropriate label when doing a mouseover.

Then I do

GraphUnion[g1,g2]

and get the desired graph, but the tooltips for the vertex names are gone. If I do

GraphUnion[g1,g2,Options[g1]] 

then as expected I get the tooltips for the vertices of g1. But how can I get the tooltips for ALL the vertices (i.e. vertices for both graphs)?

Thanks much!

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

You can extract the VertexLabels from both graphs and join them together, there might be a better way but this works

g1 = Graph[{"1", "2", "3"}, {{2, 1}, {3, 1}},
  VertexLabels -> {"1" -> Placed["One", Tooltip],
                   "2" -> Placed["Two", Tooltip],
                   "3" -> Placed["Three", Tooltip]}];
g2 = Graph[{"5", "2", "3"}, {{2, 1}, {3, 1}},
  VertexLabels -> {"5" -> Placed["Five", Tooltip],
                   "2" -> Placed["Two", Tooltip],
                   "3" -> Placed["Three", Tooltip]}];
GraphUnion[g1, g2, 
 VertexLabels -> Flatten[VertexLabels /. {Options[g1], Options[g2]}]]

Look at:

Flatten[VertexLabels/.{Options[g1],Options[g2]}]
(* {2->Placed[Two,Tooltip],1->Placed[One,Tooltip],
   3->Placed[Three,Tooltip],2->Placed[Two,Tooltip],
   5->Placed[Five,Tooltip],3->Placed[Three,Tooltip]} *)

And you see it has all the options, the repeated elements could be removed by using Union

share|improve this answer
    
Excellent! Thank you. –  PeterR Dec 13 '12 at 16:20
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.