I have written the following code which takes a matrix of nodes (x, y coordinates) and a quadratic adjacency matrix (edges) and plots a network virtualization graph.
plotGraph <- function(){
coordinates <- matrix(rexp(50), 10)
adjacency <- matrix(c(0,1,0,0,2,0,1,0,0,2,1,0,1,0,0,1,0,2,0,1,0,1,2,0,1,1,1,0,1,0), nrow=nrow(coordinates), ncol=nrow(coordinates))
x11()
plot(coordinates, main="Network Visualisation", xlab="coordinates x", ylab="coordinates y", type="p")
pos <- which(adjacency>0, arr.ind = T)
max = ncol(adjacency) * nrow(adjacency)
for(i in 1:max){
arrows(coordinates[pos[i,1],1], coordinates[pos[i,1],2], coordinates[pos[i,2],1], coordinates[pos[i,2],2], col="royalblue3", lwd=1)
}
}
plotGraph()
It seems to work well but now, using the pre-installed Graphics package, how can I increase the edge, the greater the adjacency?
Thanks in advance for any suggestions!
You need to use lwd (see help of the function lines
) and set it to adjacency. You also should not plot lines from a point to itself (and avoid warnings). Would the following do what you need:
coordinates <- matrix(rexp(50), 10)
adjacency <- matrix(c(0,1,0,0,2,0,1,0,0,2,1,0,1,0,0,1,0,2,0,1,0,1,2,0,1,1,1,0,1,0), nrow=nrow(coordinates), ncol=nrow(coordinates))
plot(coordinates, main="Network Visualisation", xlab="coordinates x", ylab="coordinates y", type="p")
pos <- which(adjacency>0, arr.ind = T)
for(i in 1:dim(pos)[1]){
if(pos[i,1]!=pos[i,2]){
arrows(coordinates[pos[i,1],1], coordinates[pos[i,1],2],
coordinates[pos[i,2],1], coordinates[pos[i,2],2],
col="royalblue3", lwd=adjacency[pos[i,]])
}
}