📜 ⬆️ ⬇️

Visualization of static and dynamic networks on R, part 6

In the first part :

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

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

In the fourth part: network placement.
')
In the fifth part: the emphasis on the properties of the network, vertices, edges, paths.

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

Interactive graphing with tkplot


R and igraph allow you to interactively build network graphics. This can be useful if you need to slightly change the appearance of a small graph. After manual adjustment, you can get the coordinates of the vertices and use them for other constructions.
tkid <- tkplot(net) # tkid -  tkplot,   l <- tkplot.getcoords(tkid) #    tkplot plot(net, layout=l) 



Other ways of representing the network


At this stage it will be useful to remind that there are other ways of representing the network, not limited to the hair ball schedule.

For example, here is the heat map of the network matrix:
 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) ) 



Depending on which properties of the network, its vertices or edges are most important, simple graphics may be more informative than a network map.
 dd <- degree.distribution(net, cumulative=T, mode="all") plot(dd, pch=19, cex=1, col="orange", xlab="Degree", ylab="Cumulative Frequency") 



Pairing networks with igraph


In paired, or dicotyled, graphs, there are two different types of nodes and bonds, but not in each. Our second example with the media is such a network, it explores the links between news sources and their consumers. As will be shown below, this time the network edges are specified in a matrix form. They can be counted in an object graph using graph.incidence . In igraph, dicotyledonous networks have an edge type parameter of 0 for one group of vertices and 1 for another.
 head(nodes2) head(links2) net2 <- graph.incidence(links2) table(E(net2)$type) plot(net2, vertex.label=NA) 



As with monocodes, you can change the network object to include the default visual properties used in the construction. Please note that this time we will also change the shape of the vertices - the media will be squares, and their consumers will be 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) 



Igraph also has a special representation for dicotyledonous networks (although it is not always well suited, perhaps the best solution would be to create your own bipartite representation).
 plot(net2, vertex.label=NA, vertex.size=7, layout=layout.bipartite) 



Sometimes it can be useful to use text labels as vertices:
 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) 



In this example, we will also experiment using pictures as vertices. To do this, you need the png library (if it is not installed, 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) 



By the way, any picture can also be added to the chart. For example, very many graphs of networks can be greatly improved by adding a photo of a puppy that carries a 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) 



 #    -   #     par()$usr 

Good practice is to disable packages when they are no longer needed. Try to remember this, because the packages from the igraph and statnet generate errors if you load them together.
 detach(package:png) detach(package:igraph) 


A small example using the network package


Building with the network package is very similar to igraph , although the syntax differs slightly (a complete set of new parameter names!). This package also uses fewer default settings, obtained by changing the network object, and more explicit parameters in the build function.

Here is a small example using the (already familiar) media network. We begin by converting data into the network format, which is used by the Statnet family of packages (including network , sna , ergm , stergm and others).

As in igraph, you can create a 'network' object from a list of edges, a mate matrix, or a connection. Details can be found by running ?edgeset.constructors . As in the igraph example, we will use a list of edges and data blocks with vertex attributes to create a network object. One feature that is worth paying attention to is the ignore.eval parameter. By default, it is set to TRUE , and this setting tells the network object to ignore edge weights.
 library(network) net3 <- network(links, vertex.attr=nodes, matrix.type="edgelist", loops=F, multiple=F, ignore.eval = F) 

It is also easy to access vertices, edges, and the network matrix:
 net3[,] net3 %n% "net.name" <- "Media Network" #   net3 %v% "media" #   net3 %e% "type" #   

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



Note that, as in igraph, the construction returns the coordinates of the vertices. They can be used in other graphs using the coord parameter.
 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) 


 detach(package:network) 

For a complete list of the parameters available in the network package, execute ?plot.network .

Source: https://habr.com/ru/post/268917/


All Articles