Developers Club geek daily blog

1 year, 6 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 the fourth part: placements of network.

In the fifth part: emphasis of properties of network, tops, edges, ways.

In this part: interactive visualization of networks, other ways of representation of network.

Interactive creation of diagrams with tkplot


R and igraph allow to build diagrams of networks online. It can be useful if it is necessary to change type of the small graph slightly. After manual adjustment it is possible to receive coordinates of tops and to use them for other constructions.
tkid <- tkplot(net) # tkid - идентификатор tkplot, который откроется
l <- tkplot.getcoords(tkid) # получаем координаты из tkplot
plot(net, layout=l)

Visualization of static and dynamic networks on R, part 6

Other ways of representation of network


At this stage it will be useful to remind that there are also other ways of representation of network which are not limited to the diagram - hair sphere.

For example, here thermal card of matrix of network:
netm <- get.adjacency(net, attr="weight", sparse=F)
colnames(netm) <- V(net)$media
rownames(netm) <- V(net)$media

palf <- colorRampPalette(c("gold", "dark orange")) 
heatmap(netm[,17:1], Rowv = NA, Colv = NA, col = palf(100), 
        scale="none", margins=c(10,10) )

Visualization of static and dynamic networks on R, part 6

Depending on what properties of network, its tops or edges are most important, simple diagrams can appear more informative, than the card of network.
dd <- degree.distribution(net, cumulative=T, mode="all")
plot(dd, pch=19, cex=1, col="orange", xlab="Degree", ylab="Cumulative Frequency")

Visualization of static and dynamic networks on R, part 6

Creation of pair networks with igraph


At steam rooms, or bichromatic, graphs two different types of nodes and communications, but not in everyone meet. Our second example with mass media — such network, in it communications between sources of news and their consumers are investigated. As it will be shown below, this time edges of network are set in matrix look. They can be considered in object columns, using graph.incidence. In igraph bichromatic networks have edge parameter type, equal 0 for one group of tops and 1 for another.
head(nodes2)
head(links2)

net2 <- graph.incidence(links2)
table(E(net2)$type)

plot(net2, vertex.label=NA)

Visualization of static and dynamic networks on R, part 6

As well as with monocotyledonous networks, it is possible to change object network that it included the visual properties used by default at creation. Pay attention, this time we will also change form of tops — mass media will be small squares, and their consumers — circles.
V(net2)$color <- c("steel blue", "orange")[V(net2)$type+1]
V(net2)$shape <- c("square", "circle")[V(net2)$type+1]
V(net2)$label <- ""
V(net2)$label[V(net2)$type==F] <- nodes2$media[V(net2)$type==F] 
V(net2)$label.cex=.4
V(net2)$label.font=2

plot(net2, vertex.label.color="white", vertex.size=(2-V(net2)$type)*8) 

Visualization of static and dynamic networks on R, part 6

igraph also have special representation for bichromatic networks (though it not always well approaches, perhaps, the best solution will be to create own bichromatic representation).
plot(net2, vertex.label=NA, vertex.size=7, layout=layout.bipartite) 

Visualization of static and dynamic networks on R, part 6

It can be sometimes useful to use text tags as tops:
plot(net2, vertex.shape="none", vertex.label=nodes2$media,
     vertex.label.color=V(net2)$color, vertex.label.font=2, 
     vertex.label.cex=.6, edge.color="gray70",  edge.width=2)

Visualization of static and dynamic networks on R, part 6

In this example we will also experiment with use of pictures as tops. For this purpose the library is required png (if it is not set, use install.packages("png")).
# install.packages("png")
library(png)
 
img.1 <- readPNG("./images/news.png")
img.2 <- readPNG("./images/user.png")

V(net2)$raster <- list(img.1, img.2)[V(net2)$type+1]

plot(net2, vertex.shape="raster", vertex.label=NA,
     vertex.size=16, vertex.size2=16, edge.width=2)

Visualization of static and dynamic networks on R, part 6

By the way, on the diagram too it is possible to add any picture. For example, very many columns of networks can be improved strongly, having added photo of puppy who bears basket with kittens.
l <- layout.auto(net2, ymin=-1.5, ymax=1.5, xmin=-1.5, xmax=1.5)

plot(net2, vertex.shape="raster", vertex.label=NA,
     vertex.size=16, vertex.size2=16, edge.width=2, layout=l)

img.3 <- readPNG("./images/puppy.png")
rasterImage(img.3,  xleft=-1.7, xright=0, ybottom=-1.2, ytop=0)

Visualization of static and dynamic networks on R, part 6

# Числа после картинки - ее координаты
# Границы построения заданы в par()$usr

Good practice — to disconnect packets when they are not necessary any more. Try to remember it, because packets from set igraph and statnet generate errors if to load them together.
detach(package:png) 
detach(package:igraph)


Small example with use of packet of network


Creation with packet network very probably on igraph, though syntax also differs a little (complete set of new names of parameters!). This packet also uses less default settings received by change of object network, and more explicit parameters as creation.

Here small example with use (already familiar) network with mass media. Let's begin with converting of data in format network, which is used by family of packets of Statnet (including network, sna, ergm, stergm and others).

As well as in igraph, it is possible to create object of 'network' from the list of edges, matrixes of interface or connectivity. Particulars can be received, having executed ?edgeset.constructors. As well as in example with igraph, we will use the list of edges and data units with attributes of tops to create object network. One feature to which it is worth paying attention — parameter ignore.eval. By default it is set in TRUE, and this setup specifies to object network to ignore the weight of edges.
library(network)

net3 <- network(links,  vertex.attr=nodes, matrix.type="edgelist", 
                loops=F, multiple=F, ignore.eval = F)

Here it is also easy to get access to tops, edges and matrix of network:
net3[,]
net3 %n% "net.name" <- "Media Network" #  параметр сети
net3 %v% "media"    # параметр вершины
net3 %e% "type"     # параметр вершины

Let's construct our network of mass media again:
net3 %v% "col" <- c("gray70", "tomato", "gold")[net3 %v% "media.type"]
plot(net3, vertex.cex=(net3 %v% "audience.size")/7, vertex.col="col")

Visualization of static and dynamic networks on R, part 6

Pay attention that as well as in igraph, creation returns coordinates of tops. They can be used in other diagrams by means of parameter coord.
l <- plot(net3, vertex.cex=(net3 %v% "audience.size")/7, vertex.col="col")
plot(net3, vertex.cex=(net3 %v% "audience.size")/7, vertex.col="col", coord=l)

Visualization of static and dynamic networks on R, part 6
detach(package:network)

To receive the complete list of the parameters available in packet network, execute ?plot.network.

This article is a translation of the original post at habrahabr.ru/post/268917/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: sysmagazine.com@gmail.com.

We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus