Re: EdgeRenderingFunction to produce edge labels in GraphPlot
- To: mathgroup at smc.vnet.net
- Subject: [mg87559] Re: [mg87549] EdgeRenderingFunction to produce edge labels in GraphPlot
- From: Carl Woll <carlw at wolfram.com>
- Date: Sun, 13 Apr 2008 03:30:33 -0400 (EDT)
- References: <200804121103.HAA00303@smc.vnet.net>
Murray Eisenberg wrote:
>I'm having trouble fathoming from the documentation how
>EdgeRenderingFunction works. In a display produced by GraphPlot,
>applied to a graph that's a Graph object created with Combinatorica, I
>want to be able to put distinct labels on the edges.
>
>For example, take something very simple:
>
> g = Cycle[3];
>
>I'd like to put labels "a", "b", "c" on the three {1,2}, {2,3}, {3,1},
>respectively.
>
>Here's what I tried finally:
>
> labels = Characters["abc"];
> edgeLabelFunction[{u_, v_}] :=
> labels[[First@Flatten@Position[Edges[Cycle[3]], {u, v}]]]
>
> offset={0.05,0.05}; (* to move labels away from edges *)
>
> GraphPlot[g, Method -> None,
> EdgeRenderingFunction -> ({
> Line[#1],
> Inset[edgeLabelFunction[#2], Mean[#1] + offset]
> ]} &)
> ]
>
>This does not work. How, exactly, do I index into the list of edges...
>
> Edges[g]
> {{1,2},{2,3},{1,3}}
>
>.. so as to select the labels. From the reference page on
>EdgeRenderingFunction, it seems to me that argument #2 is supposed to be
>the length 2 list {u,v} of vertices with which the edge is incident.
>
>
>
It seems that GraphPlot conversions of Combinatorica graphs with edge
labels isn't working. Otherwise you could use the Combinatorica function
SetEdgeLabels on the graph and then display using EdgeLabeling->True.
One possibility is to manipulate the Graph object produced by
Combinatorica into a form suitable for GraphPlot. Here is an example:
g = SetEdgeLabels[Cycle[3], {a, b, c}];
g = Replace[First@g, {{a_, b_}, opts___} :> (If[MatchQ[#, EdgeLabel], a
-> b, {a -> b, #}] &[EdgeLabel /. {opts}]), {1}];
GraphPlot[g, EdgeLabeling -> True]
The alternative as you tried to do, is to add the edge labels using
EdgeRenderingFunction
First, here's a close facsimile to the default EdgeRenderingFunction:
EdgeRenderingFunction -> ({
RGBColor[0.5, 0, 0],
Arrowheads[{{0.5, 0.5, Graphics[{Black, Inset[Style[#3], {0, 0},
ImageScaled[{0.5, 0.5}], Automatic, None, Background -> White]}]},
{0.03, 0.8}}],
Arrow[#1]
}&)
Now, it's best if the Combinatorica graph is directed (otherwise
GraphPlot will try to label it in both directions). So,
g = Cycle[3, Type -> Directed];
labels = Characters["abc"];
edgeLabelFunction[{u_, v_}] :=
labels[[First@Flatten@Position[Edges[Cycle[3, Type -> Directed]], {u, v}]]]
GraphPlot[g, EdgeRenderingFunction -> ({RGBColor[0.5, 0, 0],
Arrowheads[{{0.5, 0.5,
Graphics[{Black, Inset[Style[edgeLabelFunction[#2]], {0, 0},
ImageScaled[{0.5, 0.5}], Automatic, None,
Background -> White]}]}, {0.03, 0.8}}], Arrow[#1]} &)]
Carl Woll
Wolfram Research
- References:
- EdgeRenderingFunction to produce edge labels in GraphPlot
- From: Murray Eisenberg <murray@math.umass.edu>
- EdgeRenderingFunction to produce edge labels in GraphPlot