Circular loops in R package iGraph - r

Using iGraph, how can I represent self-reflexive nodes with circle shaped curves? By default, these curves are represented by a pinched or tear drop shaped loop.

As Spacedman said, you would need to do quite some programming to do this. You could plot a graph without self-loops and then add them (graphs are basically a scatterplot and you can use points and similar functions to add lines to them), but this is not trivial (especially since you need to know the edge of nodes, not their center) and will cause the selfloops to be plotted on top of everything else which might not look good.
This weekend I have updated qgraph with how self-loops work. qgraph can be used to plot networks and should play nicely with igraph. e.g.:
# An adjacency matrix:
A <- matrix(1,3,3)
library("igraph")
# igraph graph and layout:
Graph <- graph.adjacency(A)
Layout <- layout.circle(Graph)
# Plot in qgraph:
library("qgraph")
qgraph(get.adjacency(Graph,sparse=FALSE),layout=Layout,diag=TRUE,directed=TRUE)
I am quite content with how these self-loops turned out and they seem to be more to what you describe. So this could be an option. However, my loops are just as hardcoded. For reference, I compute the edge of a node (starting and ending point of the loop) with the inner function qgraph:::Cent2Edge and compute the shape of the loop (spline) with the inner function qgraph:::SelfLoop.

Inside plot.igraph you can see that loops are drawn using a plot.bezier function, and all the control for that is pretty much hard coded there. You'd have to rewrite large chunks of plot.igraph to call a plot.circle function you'd have to write to do this.
Also, I'm guessing you don't want complete circles, but circle segments that start on the edge of the vertex symbol (the default blue circle with the vertex number in it) and end (possibly with an arrowhead) on another part of the edge of the vertex symbol? Or do you want circles that touch the symbol like the bezier teardrop loops do?
Either way, the answer seems to be 'no, not without doing some programming or submitting a feature request to the igraph guys'
I posted an earlier answer saying the layout functions were involved, but that's not true - the layout functions only position the vertices, and it is plot.igraph's job to draw the edges.

Related

Adjust curvature of igraph mark.groups polygons when plotting networks in R

I was following the guide at https://kateto.net/network-visualization, and was wondering if there is a way to set the curvature of the polygons produced by the mark.groups option to zero, and so that their borders are straight lines (ideally they would still "loop around" the different sized nodes).
If possible, I would like the polygons to wrap exactly around each node (without any space inbetween), but if that is not possible, I can work with setting the size of the nodes to zero.
Any help is appreaciated. I can also work with other software if necessary.
I couldn't find the docs before, but found them now. The additional options are mark.expand=0 and mark.shape=0: https://igraph.org/r/doc/plot.igraph.html

Coloring Rgl 3d mesh faces

I'd like to color faces on an triangular RGL mesh based on proximity to a vertex.
The thing is, it seems that a lot of the times that the vertices are associated with faces that are very far from the actual vertex location itself, which creates a problem when I want to color faces around one vertex; the faces end up being very far from where the barycenter actually is.
What I'm doing right now is this:
Compute the barycenter of all the faces in the mesh.
Use the FAR package to compute the closest n barycenters to the desired point. Keep those indices.
Based on the indices gathered, color those faces a certain color. The rest of the faces would be colored white.
colors=rep('white',num_faces)
colors[colored_faces]='red'
mesh$material=list(color=colors)
Then I would plot the mesh: plot3d(mesh)
The thing is, I'm getting very odd coloring right now, is there any established way to color faces that close to a certain coordinate/vertex?
This is what the mesh currently looks like, with the red as the 'colored' faces, and the blue as the points that I would like there to be a colored face near.
Mesh
Update: Seeing this, my question has now been modified to:
How can I find the closest face to a given point? It still isn't clear to me, since the face barycenters are sometimes misleading, and don't represent actual distance to a given vertex.
Update 2:
I've added example code and a file here: Files and code
Basically the code finds the nearest faces to a given vertex of the same 3d mesh with the nearest neighbor algorithm, and then we color those faces in our color vector (remembering to color the colors 4 times):
Except, when we run this algorithm, we only color one side of the shape: like so:
Odd
How can I make the coloring a bit more symmetric?
Update 3: This problem has been resolved! Please look to the unreleased version of rgl on Rforge for the newest version of rgl that allows for coloring of faces, vertices, and edges.
Update 4: Here is the new image by coloring the closest vertices (to show that the new rgl package works wonders):
Better Sink
Your code to compute the centroid is incorrect. You have
#Function for computing the barycenter/centroid of a face.
compute_face_centroid=function(vertices,face){
vertex=vertices[,face][-4,]
centroid=apply(X=vertex,MARGIN = 1,FUN = mean)
return(centroid)
}
This just removes the 4th row of the vertices array, which is the wrong way to convert homogeneous coordinates to Euclidean coordinates. You really need to divide the other rows by the 4th one. You can do this using the rgl function asEuclidean:
#Function for computing the barycenter/centroid of a face.
compute_face_centroid=function(vertices,face){
vertex <- asEuclidean(t(vertices[,face]))
apply(vertex, MARGIN = 2, FUN = mean)
}
There may also be other issues in your code, I haven't traced through everything yet.
BTW, the unreleased test version of rgl changes the way colours are handled in meshes, hopefully making that part of your code simpler. You can get it from R-forge.r-project.org if you want to try it. You can now specify colours by vertex or by face.
Edited to add:
Okay, I've taken a closer look now. I think your code was actually working. The compute_face_centroid should be corrected, but since your example always has value 1 for the final component, deleting it is okay.
The reason you got colouring different from what you expected is just that the triangles making up your mesh really vary in shape. If you plot your image as a wireframe you'll see this:
wire3d(file)
The centroids of those long thin triangles are quite far from your selected point.

Smoothing a flat surface in maya

i was wondering if anyone of u here knows how to smooth a polygon in Maya? I've tried 2 methods which i found online. One of which is 'Vertice Averaging' and the other 'Smooth' which are both under the 'Mesh' option.
Vertice Averaging caused my polygons to have 'gaps' or 'holes' between the triangles, which i do not intend for that to happen.
While Smooth causes the polygon's face to have 4 vertex instead of the original 3, which i do not want as well, as i need a polygon with triangle faces.
http://img.photobucket.com/albums/v483/dragonlancer/PolygonAveragingampSmoothing.jpg
And to whoever told me that it is a bug, i tried, but doesnt work =[
You said you wanted to maintain your tris so you could switch the smooth option 'Add Divisions' from exponential to linear.
If you're getting gaps, its because the original mesh has verts which are not welded together. Try Edit Mesh > Merge with a small tolerance value before running average or smooth.
In general you'll get more pleasant results if you smooth a quadrangular mesh instead of a trimesh - when you subdivide quads the results are very similar to NURBS curves, whereas smoothed triangles always tend to look look like old-fashioned 1990's game graphics.

Map 3D point cloud onto surface then flatten

Mapping a point cloud onto a 3D "fabric" then flattening.
So I have a scientific dataset consisting of a point cloud in 3D, this point cloud comprises points on a surface that is curved. In order to perform quantitative analysis I however need to map these point clouds onto a surface I can then flatten. I thought about using mapping tools sort of like in the case of the 3d world being flattened onto a map, but not sure how to even begin as I have no experience in cartography and maybe I'm trying to solve an easy problem with the wrong tools.
Just to briefly describe the dataset: imagine entirely transparent curtains on the window with small dots on them, if I could use that dot pattern to fit the material the dots are on I could then "straighten" it and do meaningful analysis on the spread of the dots. I'm guessing the procedure would be to first manually fit the "sheet" onto the point cloud data by using contours or something along those lines then flattening the sheet thus putting the points into a 2d array. Ultimately I'll probably also reduce that into a 1D but I assume I need the intermediate 2D step as the length of the 2nd dimension is variable (i.e. one end of the sheet is shorter than the other but still corresponds to the same position in terms of contours) I'm using Matlab and Amira though I'm always happy to learn new tools!
Any advice or hints how to approach are much appreciated!
You can use a space filling curve to reduce the 3d complexity to a 1d complexity. I use a hilbert curve to index lat-lng pairs on a 2d map. You can do the same with a 3d space but it's easier to start with a simple curve for example a z morton order curve. Space filling curves are often used in mapping applications. A space filling curve also adds some proximity information and a new sort order to the 3d points.
You can try to build a surface that approximates your dataset, then unfold the surface with the points you want. Solid3dtech.com has the tool to unfold the surfaces with the curves or points.

How do I lengthen edges in an igraph network plot (layout=fruchterman.reingold)?

Trying to do a network plot in R.
How do I lengthen edges in a network graph using IGraph?
I actually want to use the fruchterman-reingold layout. Is there some way I can make that force-based algorithm "springier" so that my vertices are further apart?
thanks.
You can control the Fruchterman-Reingold algorithm using the layout.fruchterman.reingold function. see: help('layout.fruchterman.reingold'). A setup that I often use and gets you a little more spacing is:
l <- layout.fruchterman.reingold(g,niter=500,area=vcount(g)^2.3,repulserad=vcount(g)^2.8)
plot(g,layout=l)
where g is your graph object. Best to just test different values of these parameters for your graph and see what works. Especially repulserad influences the spacing in a graph. The default is the square of the number of nodes, so higher values should get you more spaced graphs.
If the layout.fruchterman.reingold algorithm still does not give what you want by tweaking the parameters, simply do the following. Every layout returns a set of coordinates, with the x- and y-coordinate in the first and second column respectively. You can apply any transformation you like here, and if you would just like to scale it, simply use
L = layout.fruchterman.reingold(G)*s; #Scaling factor s
More fancy transformations are of course also possible. Just for the record, you can also edit the layout manually with your mouse by using tkplot(G, layout=L). You can get the coordinates back via L = tkplot.getcoords(1).

Resources