Get startedGet started for free

Other packages for plotting graphs!

1. Other packages for plotting graphs

So far we've done all our plotting with igraph. While igraph's base plotting works great for quick visualization, other libraries will let you make high quality graphics with less effort. It's important to be aware that there are other libraries for plotting, but in this lesson we'll focus on two that are based on the popular ggplot2 framework, ggnet2 and ggnetwork.

2. Generating data to plot

First we'll generate a random graph, and then add a few attributes. In this case we'll add centrality and community attributes to each vertex. This will give us some extra dimensions to visualize.

3. Generate data to plot

Here is the the basic plot for reference. Keep that in mind as we look at the other basic plots.

4. Basic ggnet2

ggnet2 function calls are similar to igraph. You won't notice much difference between these two frameworks until it comes time to plot a graph with more attributes. Also it doesn't work natively with igraph objects. Instead you'll need to use the asNetwork() function from the intergraph package to convert to network format. In practice this will have very little impact on your plotting.

5. Basic ggnet2

Here we can see the default ggnet2 plot, it doesn't look that different from the igraph default plot.

6. Basic ggnetwork

ggnetwork is a bit different than ggnet2. Using the ggnetwork() function you convert the igraph object into a dataframe that can easily be plotted in ggplot2. If you're familiar with ggplot2, using this package with feel quite natural. When we look at the dataframe that's created, we can see that it has x, y,xend, and yend columns, which allow graphs to be created using ggplot2 segments and curves. It also adds new geoms to use, specifically geom_nodes() and geom_edges(). These new geoms, will specify the aesthetics for edges and vertices.

7. Basic ggnetwork

Just like ggnet2, the plot looks basic, it's when we want to visualize other attributes of the graph that these libraries start to make your life easier.

8. Plotting graphs with attributes

Now is when things start getting complicated with igraph plots, especially when adding the legends. We don't need to walk through all the details here, what's important is to note just how much code goes into creating this figure.

9. igraph plot with attributes

We've now made a plot of our random graph with centrality coded as size, and community membership shown by vertex color.

10. ggnet2 plot with attributes

Using ggnet2 we can make the same plot, all we do is specify vertex size and color with the node-dot-size and node-dot-color parameters. We can easily give it a color palette, have the size cutting by default, and provide labels for each legend.

11. ggnet2 plot with attributes

With far fewer lines of code, we've recreated the annotated igraph plot.

12. ggnetwork plot with attributes

Because we are using ggplot2 with just a specific data frame format, we specify colors and size just like we would any ggplot2 call. Within the aes() calls in each geom, we map color and size. In this case because we want to add colors for community membership and size for centrality, we just specify that within geom_nodes(). To draw edge colors by community membership we do the same within geom_edges. Again we specify legend parameters the exact same way as any ggplot2 call.

13. ggnetwork plot with attributes

Just like with ggnet2, we can use a single line of code to recreate a figure that in igraph took 6 lines.

14. Let's practice!

We've just scratched the surface on all the different ways you can plot graphs in R, but these two packages based on ggplot2 give you some powerful options to create presentation or publication quality graphics more easily than using the base igraph plotting functions. Now lets try these libraries on some of the datasets we've already used.