Network Visualization in ggplot2 Extensions

Keren Xu

2018/05/27

GGally

https://ggobi.github.io/ggally/

A tutorial on FLOWINGDATA gives example codes to create US basemap, to limit boundaries and to draw connections: http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/ How to draw a US map with all the states:

library(maps)
library(geosphere)
map("state") # this function can only provide a US map with 48 states

map("world")

xlim <- c(-171.738281, -56.601563)
ylim <- c(12.039321, 71.856229)
map("world", col="#f2f2f2", fill=, bg="pink", lwd=2, xlim=xlim, ylim=ylim) # if we do not set up the fill argument, then the map will not be filled with any color

map("world", col="#f2f2f2", fill=TRUE, bg="black", lwd=0.05, xlim=xlim, ylim=ylim) # we can add color to the background by using fill=TRUE and bg="color". However, we cannot choose the color for border for some reason. We can use lwd arguement to make border lines skinny.

devtools::install_github("ggobi/ggally")

Example: US airports

suppressMessages(library(GGally)) # make sure to load the package GGally instead of the name of ggally
suppressMessages(library(network))
suppressMessages(library(sna))
suppressMessages(library(maps))
suppressMessages(library(ggplot2))
airports <- read.csv("http://datasets.flowingdata.com/tuts/maparcs/airports.csv", header = TRUE)
head(airports)
rownames(airports) <- airports$iata
head(airports)

# select some random flights
set.seed(1234)
flights <- data.frame(
  origin = sample(airports[200:400, ]$iata, 200, replace = TRUE),
  destination = sample(airports[200:400, ]$iata, 200, replace = TRUE)
)
head(flights)

# convert to network
flights <- network(flights, directed = TRUE)

# add geographic coordinates
flights %v% "lat" <- airports[ network.vertex.names(flights), "lat" ]
flights %v% "lon" <- airports[ network.vertex.names(flights), "long" ]

# drop isolated airports
delete.vertices(flights, which(degree(flights) < 2))

# compute degree centrality
flights %v% "degree" <- degree(flights, gmode = "digraph")

# add random groups
flights %v% "mygroup" <- sample(letters[1:4], network.size(flights), replace = TRUE)

# create a map of the USA
(usa <- ggplot(map_data("usa"), aes(x = long, y = lat)) +
  geom_polygon(aes(group = group), color = "grey65",
               fill = "#f9f9f9", size = 0.2))

delete.vertices(flights, which(flights %v% "lon" < min(usa$data$long)))
delete.vertices(flights, which(flights %v% "lon" > max(usa$data$long)))
delete.vertices(flights, which(flights %v% "lat" < min(usa$data$lat)))
delete.vertices(flights, which(flights %v% "lat" > max(usa$data$lat)))

# overlay network data to map
ggnetworkmap(usa, flights, size = 4, great.circles = TRUE,
             node.group = mygroup, segment.color = "steelblue",
             ring.group = degree, weight = degree)

# geomnet https://github.com/sctyner/geomnet

data(twitter_spambots)
# create a world map
world <- fortify(map("world", plot = FALSE, fill = TRUE))
world <- ggplot(world, aes(x = long, y = lat)) +
  geom_polygon(aes(group = group), color = "grey65",
               fill = "#f9f9f9", size = 0.2)

# view global structure
ggnetworkmap(world, twitter_spambots)

# ggnetwork https://briatte.github.io/ggnetwork/

ggraph

https://github.com/thomasp85/ggraph