3
$\begingroup$

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!

$\endgroup$
5
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Excellent! Thank you. $\endgroup$ – PeterR Dec 13 '12 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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