6
$\begingroup$

Can vertices S and R9 (for example) be fill colored (say red or black) in a LayeredGraphPlot?

edges = {S -> R9, S -> R4, S -> R1, R1 -> R2, R2 -> R3, R3 -> R4, 
   R4 -> R5, R5 -> R6, R6 -> T, R9 -> T, T -> S};

LayeredGraphPlot[edges, Left, VertexLabeling -> True, 
 EdgeRenderingFunction -> ({Black, Arrowheads[0.03], 
     Arrow[#1, 0.4]} &), 
 VertexRenderingFunction -> ({White, EdgeForm[Darker[Blue]], 
     Disk[#, .4], Black, Text[#2, #1]} &)]
$\endgroup$
  • $\begingroup$ have you seen this question: mathematica.stackexchange.com/questions/33409/… $\endgroup$ – kickert Jun 27 '19 at 19:51
  • $\begingroup$ Why don't you just use Graph[..., GraphLayout -> "LayeredDigraphEmbedding", VertexStyle -> {1->Red, 2->Green, ...}]? $\endgroup$ – Szabolcs Jun 27 '19 at 22:22
  • $\begingroup$ I prefer the orientaton options LayeredGraphPlot allows such as Left, Top, etc ... the graph layout you show is nice - being parallel edges. Is there a way to shift that orientation as in the LayerGraphPlot? $\endgroup$ – PRG Jun 27 '19 at 22:41
  • $\begingroup$ Look up GraphLayout, same options are available. $\endgroup$ – Szabolcs Jun 27 '19 at 23:32
  • $\begingroup$ yes; good recommendation. I was looking at this in IGraph-M $\endgroup$ – PRG Jun 28 '19 at 15:11
6
$\begingroup$

You can use the option "Orientation" with "LayeredDigraphEmbedding" layout:

Graph[edges, 
 GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left},
 VertexSize -> .6,
 VertexLabelStyle -> 16,
 VertexStyle -> {_ :> Directive[White, EdgeForm[Darker[Blue]]], S -> Red, T -> Green},
 VertexLabels -> Placed["Name", Center],
 EdgeStyle -> Directive[Black, Arrowheads[{{0.03, .8}}]], 
 ImageSize -> Large]

enter image description here

Alternatively, wrap the desired vertices with Style

Graph[VertexList[edges] /.{S -> Style[S, Red], T -> Style[T, Green]}, edges, 
 GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left},
 VertexSize -> .6,
 VertexLabelStyle -> 16,
 VertexStyle ->  Directive[White, EdgeForm[Darker[Blue]]],
 VertexLabels -> Placed["Name", Center],
 EdgeStyle -> Directive[Black, Arrowheads[{{0.03, .8}}]], 
 ImageSize -> Large]

same picture

| improve this answer | |
$\endgroup$
  • $\begingroup$ Fantastic ... thank you! $\endgroup$ – PRG Jun 28 '19 at 15:07
5
$\begingroup$

In the specific case requested, by writing:

edges = {S -> R9, S -> R4, S -> R1, R1 -> R2, R2 -> R3, R3 -> R4, 
         R4 -> R5, R5 -> R6, R6 -> T, R9 -> T, T -> S};

color = ConstantArray[RGBColor[1, 1, 1], 150];
fct[t_] := color[[Total[ToCharacterCode[ToString[t]]]]]
fct[S] = LightRed;
fct[R9] = LightBlue;

LayeredGraphPlot[edges, Left, VertexLabeling -> True, 
                 EdgeRenderingFunction -> ({Black, Arrowheads[0.03],
                                            Arrow[#1, 0.4]} &),                     
                 VertexRenderingFunction -> ({fct[#2], EdgeForm[Darker[Blue]], 
                                              Disk[#, .4], Black, Text[#2,#1]}&)]

I get:

enter image description here

which is what is desired.


Wanting to satisfy the needs requested in the comments, by writing:

edges = {S -> R9, S -> R4, S -> R1, R1 -> R2, R2 -> R3, R3 -> R4, 
         R4 -> R5, R5 -> R6, R6 -> T, R9 -> T, T -> S};

colorules = {{"S", "T", LightRed}, 
             {"R1", "R3", "R5", "R9", LightBlue}, 
             {"R2", "R4", "R6", LightGreen}};
fct[t_] := Last[colorules[[Position[colorules, ToString[t]][[1, 1]]]]]

LayeredGraphPlot[edges, Left, VertexLabeling -> True, 
                 EdgeRenderingFunction -> ({Black, Arrowheads[0.03],
                                            Arrow[#1, 0.4]} &),                     
                 VertexRenderingFunction -> ({fct[#2], EdgeForm[Darker[Blue]], 
                                              Disk[#, .4], Black, Text[#2,#1]}&)]

I get:

enter image description here

which is what is desired.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Very nice! Is there a way to modify fct[t_]? Instead of writing fct[S] and fct[R9} as separate lines, if you want S, R2, R5, R9 all the same color (say light red) then can you write code for fct[S,R2,R5,R9}? $\endgroup$ – PRG Jun 27 '19 at 22:15
  • $\begingroup$ I changed the answer, see if it satisfies you. $\endgroup$ – TeM Jun 28 '19 at 7:33
  • $\begingroup$ TeM: Wonderful; thank you for sharing your creative approaches. I've learned a lot ... prg $\endgroup$ – PRG Jun 28 '19 at 15:09

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.