ggnet2 function is a visualization function to plot network objects as
ggplot2 objects. It accepts any object that can be coerced to the
network class, including adjacency or incidence matrices, edge lists, or one-mode
igraph network objects.
R already provides many ways to plot static and dynamic networks, many of which are detailed in a beautiful tutorial by Katherine Ognyanova.
All of these tools, however, require to use a new graph syntax, either within or outside of
R, in order to create new network objects with the appropriate properties for plotting.
Instead, for the many users who are familiar with the
ggplot2 package, it might be interesting to use a syntax that comes close to its “grammar of graphics” to process and plot network data, in the same format as was used for network analysis.
This idea motivated the very first version of
ggnet, by Moritz Marbach, and is also motivating the development of
geom object for network data structured as data frames, by Sam Tyner and Heike Hofmann.
ggnet2 is an improved version of
ggnet. Both functions are available from the
GGally package or as standalone functions.
ggnet2 brings several improvements that convey additional control over all plotting parameters.
ggnet2 is available through the
Or it can also be installed from its standalone package:
library(network) library(sna) library(ggplot2)
ggnet2 suggests the following packages:
RColorBrewerpackage is installed,
ggnet2will be able to use ColorBrewer palettes to color network nodes.
intergraphpackage is installed,
ggnet2will be able to process one-mode networks objects created with the
In this vignette, “nodes” designate the vertices of a network, and “edges” designate its ties. Readers who are not familiar with network terminology might want to consult a handbook such as Networks. An Introduction, by Mark Newman.
Most of this vignette is organized around two simple network examples:
The vignette also contains a section that illustrates some additional capabilities of
ggnet2, and another section showing two additional examples of real-world networks plotted with
ggnet2. It closes on known limitations of
Let’s start with an undirected Bernoulli random graph, with 10 nodes named “a, b, …, i, j”, and a rather high likelihood of an edge to exist between them:
# random graph net = rgraph(10, mode = "graph", tprob = 0.5) net = network(net, directed = FALSE) # vertex names network.vertex.names(net) = letters[1:10]
This graph can be visualized with
ggnet2 without any further work:
net argument is the only compulsory argument of
ggnet2. It can be a
network object or any object that can be coerced to that class through its
edgeset.constructors functions, such as adjacency matrixes, incidence matrixes and edge lists.
The most basic properties that one might want to change at that stage are the size and color of the nodes, or the size and color of the edges. Let’s modify each of these properties:
ggnet2(net, node.size = 6, node.color = "black", edge.size = 1, edge.color = "grey")
The vertex-related arguments of
ggnet2 start with
node, and its edge-related arguments start with
node.size arguments can be abbreviated:
ggnet2(net, size = 6, color = "black", edge.size = 1, edge.color = "grey")
It also possible to pass a vector of node colors directly to
ggnet2, as long as it has the same number of elements as the network has nodes:
ggnet2(net, size = 6, color = rep(c("tomato", "steelblue"), 5))