1. Learn
  2. /
  3. Courses
  4. /
  5. Case Studies: Network Analysis in R

Exercise

ggnetwork Basics

In the last lesson, you saw that the ggnet2 package produces ggplot2-like plots with a reasonably familiar syntax to igraph. However, the ggnetwork package works a bit differently. It converts igraph objects into data frames that are easily plotted by ggplot2. It also adds several new geoms that can be used to build plots. The ggnetwork() function converts the igraph object to a data frame, and some parameters are added into the data frame (in this case, the arrow gap parameter) and then can be plotted using ggplot. From there, you build your graph up with geom_edges() for edges and geom_nodes() for vertices. In this lesson we'll do two basic plots of the retweet graph, one with ggplot defaults, and one with some basic theming to look a bit nicer.

Instructions 1/2

undefined XP
  • 1
    • Draw a ggplot:
      • Convert retweet_samp with ggnetwork().
      • Specify the aesthetics x = x, y = y, xend = xend, and yend = yend.
    • Add a node layer with geom_nodes().
    • Add an edge layer with geom_edges().
  • 2
    • The graph is directed, so update the edge layer to use arrows:
      • Set arrow to arrow() with length set to unit(6, "pt").
    • Stop displaying the axes by adding a blank theme with theme_blank().