Visualizing networks with ggplot2
This note describes a few ways to handle network objects (which might be objects of class igraph
or network
, or data frames representing edge lists) through graphical methods that rely on ggplot2
.
ggnet
and ggnet2
ggnet2
is an extended version of the ggnet
function, which plots network objects as ggplot2
objects. In comparison to ggnet
, ggnet2
provides additional controls over the aesthetics of the plot and simplifies the visualization of bipartite networks.
Both ggnet
and ggnet2
can be downloaded from their development repository or can be loaded through the GGally
package, the latest version of which is now available from CRAN. How to use the ggnet2
function is explained and illustrated in this vignette.
Both functions are extended versions of Moritz Marbach's plotg
function. The code relies on network
and sna
for its internals, although the same results could also be achieved with igraph
.
geomnet
Samantha Tyner and Heike Hofmann have been working for some time on a very interesting approach to the problem of plotting networks with ggplot2
. Their code is a straight add-on to ggplot2
that comes in the form of a single geom, geom_net
.
The geometry can be installed from CRAN as a standalone package.
The input for geom_net
is not an object of class network
or igraph
. Instead, the geom requires a data frame that containing both edge and node information. To achieve that, all the user needs to do is to left-join the edge list of the network to the node-level attributes of the sender nodes.
The result of that approach is a highly economical way to get networks plotted with ggplot2
, through a function that requires nothing more than a call to ggplot
and a call to geom_net
. The results is compact and fully in line with the spirit of how ggplot2
is supposed to work.
geom_edges
and geom_nodes
My own take at writing up some network geoms involves a different and less compact logic than that of geom_net
. Let's conceive all network plots as a combination of four graphical layers, the first two being generally present in all network plots:
- nodes, which are points
- edges, which are segments
- node labels, which are text
- edge labels, which are text
What I did was simply to ~~hack~~ alias the geoms for points, segments and text to produce each layer, while also taking advantage of the new geoms for curved segments and text labels introduced in ggplot2
~~1.1.0~~ 2.0.0. The code is illustrated through many different examples in the package vignette.
This approach is more verbose, but potentially more flexible, as every single layer of the graph gets its own aesthetics. Because there is no way to create a dual color scale for points and segments, this approach still does have its limits, though.
The limits of trying to plot networks with ggplot2
become even more obvious when one tries to articulate faceting with either of the methods presented above: faceting the edges or the nodes of the network is quite doable, but faceting both at the same time is far from obvious.
Last, edge arrows are also a challenge of their own, as there is no simple way to ‘stop’ drawing a segment when it ‘reaches’ a given point of the plotting surface.
Update (March 24, 2016): the ggnetwork
package, which contains the geom_edges
and geom_nodes
geoms described above, has been greatly improved to work with ggplot2
2.0.0, and is on its way to CRAN (see this other note).
- First published on October 5th, 2015