3
$\begingroup$

I am trying to draw a graph that looks like below,

enter image description here

This the code I have so far,

DeBruijnGraph[4, 2, EdgeShapeFunction -> "Line", GraphLayout -> Automatic, 
VertexLabels -> Table[i -> {RandomChoice[{A, C, T, G}], 
RandomChoice[{A, C, T, G}]}, {i, 20}], VertexSize -> 
RandomReal[0, 1],   VertexStyle -> Blue] 

But I don't get a similiar graph above. Can someone give me a suggestion to graph it? Thanks

$\endgroup$
9
$\begingroup$

The following changes to your code will produce the desired graph:

  1. Change GraphLayout to "SpringElectricalEmbedding" instead of Automatic
  2. Instead of using a list of symbols like {A, C, G, T}, use strings and concatenate them for the labels.
  3. VertexSize with a single value uses the same size for all vertices. Instead, use VertexShapeFunction with a random disk radius to achieve the same effect.

The resulting code:

DeBruijnGraph[4, 2, 
    EdgeShapeFunction -> "Line", 
    GraphLayout -> "SpringElectricalEmbedding", 
    VertexLabels -> Table[i -> Row@RandomChoice[{"A", "C", "T", "G"}, 2], {i, 20}], 
    VertexShapeFunction -> ({Opacity@0.5, Blue, Disk[#, RandomReal[{0.05, 0.2}]]}&)
]


If you want to get perfect circles for the self-loops, it might be easier to use the older GraphPlot instead of the newer Graph-style functions.

adjMatrix = AdjacencyMatrix@DeBruijnGraph[4, 2];
With[{vertexLabels = Table[i -> Row@RandomChoice[{"A", "C", "T", "G"}, 2], {i, 20}]},
    GraphPlot[adjMatrix,
        SelfLoopStyle -> All,
        Method -> "SpringElectricalEmbedding",
        MultiedgeStyle -> 0.2,
        PlotStyle -> Lighter@Orange,
        VertexRenderingFunction -> Function[{center, label}, 
            With[{radius = RandomReal[{0.05, 0.2}]}, 
                {Opacity@0.5, FaceForm@Blue, EdgeForm@Black, Disk[center, radius], 
                 Black, Text[label /. vertexLabels, center + Min[1.75 radius, 0.25] {Cos[Pi/4], Sin[Pi/4]}]}
            ]
        ],
        VertexLabeling -> True,
        PlotRangePadding -> None
    ]
]

$\endgroup$
  • $\begingroup$ Can we make the self-loops a perfect circles? $\endgroup$ – Silvia Nov 16 '15 at 4:51
  • $\begingroup$ @Silvia Good catch! I couldn't get it to work with EdgeShapeFunction but I knew it was possible using the older GraphPlot, so I've updated my answer with a perfect-circle self-loop version :) $\endgroup$ – rm -rf Nov 16 '15 at 5:40
  • $\begingroup$ Then +1 :) (Totally forgot GraphPlot!) $\endgroup$ – Silvia Nov 16 '15 at 5:46
  • $\begingroup$ for a perfect circle loop, set EdgeShapeFunction -> (Line[#] &).. $\endgroup$ – halmir Nov 16 '15 at 14:15
  • $\begingroup$ @halmir Hmm... It doesn't show up as a perfect loop here :( 10.3 on OS X El Capitan $\endgroup$ – rm -rf Nov 16 '15 at 15:54

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.