Skip to content
forked from briatte/ggnet

Simple network plots with ggplot2 in R.

Notifications You must be signed in to change notification settings

falconzyx/ggnet

 
 

Repository files navigation

ggnet: simple network plots with ggplot2

French MPs on Twitter

A plot of Twitter connexions between 339 French MPs currently in office, colored by parliamentary groups and quartile-weighted by degree. See functions.R for network exploration routines. Data assembled by scraping a few web sources in May 2013 with the help of Jonathan Chibois and Benjamin Ooghe-Tabanou from Regards Citoyens. My blog post at Polit'bistro has more details.

Syntax

The functions builds on Moritz Marbach's plotg() function and accepts the following arguments:

ggnet(net,                          # an object of class network
      mode = "fruchtermanreingold", # placement algorithm
      size = 12,                    # node size
      alpha = .75,                  # transparency
      weight.method = "none",       # what to weight the nodes with: "degree", "indegree", "outdegree"
      names = c("", ""),            # what to call the node color and node weight legends
      node.group = NULL,            # what to color the nodes with
      node.color = NULL,            # what colors to use for the node classes
      node.alpha = NULL,            # transparency for nodes (inherits from alpha)
      segment.alpha = NULL,         # transparency for links (inherits from alpha)
      segment.color = "grey",       # default links are rgb(190, 190, 190)
      segment.size  = .25,          # set to 0 to remove from plot
      arrow.size = 0,               # set to 0 to remove from plot
      label.nodes = FALSE,          # add vertex names in small print; can be a list of vertex names
      top8.nodes  = FALSE,          # color the top 8 nodes by weighting method with ColorBrewer Set1
      trim.labels = TRUE,           # remove '@', 'http://' and 'www.' from vertex names
      quantize.weights = FALSE,     # break weights to quartiles
      subset.threshold = 0,         # what nodes to exclude, based on weight.method ≥ subset
      legend.position = "right",    # set to "none" to remove from plot
      ...)                          # arguments passed to node labels with geom_text()

The function needs an object of class network and automatically handles the conversion of objects of class igraph by calling the intergraph package. It supports all placement algorithms available through the sna package. The code has been submitted to the GGally package.

Options

The ggnet() function returns a ggplot object in which nodes are represented by points that can be colored and/or weighted using proportional scaling. The network above can therefore be set to look like this when the segments are not drawn and the nodes are weighted by indegree and left uncolored:

ggnet(net, 
      size = 6, 
      segment.size = 0, 
      weight = "indegree", 
      legend = "none") + 
  geom_density2d()

The node colors are set through a group variable colored by a discrete palette. Node groups can be any vector containing as many items as there are nodes in the network. Hence, to verify that the dual structure shown above corresponds to the left-right party divide, we group nodes by a logical value and let the function select from the default Set1 scheme to discriminate them:

rightwing = ifelse(mp.groups == "NI", NA, mp.groups %in% c("UDI", "UMP"))
ggnet(net, 
      node.group = rightwing, 
      alpha = .25, 
      name = "Rightwing group")

The function can also label all or a selection of nodes, identified by vertex names. See, for example, how party polarization is much less obvious when you look at a single individual's network (Nathalie Kosciusko-Morizet in this example, the rightwing candidate for the mayor of Paris in the next municipal election):

follows.nkm = ids$Twitter %in% who.follows(df, "nk_m")$followers
ggnet(net, 
      size = 6, 
      node.group = follows.nkm, 
      alpha = .25, 
      name = "Follows NKM",
      label = "nk_m", 
      color = "black")

The function contains a few examples with small random networks, as well as an example using the city and service firms dataset that is also available in Pajek. The visualization below uses the Kamada-Kawai placement algorithm with no node weighting, a few node labels and a default three-color scheme built from ColorBrewer's Set1:

url = url("http://networkdata.ics.uci.edu/netdata/data/cities.RData")
print(load(url))
close(url)
type = network::get.vertex.attribute(cities, "type")
type = ifelse(grepl("City|Law", type), gsub("I+", "", type), "Firm")
ggnet(cities,
      mode = "kamadakawai",
      alpha = .2,
      node.group = type,
      label = c("Paris", "Beijing", "Chicago"),
      color = "darkred")

Finally, some of the more experimental arguments like subset.threshold and top8.nodes, which highlight the most central nodes of a network, are shown in the code for a conference paper using online network data collected with VOSON:

Inspired by Baptiste Coulmont and Ewen Gallic.

About

Simple network plots with ggplot2 in R.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published