Plotting graph visualization of weighted Adjacency Matrix in Julia - plot

I have a weighted directed adjacency matrix and I am trying to plot a graph visualization using PlotRecipes.jl. I can't seem to find a way to show both the nodes with labels and somehow represent the edge weights on the edges. I have tried...
graphplot(G, weights=weights,names=names)
where G is the adjacency matrix, weights are the edge weights and names is a list of names for the nodes, but I get this output
using just
graphplot(G,names=names)
I get the same output.
Is there any way to do this with PlotRecipes.jl or is there possibly another library?

You can represent the weights with color (using the line_z keyword), but not with line thickness at the moment. cf https://github.com/JuliaPlots/PlotRecipes.jl/issues/39

Related

The plotting function in R igraph

I use igraph R to visualize chemical compounds. In this approach, nodes correspond to atoms and edges correspond to bonds between atoms. Such represented chemical molecule is known as a molecular graph or H-depleted graphs. For instance, to represent 2-methylbutane, 2mb for short, (simple organic molecule whose carbon skeleton consists of five atoms) I use the following R code:
molecular.graph.2mb = graph.formula(1-2,2-3,3-4,2-5)
To visualize this molecular graph I use as a layout the following matrix m:
m = matrix(c(1,0,2,0,3,0,4,0,2,1), nrow=5, byrow=TRUE).
This matrix contains coordinates of vertices of my graph. Then I plot this graph:
plot(molecular.graph.2mb, layout=m).
I obtain the graph where the geometrical distance between the vertices 2 and 5 is larger than between v1 and v2, v2 and v3 as well as v3 and v4.
My question is: How to force igraph R to plot the graph where all geometrical distances will be equal?
The main trick to doing this is to set the parameter rescale=FALSE and adjust the xlim. Doing that alone made the nodes very small so I needed to adjust the node size as well. I think this is what you are looking for.
library(igraph)
## Your graph
molecular.graph.2mb = graph.formula(1-2,2-3,3-4,2-5)
m = matrix(c(1,0,2,0,3,0,4,0,2,1), nrow=5, byrow=TRUE)
## Modified plot
plot(molecular.graph.2mb, layout=m, rescale=FALSE,
vertex.size=30, xlim=c(0.5,4.5))

From (not a square) adjacency matrix to visual graph in R

We have an non square adjacency matrix p (197x190)matrix without weights (only 1 if the 2 telephone numbers have called with each other and 0 otherwise). We tried to visualize this with a graph using this simple code and the igraph package:
p<-as.matrix(dcast(SNA_data, A_NUMBER~B_NUMBER, value.var="W", fill=0))[,]
graph<-graph.adjacency(p, mode="undirected", weighted=NULL)
plot(graph)
The result is a very small graph in plot that is totally unreadable graph anybody knows how to solve this?
Thanks in advance.
Try to use x11() to plot your graph in an external window :
library(igraph)
adjm1<-matrix(sample(0:1,1000,replace=TRUE,prob=c(0.9,01)),nc=10)
g1<-graph.adjacency(adjm1)
x11()
plot(g1)
Reference for the simulation : using graph.adjacency() in R
If your matrix is not a square, it might be worth trying an incidence matrix. By default the matrix is directed from rows to columns.
# create the network object
network <- graph_from_incidence_matrix(p)
network
class(network)
# plot it
plot(network)

How to study the interaction between a set of nodes when the network is quite dense, using igraph?

I have 4 undirected graph with 1000 vertices and 176672, 150994, 193477, 236060 edges. I am trying to see interaction between a specific set of nodes (16 in number) for each graph. This visualization in tkplot is not feasible as 1000 vertices is already way too much for it. I was thinking of if there is some way to extract the interaction of these 16 nodes from the parent graph and view separately, which will be then more easy to handle and work with in tkplot. I don't want the loss of information as in what is the node(s) in he path of interaction if it comes from other than 16 pre-specified nodes. Is there a way to achieve it?
In such a dense graph, if you only take the shortest paths connecting each pair of these 16 vertices, you will still get a graph too large for tkplot, or even to see any meaningful on a cairo pdf plot.
However, if you aim to do it, this is one possible way:
require(igraph)
g <- erdos.renyi.game(n = 1000, p = 0.1)
set <- sample(1:vcount(g), 16)
in.shortest.paths <- NULL
for(v in set){
in.shortest.paths <- c(in.shortest.paths,
unlist(get.all.shortest.paths(g, from = v, to = set)$res))
}
subgraph <- induced.subgraph(g, unique(in.shortest.paths))
In this example, subgraph will include approx. half of all the vertices.
After this, I think you should consider to find some other way than visualization to investigate the relationships between your vertices of interest. It can be some topological metric, but it really depends on the aims of your analysis.

Plot communities with igraph

I want to create a graph plot with each community of nodes been covered by some background color, similar to the graph by the following code
karate <- graph.famous("Zachary")
wc <- walktrap.community(karate)
modularity(wc)
membership(wc)
plot(wc, karate)
But different from this approach, I want to: (1) group the nodes by myself, instead of resulting from community detection algorithm. I achieved this by wc$membership <- some_vector; (2) plot such graph possibly with overlap between communities, then how can I assign one node to multiple communities?
Plot the graph itself instead of the community structure, and use an appropriately constructed mark.groups argument to plot() to tell igraph which groups should be enclosed by the same group. The following quote is from the manual page of plot.igraph:
mark.groups: A list of vertex id vectors. It is interpreted as a set of vertex groups. Each vertex group is highlighted, by plotting a colored smoothed polygon around and "under" it.

igraph nonreciprocal edges after converting to undirected graph using mutual

I'm working on a directed graph in igraph for R. I'm trying to convert it to an undirected graph where just reciprocal edges of the former persist. Should be easy but I'm getting strange results.
first I did it like that
library(igraph)
load("dmNet.Rdata")
#http://www.unet.univie.ac.at/~a0406222/dmNet.Rdata
recNet <- as.undirected(net, mode = "mutual",edge.attr.comb="sum")
when I check E(recNet)$weight there are a lot of edges with a weight of 1, which should not be possible since the sum of two reciprocal edges has to be at least 2. Then I did it like that
recNet <- as.undirected(net, mode = "mutual",edge.attr.comb="c")
now I can see that there are actually some edges containing just one value. My new graph recNet seems to contain nonreciprocal edges of net. What am I doing wrong, or am I missunderstandig the "mutual option"?
This happens because some edges are self-loops in your graph. All the edges that end up with a weight of 1 are self-loops:
all(which(E(recNet)$weight == 1) %in% which(is.loop(recNet)))
# [1] TRUE
Apparently, a self-loop is considered as a mutual edge in a directed graph. If you want to consider self-loops as non-mutual, then you can just remove them from the graph. Be careful, though, because some vertices have multiple self-loops, and you might not want to remove these.

Resources