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$
8
  • $\begingroup$ THere are several examples in the documentation for VertexRenderingFunction. The help file will show you how to apply it. $\endgroup$
    – bill s
    Commented Jun 5, 2013 at 19:41
  • 1
    $\begingroup$ Something like HighlightGraph[Graph[{1 -> 2, 1 -> 3, 3 -> 4, 4 -> 1}], {1 -> 2, 1 -> 3}]? $\endgroup$ Commented Jun 5, 2013 at 19:43
  • $\begingroup$ @J. M. Yes almost, like that.However, please see edit. $\endgroup$ Commented Jun 5, 2013 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$ Commented Jun 5, 2013 at 21:59
  • 1
    $\begingroup$ @C. E. I posted an answer, let's see if the community thinks it has some value $\endgroup$ Commented Jan 13, 2014 at 0:23

3 Answers 3

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 and acknowledge you have read our privacy policy.

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