### Developers Club geek daily blog

1 year, 9 months ago
In the first part:
• visualization of networks: what for? how?
• visualization parameters
• best practices — esthetics and productivity
• data formats and preparation
• the description of data sets which are used in examples
• the beginning of work with igraph

In the second part: colors and fonts in diagrams R.

In the third part: parameters of graphs, tops and edges.

In this part: placements of network.

#### Placements of network

Placements of network — it is the algorithms returning coordinates of each top of the graph.
For research of placements we will create a little bigger graph, from 80 tops. We use the barabasi.game function which generates the simple graph beginning with one top and increasing tops and edges depending on the set level of preferable accession (how many still new objects will prefer to establish connection with more popular tops in network).
``````net.bg <- barabasi.game(80)
V(net.bg)\$frame.color <- "white"
V(net.bg)\$color <- "orange"
V(net.bg)\$label <- ""
V(net.bg)\$size <- 10
E(net.bg)\$arrow.mode <- 0
plot(net.bg)
``````

It is possible to set placement as creation:
``````plot(net.bg, layout=layout.random)
``````

Or to calculate coordinates of tops in advance:
``````l <- layout.circle(net.bg)
plot(net.bg, layout=l)
``````

`l` — matrix x-and y-coordinates (N x 2) for N tops of the graph. They can be set easily independently:
``````l <- matrix(c(1:vcount(net.bg), c(1, vcount(net.bg):2)), vcount(net.bg), 2)
plot(net.bg, layout=l)
``````

This placement — only example, it is not too useful. Fortunately, in igraph there is set of the built-in placements, including such:
``````# Случайно расположенные вершины
l <- layout.random(net.bg)
plot(net.bg, layout=l)
``````

``````# Размещение по кругу
l <- layout.circle(net.bg)
plot(net.bg, layout=l)
``````

``````# Размещение в виде 3D-сферы
l <- layout.sphere(net.bg)
plot(net.bg, layout=l)
``````

Fryukhterman-Reyngold — one of the most often used power algorithms of placement.

Power placements assume creation of the beautiful graph with edges of identical length and minimum possible number of intersections. They present the graph in the form of physical system. Tops — electrically the charged particles which are making a start from each other at excessive approach. Edges behave as the springs attracting the connected tops are closer to each other. As a result of top are evenly distributed on diagram area, and placement intuitively in the sense that tops with large number of communications are more closely to each other. The lack of these algorithms consists that they are quite slow, and therefore are seldom used on graphs with quantity of tops more than 1000.

Here some parameters which can be set for this placement: `area` (by default — square of quantity of tops) and `repulserad` (suppression radius for pushing away — area increased by quantity of tops). Both parameters influence diagram arrangement — change them, will not remain are happy with result yet.

It is also possible to use the parameter of "weight" which increases attracting forces between the tops connected by heavier edges.

You will notice that placement is not strictly defined — different starts lead to a little different configurations. Placement saving `l` allows to receive the same result many times that can be useful if it is necessary to construct change of the graph in time or the different relations — and it is necessary to leave tops on the same places in several diagrams.

Placement `fruchterman.reingold.grid` similarly `fruchterman.reingold`, but quicker.
``````l <- layout.fruchterman.reingold(net.bg, repulserad=vcount(net.bg)^3,
area=vcount(net.bg)^2.4)
par(mfrow=c(1,2),  mar=c(0,0,0,0)) # построить два графика - 1 строка, 2 столбца
plot(net.bg, layout=layout.fruchterman.reingold)
plot(net.bg, layout=l)
``````

``````dev.off() # выключить графику, чтобы очистить конфигурацию.
``````

Other popular algorithm of power placement showing good results for connected graphs — Kamada-Kavai. As well as Fryukhterman-Reyngold, its purpose — to minimize energy in system of springs. Igraph also contains placement on the basis of springs which is called `layout.spring()`.
``````l <- layout.kamada.kawai(net.bg)
plot(net.bg, layout=l)

l <- layout.spring(net.bg, mass=.5)
plot(net.bg, layout=l)
``````

The algorithm of LGL (Large Graph Layout — placement of big graphs) is intended for big connected graphs. It also allows to set root — top which will be located in the center of placement.
``````plot(net.bg, layout=layout.lgl)
``````

By default coordinates of diagrams are scaled to interval [-1,1] both on x, and on at. It can be changed by means of parameter `rescale=FALSE` and to scale the diagram manually, multiplying coordinates by number. Function `layout.norm` allows to normalize the diagram on desirable borders.
``````l <- layout.fruchterman.reingold(net.bg)
l <- layout.norm(l, ymin=-1, ymax=1, xmin=-1, xmax=1)

par(mfrow=c(2,2), mar=c(0,0,0,0))
plot(net.bg, rescale=F, layout=l*0.4)
plot(net.bg, rescale=F, layout=l*0.6)
plot(net.bg, rescale=F, layout=l*0.8)
plot(net.bg, rescale=F, layout=l*1.0)
``````

``````dev.off()
``````

By default igraph uses placement `layout.auto`. It automatically selects necessary algorithm of placement on the basis of properties of the graph (the size and connectivity).

Let's look at the placements available in igraph:
``````layouts <- grep("^layout\\.", ls("package:igraph"), value=TRUE)
# Убрать размещения, не подходящие для нашего графа.
layouts <- layouts[!grepl("bipartite|merge|norm|sugiyama", layouts)]

par(mfrow=c(3,3))

for (layout in layouts) {
print(layout)
l <- do.call(layout, list(net))
plot(net, edge.arrow.mode=0, layout=l, main=layout) }

dev.off()
``````