# How to plot a network (package network) – Tip 2

**ProbaPerception**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

As you have certainly seen now, I like working on artificial neural networks. I have written a few posts about models with neural networks (Models to generate networks, Want to win to Guess Who and Study of spatial segregation).

Unfortunately, I missed so far a nice and pleasant aspect of networks : its graphical approach. Indeed, plots of neural networks are often really nice and really useful to understand the network.

Sometimes such a graph can point out some characteristics of the network. For example, we see in the graph below that there is one “central” node linked with many other nodes. It would not be that obvious if we were looking at a simple print() of a network matrix!

So if you like this representation, let me introduce you to the package network.

Let’s first download the package and use a home-made function to generate networks randomly (if you want to see more details about this function, see here):

install.packages(‘network’)

library(network)

generateBA = function(n = 100, n0 = 2){

mat = matrix(0, nrow= n, ncol = n)

for(i in 1:n0){

for(j in 1:n0){

if(i != j){

mat[i,j] = 1

mat[j,i] = 1

}

}

}

for(i in n0:n){

list = c()

for(k in 1:(i-1)){

list = c(list, sum(mat[,k]))

}

link = sample(c(1:(i-1)), size = 1, prob = list)

mat[link,i] = 1

mat[i,link] = 1

}

return(mat)

}

artificialNet= generateBA(200)

To create an object network from a matrix, the package uses the function network():

a = network(artificialNet, directed = FALSE)

network.size(a)

**+**: An (i; j) edge is created in the return graph for every (i; j) edge in each of the input graphs.

matched by an (i; j) edge in the second input; if the second input has more (i; j) edges than

the first, no (i; j) edges are created in the return graph.

**– :**An (i; j) edge is created in the return graph for every (i; j) edge in the first input that is notmatched by an (i; j) edge in the second input; if the second input has more (i; j) edges than

the first, no (i; j) edges are created in the return graph.

*****: An (i; j) edge is created for every pairing of (i; j) edges in the respective input graphs.

in the first input and the second edge in the second input.

**%c%**: An (i; j) edge is created in the return graph for every edge pair (i; k); (k; j) with the first edgein the first input and the second edge in the second input.

**!**: An (i; j) edge is created in the return graph for every (i; j) in the input not having an edge.

**|**: An (i; j) edge is created in the return graph if either input contains an (i; j) edge.

**&**: An (i; j) edge is created in the return graph if both inputs contain an (i; j) edge.**leave a comment**for the author, please follow the link and comment on their blog:

**ProbaPerception**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.