4 Plotting packages

4.1 ggraph

For this workshop we will use the ggraph package (and function) for network visualization, created by Thomas Lin Pedersen, who also happens to make ‘generative’ artwork in R.

ggraph uses the same approach as the ggplot2 grammar of graphics. This means is has three core components: 1. (network) data, 2. aesthetic mappings, and 3. geometries, in this case edges and nodes. We can combine the ggraph() function with a geometry specific to networks, geom_node_point(), to see the nodes. Where this differs from more traditional ggplotting approaches using tabular data (e.g. data frames), network objects have built-in mapping coordinates, so we don’t need to specify a mapping.

library(ggraph)
## Loading required package: ggplot2
ggraph(net1) +
    geom_node_point()
## Using "stress" as default layout

Here we see that the node point geometry uses a default layout based on the ‘stress’ algorithm (more in the following section), plotting the node points as we see above. To connect these points, we can use a ‘edge link’ geometry, geom_edge_link(), which uses the network object’s built in x, y, xend, and yend variables for the edge data.

ggraph(net1) +
    geom_node_point() +
    geom_edge_link()
## Using "stress" as default layout

This will be the foundation what we build on, starting in the next section.

4.2 Other packages and functions

There are several plotting packages out there, some of them work with different network objects and requiring different knowledge. Excellent overviews of some of them are provided in this post by Katya Ognyanova. I won’t need to repeat these, but it is worth seeing the basic representation and understanding how igraph vs. network objects look differently in them, by default.

4.2.1 plot()

Base R plotting works with network and igraph objects, though their defaults for each object are different.

plot(net1)
plot(g1)

4.2.2 GGally::ggnet2()

The ggnet2 function is also dynamic, but despite being housed as part of the GGally extension of the ggplot2 series it seems to function more like a base R plot. For this function, igraph and network objects look the same.

GGally::ggnet2(net1)
GGally::ggnet2(g1)

4.2.3 ggnetwork::ggnetwork()

Very similar to ggraph and also an extension of the ggplot family is the ggnetwork function. This function integrates directly with ggplot2 and specifies the default coordinates of ggraph, but otherwise operates quite similarly. For this function, igraph and network objects look the same.

library(ggplot2)
library(ggnetwork)
ggplot(net1, aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_nodes() +
  geom_edges()
ggplot(g1, aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_nodes() +
  geom_edges()