4
$\begingroup$

I currently plotted a graph with the following code:

AdjacencyGraph[repindhallo2000c, 
 VertexLabels -> Table[i -> Placed[labels[[i]], Center], {i, 1, 56}], 
 VertexSize -> Table[j -> figures[[j]], {j, 1, 56}], ImageSize -> 600,
 EdgeStyle -> {Arrowheads[0.02]}]

It looks as follows:

example1

Now I have two questions which I could not solve so far:

  1. How is it possible to separate the nodes that they not overlap? As I use different sizes, I would like to show that the larger ones have more directed edges. Therefore I need them not to overlap.

  2. As you can see on the bottom of the graph, apparently there are rows in repindhallo2000cwhich sum to zero. Hence there are vertices without any connection. Does anyone know how to delete them? Either directly out the adjacency graph or beforehand in the binary matrix.


Here is a small example:

list = List[{1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1}, {1, 0, 
    1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1}, {1, 0, 1, 0, 1, 1, 0, 
    1, 0, 1, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 
    1, 0, 1, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1}, {1,
     0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 0, 1, 1, 
    0, 1, 0, 1, 1, 0, 1, 0, 1, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 
    0, 1, 0, 1, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 
    1}, {1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1}, {1, 0, 1, 0,
     1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 0, 
    1, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 
    1, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1}, {1, 0, 1,
     1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 
    1, 1, 1, 1, 1, 0, 1, 1}];
head = List[{"a"}, {"b"}, {"c"}, {"d"}, {"e"}, {"f"}, {"g"}, {"h"}, \
{"i"}, {"j"}, {"k"}, {"l"}, {"m"}, {"n"}, {"o"}];
vertexsize = N[(Total[list]/Total[Total[list]])*15];
AdjacencyGraph[list, 
 VertexLabels -> Table[i -> Placed[head[[i]], Center], {i, 1, 15}], 
 VertexSize -> Table[j -> vertexsize[[j]], {j, 1, 15}]]

Example

$\endgroup$
1
  • $\begingroup$ Thanks for update $\endgroup$
    – Kuba
    Commented Sep 13, 2017 at 14:56

2 Answers 2

3
$\begingroup$

How is it possible to separate the nodes that they not overlap?

I do not think that there is an automatic method. I usually scale them manually until there is no overlap. At the same time, I usually increase the size of the graphics (ImageSize). Text labels do not scale with the image, they always stay the same size. I try to increase the image size until a text label fits inside of the smallest node.

It often helps to define the vertex size as {"Nearest", s} (see VertexSize under Details).

Hence there are vertices without any connection. Does anyone know how to delete them?

Pick[VertexList[g], VertexDegree[g], 0]

gives you the isolated vertices. You can VertexDelete them.

You may want VertexInDegree or VertexOutDegree instead of directed graphs.

$\endgroup$
3
$\begingroup$

... vertices without any connection. Does anyone know how to delete them?

You can also use VertexDelete directly, using a pattern in the second argument:

VertexDelete[g, _?(VertexDegree[g, #] === 0 &)]

Or, Subgraph combined with KCoreComponents:

Subgraph[g, KCoreComponents[g, 1]]

For the example:

 g = Graph[Range[7], {1 <->  2, 2 <-> 3,  3 <-> 1, 3<-> 4}, 
      VertexLabels -> Placed["Name", Center], VertexShapeFunction -> None,
      VertexLabelStyle -> Large, ImagePadding -> 5]  

enter image description here

 VertexDelete[g, _?(VertexDegree[g, #] === 0 &)]
 Subgraph[g, KCoreComponents[g, 1], Options[g]]

both give

enter image description here

$\endgroup$
2
  • $\begingroup$ You answer lots of graph questions. Have you tried out IGraph/M? Let me know if you have any comments on it. $\endgroup$
    – Szabolcs
    Commented Sep 20, 2017 at 12:35
  • $\begingroup$ @Szabolcs, I haven't tried IGraphM. I am still on version 9.1 :( $\endgroup$
    – kglr
    Commented Sep 20, 2017 at 12:43

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.