4
$\begingroup$

I want to color a subset of the edges of my graph with a different color. My graph currently has a large number of edges, so the following is just a toy example

s = {1->2,1->3}
g = {1->2, 1->3,3->4,4->1}
GraphPlot[g,VertexLabeling->True]

enter image description here

Here s is a subset of g I would like to tell GraphPlot to plot the edges in s with a different color, and make the edges of s directed.

How to do this?


EDIT:

The closest I have coming after looking at some answer to another question on this site is

s = {{1,2},{1,3}}
g = {1->2, 1->3,3->4,4->1}
GraphPlot[g,VertexLabeling->True,EdgeRenderingFunction -> (If[
     Intersection[s, {#2}] != {}, {Red ,Arrow[#1, .1]}, 
                                  {Blue,Arrow[#1, .1]}] &)]

enter image description here

However, since I want the complement of the set s in g to be represented as blue segment (and not an arrow) I am not sure what I should write in place of Arrow inside {Blue,Arrow[#1, .1]} above.

$\endgroup$
  • $\begingroup$ THere are several examples in the documentation for VertexRenderingFunction. The help file will show you how to apply it. $\endgroup$ – bill s Jun 5 '13 at 19:41
  • 1
    $\begingroup$ Something like HighlightGraph[Graph[{1 -> 2, 1 -> 3, 3 -> 4, 4 -> 1}], {1 -> 2, 1 -> 3}]? $\endgroup$ – J. M. will be back soon Jun 5 '13 at 19:43
  • $\begingroup$ @J. M. Yes almost, like that.However, please see edit. $\endgroup$ – smilingbuddha Jun 5 '13 at 19:51
  • 1
    $\begingroup$ s = {{1, 2}, {1, 3}} g = {1 -> 2, 1 -> 3, 3 -> 4, 4 -> 1} GraphPlot[g, VertexLabeling -> True, EdgeRenderingFunction -> (If[ Intersection[s, {#2}] != {}, {Red, Line[#1]}, {Blue, Arrow[#1, .1]}] &)] $\endgroup$ – Dr. belisarius Jun 5 '13 at 21:59
  • 1
    $\begingroup$ @C. E. I posted an answer, let's see if the community thinks it has some value $\endgroup$ – Dr. belisarius Jan 13 '14 at 0:23
6
$\begingroup$
s = {{1, 2}, {1, 3}};
g = {1 -> 2, 1 -> 3, 3 -> 4, 4 -> 1} ; 
GraphPlot[g, VertexLabeling -> True, 
          EdgeRenderingFunction -> (If[s ⋂ {#2} != {}, {Red, Arrow[#1, .1]}, {Blue, Line[#1]}] &)]

Mathematica graphics

$\endgroup$
0
$\begingroup$

HighlightGraph has been mentioned in the comments, and you responded to that comment, but it is still not clear to me when you cannot use this function.

HighlightGraph[
 Graph[g, GraphStyle -> "VintageDiagram", EdgeStyle -> Blue],
 Style[s, Red]
]

enter image description here

  • GraphStyle -> "VintageDiagram" uses the style of GraphPlot with Graph
  • EdgeStyle -> Blue makes the default edge colour blue
  • HighlightGraph[..., Style[s, Red]] highlights the edges listed in s in Red.
$\endgroup$
0
$\begingroup$

Using Graph with options EdgeStyle:

Graph[g, GraphStyle -> "VintageDiagram",
  EdgeStyle -> {_ -> Directive[Arrowheads[0], Blue],
     ## & @@ Thread[s -> Directive[Opacity[1], Thick, Red]]}]

enter image description here

Same result with a combination of options EdgeStyle and EdgeShapeFunction:

EdgeStyle -> Prepend[Thread[s -> Directive[Opacity[1], Thick, Red]], _ -> Blue]

and

EdgeShapeFunction -> Prepend[Thread[s -> "Arrow"], _ -> "Line"]
$\endgroup$

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.