6
$\begingroup$

I am pretty sure that the answer to this question is pretty simple, but after googling for several hours in an attempt to find an answer I feel I have to ask someone for help.

I just made my first diagram in Mathematica, however I need different vertex sizes, since the labels are of different lengths:

e = {{M1, W1} -> {M1, M2, W1, W2}, {M1, W2} -> {M1, M2, W1, W2}, {M1, 
     W3} -> {M1, M3, W3, W4}, {M1, W4} -> {M1, M3, W3, W4}, {M2, 
     W1} -> {M1, M2, W1, W2}, {M2, W1} -> {M2, M3, W1, W2}, {M2, 
     W2} -> {M1, M2, W1, W2}, {M2, W2} -> {M2, M3, W1, W2}, {M2, 
     W3} -> {M2, M3, W3, W4}, {M2, W4} -> {M2, M3, W3, W4}, {M3, 
     W1} -> {M2, M3, W1, W2}, {M3, W1} -> {M3, M4, W1, W2}, {M3, 
     W1} -> {M3, M4, W1, W3}, {M3, W1} -> {M3, M4, W1, W4}, {M3, 
     W2} -> {M2, M3, W1, W2}, {M3, W2} -> {M3, M4, W1, W2}, {M3, 
     W3} -> {M3, M4, W1, W3}, {M3, W3} -> {M2, M3, W3, W4}, {M3, 
     W3} -> {M1, M3, W3, W4}, {M3, W4} -> {M3, M4, W1, W4}, {M3, 
     W4} -> {M2, M3, W3, W4}, {M3, W4} -> {M1, M3, W3, W4}, {M4, 
     W1} -> {M3, M4, W1, W2}, {M4, W1} -> {M3, M4, W1, W3}, {M4, 
     W1} -> {M3, M4, W1, W4}, {M4, W2} -> {M3, M4, W1, W2}, {M4, 
     W3} -> {M3, M4, W1, W3}, {M4, W4} -> {M3, M4, W1, W4}};

Graph[
 e,
 PerformanceGoal -> "Quality",
 DirectedEdges -> True,
 VertexLabels -> Placed["Name", Center],
 VertexSize -> {0.6,
   {M1, M2, W1, W2} -> 0.9, {M1, M3, W3, W4} -> 
    0.9, {M2, M3, W1, W2} -> 0.9, {M2, M3, W3, W4} -> 
    0.9, {M3, M4, W1, W2} -> 0.9, {M3, M4, W1, W3} -> 
    0.9, {M3, M4, W1, W4} -> 0.9},
 ImageSize -> 1500,
 BaseStyle -> {"Arrowheads" -> .009}
 ]

The way I solved it here is pretty inelegant, but I simply do not see how I can assign a vertex size to more than one vertex at a time. Optimally I would like to assign a vertex size according to the Length of the label. Alternatively, I'd be happy to assign the VertexSize to a list. In the documentation for VertexSize, it is done the same way as I have done it here.

$\endgroup$
6
$\begingroup$

One possibility is to use a VertexShapeFunction whose size depends on the length of the vertex name:

Graph[
    e,
    PerformanceGoal->"Quality",
    DirectedEdges->True,
    VertexLabels->Placed["Name",Center],
    ImageSize->1500,
    BaseStyle->{"Arrowheads"->.009},
    VertexShapeFunction->Function@{Disk[#, Length[#2]/8.5], Text[#2,#1]}
]

I just used trial and error for the radius.

enter image description here

$\endgroup$
6
$\begingroup$

You can use rules for setting values for the option VertexSize :

Graph[e, VertexSize -> {v_ -> .6, v_ /; Length[v] === 4 -> .9},
 PerformanceGoal -> "Quality", DirectedEdges -> True, 
 VertexLabels -> Placed["Name", Center], 
 ImageSize -> 1500, BaseStyle -> {"Arrowheads" -> .009}]

enter image description here

Alternatively,

Graph[e, VertexSize -> {v_ :> If[Length[v] === 4, .9, .6]},
 PerformanceGoal -> "Quality", DirectedEdges -> True, 
 VertexLabels -> Placed["Name", Center], 
 ImageSize -> 1500, BaseStyle -> {"Arrowheads" -> .009}]

same picture

You might also consider an alternative vertex shape that eliminates the need to set vertex sizes explicitly:

Graph[e, PerformanceGoal -> "Quality", DirectedEdges -> True, 
 VertexShapeFunction -> (Text[Framed[Style[#2, Black], RoundingRadius -> 10, 
      Background -> LightBlue], #] &), 
 EdgeStyle -> Arrowheads[.009], 
 ImageSize -> 1500]

enter image description here

$\endgroup$
5
$\begingroup$
Clear["Global`*"]

e = {{M1, W1} -> {M1, M2, W1, W2}, {M1, W2} -> {M1, M2, W1, W2}, {M1, 
     W3} -> {M1, M3, W3, W4}, {M1, W4} -> {M1, M3, W3, W4}, {M2, 
     W1} -> {M1, M2, W1, W2}, {M2, W1} -> {M2, M3, W1, W2}, {M2, 
     W2} -> {M1, M2, W1, W2}, {M2, W2} -> {M2, M3, W1, W2}, {M2, 
     W3} -> {M2, M3, W3, W4}, {M2, W4} -> {M2, M3, W3, W4}, {M3, 
     W1} -> {M2, M3, W1, W2}, {M3, W1} -> {M3, M4, W1, W2}, {M3, 
     W1} -> {M3, M4, W1, W3}, {M3, W1} -> {M3, M4, W1, W4}, {M3, 
     W2} -> {M2, M3, W1, W2}, {M3, W2} -> {M3, M4, W1, W2}, {M3, 
     W3} -> {M3, M4, W1, W3}, {M3, W3} -> {M2, M3, W3, W4}, {M3, 
     W3} -> {M1, M3, W3, W4}, {M3, W4} -> {M3, M4, W1, W4}, {M3, 
     W4} -> {M2, M3, W3, W4}, {M3, W4} -> {M1, M3, W3, W4}, {M4, 
     W1} -> {M3, M4, W1, W2}, {M4, W1} -> {M3, M4, W1, W3}, {M4, 
     W1} -> {M3, M4, W1, W4}, {M4, W2} -> {M3, M4, W1, W2}, {M4, 
     W3} -> {M3, M4, W1, W3}, {M4, W4} -> {M3, M4, W1, W4}};

vLong = Cases[e, _List?(Length[#] > 2 &), {2}] // Union;

Graph[e, PerformanceGoal -> "Quality",
 DirectedEdges -> True,
 VertexLabels -> Placed["Name", Center],
 VertexSize -> Flatten[{0.6, Thread[vLong -> 0.9]}],
 ImageSize -> 1500,
 BaseStyle -> {"Arrowheads" -> 0.009}]
$\endgroup$
  • $\begingroup$ Thank you very much! I have one last question I learned how I can change the filling of the vertices and I also learnt how I can change the border color of the vertices. But somehow I can't change both. The problem seems to be similar: The former would be changed by VertexStyle -> White the latter by VertexStyle -> {EdgeForm[White] but combining both is impossible. I'd like to see if that would not allow me to make the diagram more compact. $\endgroup$ – user3201 Nov 19 at 19:58
  • $\begingroup$ Use either VertexStyle -> Directive[White, EdgeForm[White]] or VertexStyle -> {{White, EdgeForm[White]}} $\endgroup$ – Bob Hanlon Nov 19 at 21:05
  • $\begingroup$ okay sorry! I thought I had tried that but it didn't work now I copy pasted it from your response and it works like a charm thank you so much! $\endgroup$ – user3201 Nov 19 at 21:10

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.