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.

One can make graphs

GraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 5, 5 -> 1, 1 -> 1, 5 -> 5}]

and one can generalize the input such that one labels edges

 GraphPlot[{1 -> 2, {2 -> 3, "my favorite edge"}, 3 -> 4, 4 -> 5,  5 -> 1, 1 -> 1, 5 -> 5}] 

Now in the Mathematica help for GraphPlot, under Options and then EdgeRenderingFunction one finds the example

GraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 5, 5 -> 1, 1 -> 1, 5 -> 5}, 
EdgeRenderingFunction -> (If [
 First[#2] == Last[#2], {Red, Arrow[#1]}, {GrayLevel[0.5], 
  Arrow[#1]}] &)]

I want to use this to change the color of edges according to their naming, i.e. if the edge says "my favorite edge", then that edge should be green.

Problem is that once I use EdgeRenderingFunction,

GraphPlot[{1 -> 2, {2 -> 3, "my favorite edge"}, 3 -> 4, 4 -> 5, 5 -> 1, 1 -> 1, 5 -> 5}, 
EdgeRenderingFunction -> (If [
 First[#2] == Last[#2], {Red, Arrow[#1]}, {GrayLevel[0.5], 
  Arrow[#1]}] &)]

the data beyond "2 -> 3" gets ignored. How to solve this problem?

(I don't really have to use EdgeRenderingFunction to change the color, I just want more than just the names to point out some special edges.)

share|improve this question

2 Answers 2

up vote 3 down vote accepted

They're not ignored — the edge label is given as the third argument to EdgeRenderingFunction. You can do something like the following:

color := If[# === "my favorite edge", Darker@Green, GrayLevel[0.5]] &
GraphPlot[{1 -> 2, {2 -> 3, "my favorite edge"}, 3 -> 4, 4 -> 5, 5 -> 1, 1 -> 1, 5 -> 5}, 
    EdgeRenderingFunction -> (If[First[#2] == Last[#2], {Red, Arrow[#1]}, {color[#3], Arrow[#1]}] &)]

enter image description here

It might have seemed that it was being ignored because the labels were not displayed. The reason for this is given in "Possible issues" under EdgeLabeling:

An explicit setting for EdgeRenderingFunction overrides the setting for EdgeLabeling

So in order to get the edge labels, you'll have to incorporate that also in your EdgeRenderingFunction. Here's an example:

label := If[#1 =!= None, Text[#1, #2]] &
GraphPlot[{1 -> 2, {2 -> 3, "my favorite edge"}, 3 -> 4, 4 -> 5, 5 -> 1, 1 -> 1, 5 -> 5}, 
    EdgeRenderingFunction -> (If[First[#2] == Last[#2], {Red, Arrow[#1]}, {GrayLevel[0.5], 
        Arrow[#1], label[#3, Mean@#1]}] &)]

enter image description here

share|improve this answer
    
Thanks. I really wanted both at the same time, here is what I end up with: EdgeRenderingFunction -> (If[#3 == "my favorite edge", {Green, Arrow[#1], Text[Style[#3, Small, Black], Mean[#1]]}, {Gray, Arrow[#1]}] &). –  NikolajK Jul 14 '12 at 17:27

Perhaps:

GraphPlot[{1 -> 2, {2 -> 3, "my favorite edge"}, 3 -> 4, 4 -> 5, 5 -> 1, 1 -> 1, 5 -> 5}, 
 EdgeRenderingFunction -> 
  Function[{x, y,  z}, (If[z == "my favorite edge", {Red, Arrow[x]}, , 
                                                    {GrayLevel[0.5], Arrow[x]}])]]

enter image description here

torn[] credit

share|improve this answer

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.