I was trying to draw a weight graph using Sage for a 10x10 matrix, A. I used the following code:
G = DiGraph(A, format='weighted_adjacency_matrix')
H = G.plot(edge_labels=False, graph_border=True)
H.show()
I was wondering if someone could help me change the starting node from 0 to 1. Currently the node labes are 0,1,...9 but I would prefer them to be 1,...,10.
It took me a lot longer to find this than it should have.
G.relabel([1..10],inplace=True)
This function actually has quite a few nice features.
Related
Hi I am new to R and trying to learn. I would like to compare the overlap of 4 clusters which have the same 4 categories each in them. Basically, I am thinking of making a clustered heatmap like this image below that I quickly made as an example in excel. Does anyone know of an R package that would allow me to make a graph like this? So far, I have only found packages that limit you to one variable per X vs Y variable grid space. Thanks so much for your suggestions!
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.
I am trying to plot an igraph object using the visIgraph function in R, this function has a parameter layout which takes a string input. However, I want a layout_as_tree with a specific root node like how it's possible while plotting on igraph.
You do not provide any data to plot, so I will make a simple tree as an illustration.
library(igraph)
library(visNetwork)
## Basic tree
EL = matrix(c(1,2,1,3,2,4,2,5,3,6,3,7), byrow=TRUE, ncol=2)
Tree = graph_from_edgelist(EL)
There are two ways that you can use the igraph layouts to plot using visIgraph.
1 Pass in the layout function to be used
visIgraph(igrap=Tree, layout="layout_as_tree", flip.y=FALSE, root=1)
The extra arguments flip.y=FALSE, root=1 are passed to the function layout_as_tree.
2 Create the layout first and pass the layout matrix to visIgraph
This version allows you to look at the layout and adjust it if you want.
LO = layout_as_tree(Tree, root =1, flip.y=FALSE)
visIgraph(Tree, layout='layout.norm', layoutMatrix=LO)
Both versions create this graph.
Edit - Responding to comment on 100 nodes
Of course, you can only show so many nodes on the screen and still see what is going on, but at 100 nodes I got pretty good results by using type="full".
T100 = make_tree(100)
LO = layout_as_tree(T100, root =1, flip.y=FALSE)
visIgraph(T100, type="full", layout='layout.norm', layoutMatrix=LO)
I am trying to build graphs using tree-like data, where nodes typically split into >2 edges. I have tried various layouts, and I see that the layout.reingold.tilford parameter will generate tree-like graphs with non-bifurcating data. However the outputs are not particularly attractive. I would rather use something like the layout.lgl or layout.kamada.kawai since these produce more radial structures. I cannot see how to change the parameters in R such that these trees have no overlapping edges though. Is this possible?
I imported a simple data file in Pajek format, with 355 nodes and 354 edges. I'm currently printing it using:
plot.igraph(g,vertex.size=3,vertex.label=NA,layout=layout.lgl)
This gives me an output like this, which is nice, but still has overlapping edges. I have read that you can manually fix this using tkplot, or another program like cytoscape, however I have quite a few of these to build, and the size of them makes manual correction a hassle.
Many thanks.
Just want to add a comment but my rep is too low. The method that #bdemarest posted does not work on igraph version > 0.7. The newer version does not support the area parameter, so I cannot get the same effect. And getting the old version to build took me a while, so I though I'd share some insights. You can manually install igraph 0.7 from source if you download it from igraph nightly builds. On my machine (Mac OS 10.10), I encountered some problems building it, due to gfortran, so I found this link that solved the problem. Hope that helps anyone who wants to create similar graphs in R.
You may want to try layout.fruchterman.reingold(). It seems to do a good job keeping the edges from crossing. I've tested it with a 355 node version of the barabasi graph suggested by #Tamás.
library(igraph)
g = barabasi.game(355, directed=FALSE)
png("plot1.png", height=6, width=12, units="in", res=200)
par(mfrow=c(1, 2))
plot.igraph(g,vertex.size=3,vertex.label=NA,
layout=layout.fruchterman.reingold(g, niter=10000))
mtext("layout.fruchterman.reingold, area = vcount^2", side=1)
plot.igraph(g,vertex.size=3,vertex.label=NA,
layout=layout.fruchterman.reingold(g, niter=10000, area=30*vcount(g)^2))
mtext("layout.fruchterman.reingold, area = 30 * vcount^2", side=1)
dev.off()
layout.reingold.tilford has a parameter called circular. Setting this to TRUE will convert the final layout into a radial one by treating the X coordinate as the angle (after appropriate rescaling) and the Y coordinate as the radius. Ironically enough, this does not guarantee that there will be no edge crossings in the end, but it works nicely if your subtrees are not exceedingly wide compared to the rest of the graph:
> g <- barabasi.game(100, directed=F)
> layout <- layout.reingold.tilford(g, circular=T)
> plot(g, layout=layout)
my questions involves the following: I have the basic task of visualizing the steps in a sorting algorithm by plotting the vector as a bar graph. That's no problem and I already have my solution. The only problem is that I consider my solution ugly in the sense, that I always make a call to a plotting function and thus get a new window all the time, resulting in a lot of them.
Question: Can I somehow make a function that takes the previous plot as an argument and plots the graph in the same window? Or something similar.
Thanks
You should just use the Jupyter notebook from within Sage (using a SageMath kernel). Then you can just reevaluate the cell when you want to update your graph. Or, if I'm understanding the question correctly, you could have
Cell 1 - basic functions
Cell 2 - function that updates graph using P += new_plot syntax
Cell 3 - cell where you do the plotting
I'm not certain I've understood, though.
Previously:
You should probably try using the Sage notebook.