How to sort and visualize a directed graph? - r

I am using R with igraph and I have a square matrix with weights. I want to sort it. I thought to use page.rank(g) and I got a corresponding vector and its values.
library(igraph)
g<-get.matrix()
page.rank(g)$value
page.rank(g)$vector
Now I want to sort using this values and visualizing it in a graph if it is possible.
Something similar to the following picture:
How I could do this?

Choose a force-based layout and set the vertex size (vertex.size) to be proportional to the page rank values. See an example on the igraph homepage on how to set the vertex size. (The example uses tkplot, but you can just use plot instead of that.) You can set the vertex labels via the vertex.label argument to plot, and \n is allowed to make multi-line labels.

Related

R: Untangle graph plot

I have the following graph plot with 131 vertices made by using plot with an object of the igraph class. My question is whether there is any way to present this in a cleaner way so that nodes at least don't overlap each other and edges are more visible.
I'm not too familiar with these types of graphs, so I can't provide a specific answer. But this sounds like a good time to use the jitter() function which adds random noise between the data points, thus separating them out.

How to plot a graph with labels that don't superimpose on each others

I am new at R but I haven't find any topic that could answer my question.
I want to plot a graph named g, using the package igraph. However the result is not fully satisfying as the nodes names aren't all readable.
This is the code I used to plot :
plot(g, layout_nicely(g), asp=FALSE, vertex.shape=V(g)$shape, vertex.label.dist=0.3,
vertex.label.degree=-pi/2)
And this is the result
I've tried to reduce the number of characters of each label, or to reduce their font sizes but it wasn't enough. I also used several layout such as layout.kamada.kawai, layout.fruchterman.reingold but the labels are always too close to each others.
Is there any way to choose a better position for the labels or to increase the length of the edges ?
Any help would be appreciated

How can I rearrange the order of edges in an igraph plot?

I'm trying to make a network plot in igraph that highlights certain important edges by coloring them differently than the others. For large graphs, they often get buried under the others. For example:
library(igraph)
test <- barabasi.game(200,m=2)
E(test)$color <- "gray"
E(test)[1]$color <- "red"
sort(order(E(test)$color)[E(test)],decreasing=TRUE)
plot(test,
vertex.label=NA,
vertex.shape="none",
vertex.size=0,
edge.arrow.mode=0,
edge.width=2)
gives me a plot where the single red edge is at the bottom.
If I choose to color a higher-numbered edge (rather than #1) it has a better chance of not being buried.
So it seems to me that one option is to somehow re-order the edges. I tried
E(test) <- E(test)[order(E(test)$color)]
but that gets me an "invalid indexing" error. Any ideas about what else I should try?
igraph plots the edges in the order they appear in the graph's edge list, so you are right, edges with higher IDs will be drawn on top of the edges with lower IDs. Unfortunately igraph does not provide an easy way to reorder the edges of the graph (although it has a function named permute.vertices, which will allow you to permute the vertices), so the only way I can think of right now is that you need to construct another graph in which the edges are in the "right order". make_graph ensures that the edges are stored in the graph exactly in the order you specify them, and I think so does graph_from_data_frame.
Another option (if you don't want to reconstruct the entire graph) is to plot the graph twice: first you plot the "not-so-important" edges and set the width of the important ones to zero, then you plot the important edges on top.
If you would like edge permutations to be supported in an upcoming version of igraph, please file a feature request on Github.
You can reconstruct the graph with the edges reordered using "as_data_frame" and "graph_from_data_frame" fairly easily in conjunction with the dplyr and tidyr packages:
new_graph=graph_from_data_frame(d=as_data_frame(old_graph,what="edges") %>%
arrange(desc(ordering_attribute)),
vertices=as_data_frame(old_graph,what="vertices"))
if you had a layout stored on the old graph you would need to transfer it manually...
new_graph$layout = old_graph$layout
The reason why E(test) <- E(test)[order(E(test)$color)] can not work out is that we are not allowed to assign an 'igraph.es' variable to another existing one or itself; however, we can create a new 'igraph.es' varibale using: a <- E(test)[order(E(test)$color)]. Thus, we need create a new graph to inherit a new order of edges from the orginal graph:
library(igraph)
test <- barabasi.game(200,m=2)
E(test)$color <- "gray"
E(test)[1]$color <- "red"
# to make a new graph:test2 whose edges are descended from the ordering edges of the orginal graph:test
test2 <- make_graph(as.vector(t(get.edgelist(test)[order(E(test)$color),])))
# to assign the attribute value to the edges of the new graph
E(test2)$color <- E(test)$color[order(E(test)$color)]
plot(test2,
vertex.label=NA,
vertex.shape="none",
vertex.size=0,
edge.arrow.mode=0,
edge.width=2)
As the plot shows, the red edge becomes the toppest one:
Or you could simply rearrange your data based on the variable you wish to be ordered and then feed it to igraph

plot igraph in a big area

Just wondering if it is possible to increase the size of the plot so that the nodes and edges can be more scattered over the plot.
Original plot:
What are expected:
I tried many parameters in the layout function such as area, niter, and so on, but all of them do not work. By the way, I am using 'igraph' package in R.
If you are referring to the actual size of the produced output (pdf, png, etc), you can configure it with the width and height parameters. Check this link for png,bpm, etc, and this link for PDF format.
A MWE is something like this:
png("mygraph.png", heigh=400, width=600)
#functions to plot your graph
dev.off()
If you are referring to the size of the graphic produced by the layout function, as #MrFlick referred, you should check the parameters of the particular layout you are using.
Hope it helps you.
In your second graph, it's obviously the graph can be divided into several clusters (or sections). If I understood you correctly, you want to have a layout that separates your clusters more visibly.
Then you can draw this by calculating a two-level layout:
First, calculate the layout of the graph in order to find a place for each cluster.
Second, calculate the layout in each cluster according to first step and plot nodes in the corresponding place.

Use tkplot in igraph when matching vertex size to label

I am trying to apply the script suggested in another post on this forum for matching vertex size to label: Match vertex size to label size in igraph.
The solution for the plot function works perfectly but the same syntax cannot be used for the tkplot function. Replacing plot by tkplot returns an error message. I need to use the latter one because my figure has many vertexes and too long vertex labels, and i want to be able to readjust the positions of the vertexes manually. Can someone help?
This is not really an answer to your question, but a workaround. Use tkplot() to plot your vertices, adjust the vertex positions, then query the positions with tkplot.getcoords(), and use the returned coordinates in plot().

Resources