EVOLUTION-MANAGER
Edit File: shapes.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Various vertex shapes when plotting igraph graphs</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for shapes {igraph}"><tr><td>shapes {igraph}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Various vertex shapes when plotting igraph graphs</h2> <h3>Description</h3> <p>Starting from version 0.5.1 igraph supports different vertex shapes when plotting graphs. </p> <h3>Usage</h3> <pre> shapes(shape = NULL) shape_noclip(coords, el, params, end = c("both", "from", "to")) shape_noplot(coords, v = NULL, params) add_shape(shape, clip = shape_noclip, plot = shape_noplot, parameters = list()) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>shape</code></td> <td> <p>Character scalar, name of a vertex shape. If it is <code>NULL</code> for <code>shapes</code>, then the names of all defined vertex shapes are returned.</p> </td></tr> <tr valign="top"><td><code>coords, el, params, end, v</code></td> <td> <p>See parameters of the clipping/plotting functions below.</p> </td></tr> <tr valign="top"><td><code>clip</code></td> <td> <p>An R function object, the clipping function.</p> </td></tr> <tr valign="top"><td><code>plot</code></td> <td> <p>An R function object, the plotting function.</p> </td></tr> <tr valign="top"><td><code>parameters</code></td> <td> <p>Named list, additional plot/vertex/edge parameters. The element named define the new parameters, and the elements themselves define their default values. Vertex parameters should have a prefix ‘<code>vertex.</code>’, edge parameters a prefix ‘<code>edge.</code>’. Other general plotting parameters should have a prefix ‘<code>plot.</code>’. See Details below.</p> </td></tr> </table> <h3>Details</h3> <p>In igraph a vertex shape is defined by two functions: 1) provides information about the size of the shape for clipping the edges and 2) plots the shape if requested. These functions are called “shape functions” in the rest of this manual page. The first one is the clipping function and the second is the plotting function. </p> <p>The clipping function has the following arguments: </p> <dl> <dt>coords</dt><dd><p>A matrix with four columns, it contains the coordinates of the vertices for the edge list supplied in the <code>el</code> argument.</p> </dd> <dt>el</dt><dd><p>A matrix with two columns, the edges of which some end points will be clipped. It should have the same number of rows as <code>coords</code>.</p> </dd> <dt>params</dt><dd><p>This is a function object that can be called to query vertex/edge/plot graphical parameters. The first argument of the function is “<code>vertex</code>”, “<code>edge</code>” or “<code>plot</code>” to decide the type of the parameter, the second is a character string giving the name of the parameter. E.g. </p> <pre> params("vertex", "size") </pre> </dd> <dt>end</dt><dd><p>Character string, it gives which end points will be used. Possible values are “<code>both</code>”, “<code>from</code>” and “<code>to</code>”. If “<code>from</code>” the function is expected to clip the first column in the <code>el</code> edge list, “<code>to</code>” selects the second column, “<code>both</code>” selects both.</p> </dd> </dl> <p>The clipping function should return a matrix with the same number of rows as the <code>el</code> arguments. If <code>end</code> is <code>both</code> then the matrix must have four columns, otherwise two. The matrix contains the modified coordinates, with the clipping applied. </p> <p>The plotting function has the following arguments: </p> <dl> <dt>coords</dt><dd><p>The coordinates of the vertices, a matrix with two columns.</p> </dd> <dt>v</dt><dd><p>The ids of the vertices to plot. It should match the number of rows in the <code>coords</code> argument.</p> </dd> <dt>params</dt><dd><p>The same as for the clipping function, see above.</p> </dd> </dl> <p>The return value of the plotting function is not used. </p> <p><code>shapes</code> can be used to list the names of all installed vertex shapes, by calling it without arguments, or setting the <code>shape</code> argument to <code>NULL</code>. If a shape name is given, then the clipping and plotting functions of that shape are returned in a named list. </p> <p><code>add_shape</code> can be used to add new vertex shapes to igraph. For this one must give the clipping and plotting functions of the new shape. It is also possible to list the plot/vertex/edge parameters, in the <code>parameters</code> argument, that the clipping and/or plotting functions can make use of. An example would be a generic regular polygon shape, which can have a parameter for the number of sides. </p> <p><code>shape_noclip</code> is a very simple clipping function that the user can use in their own shape definitions. It does no clipping, the edges will be drawn exactly until the listed vertex position coordinates. </p> <p><code>shape_noplot</code> is a very simple (and probably not very useful) plotting function, that does not plot anything. </p> <h3>Value</h3> <p><code>shapes</code> returns a character vector if the <code>shape</code> argument is <code>NULL</code>. It returns a named list with entries named ‘clip’ and ‘plot’, both of them R functions. </p> <p><code>add_shape</code> returns <code>TRUE</code>, invisibly. </p> <p><code>shape_noclip</code> returns the appropriate columns of its <code>coords</code> argument. </p> <h3>Examples</h3> <pre> # all vertex shapes, minus "raster", that might not be available shapes <- setdiff(shapes(), "") g <- make_ring(length(shapes)) set.seed(42) plot(g, vertex.shape=shapes, vertex.label=shapes, vertex.label.dist=1, vertex.size=15, vertex.size2=15, vertex.pie=lapply(shapes, function(x) if (x=="pie") 2:6 else 0), vertex.pie.color=list(heat.colors(5))) # add new vertex shape, plot nothing with no clipping add_shape("nil") plot(g, vertex.shape="nil") ################################################################# # triangle vertex shape mytriangle <- function(coords, v=NULL, params) { vertex.color <- params("vertex", "color") if (length(vertex.color) != 1 && !is.null(v)) { vertex.color <- vertex.color[v] } vertex.size <- 1/200 * params("vertex", "size") if (length(vertex.size) != 1 && !is.null(v)) { vertex.size <- vertex.size[v] } symbols(x=coords[,1], y=coords[,2], bg=vertex.color, stars=cbind(vertex.size, vertex.size, vertex.size), add=TRUE, inches=FALSE) } # clips as a circle add_shape("triangle", clip=shapes("circle")$clip, plot=mytriangle) plot(g, vertex.shape="triangle", vertex.color=rainbow(vcount(g)), vertex.size=seq(10,20,length.out=vcount(g))) ################################################################# # generic star vertex shape, with a parameter for number of rays mystar <- function(coords, v=NULL, params) { vertex.color <- params("vertex", "color") if (length(vertex.color) != 1 && !is.null(v)) { vertex.color <- vertex.color[v] } vertex.size <- 1/200 * params("vertex", "size") if (length(vertex.size) != 1 && !is.null(v)) { vertex.size <- vertex.size[v] } norays <- params("vertex", "norays") if (length(norays) != 1 && !is.null(v)) { norays <- norays[v] } mapply(coords[,1], coords[,2], vertex.color, vertex.size, norays, FUN=function(x, y, bg, size, nor) { symbols(x=x, y=y, bg=bg, stars=matrix(c(size,size/2), nrow=1, ncol=nor*2), add=TRUE, inches=FALSE) }) } # no clipping, edges will be below the vertices anyway add_shape("star", clip=shape_noclip, plot=mystar, parameters=list(vertex.norays=5)) plot(g, vertex.shape="star", vertex.color=rainbow(vcount(g)), vertex.size=seq(10,20,length.out=vcount(g))) plot(g, vertex.shape="star", vertex.color=rainbow(vcount(g)), vertex.size=seq(10,20,length.out=vcount(g)), vertex.norays=rep(4:8, length.out=vcount(g))) </pre> <hr /><div style="text-align: center;">[Package <em>igraph</em> version 1.3.5 <a href="00Index.html">Index</a>]</div> </body></html>