Visualizing networks with ggplot2 in R

Created on Aug 19 2013
Revised on Mon Aug 19 14:10:22 2013

original post is here

suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(network))
suppressPackageStartupMessages(library(sna))
suppressPackageStartupMessages(library(ergm))
library(network)
library(ggplot2)
library(sna)
library(ergm)

plotg <- function(net, value = NULL) {
    m <- as.matrix.network.adjacency(net)  # get sociomatrix
    # get coordinates from Fruchterman and Reingold's force-directed placement
    # algorithm.
    plotcord <- data.frame(gplot.layout.fruchtermanreingold(m, NULL))
    # or get it them from Kamada-Kawai's algorithm: plotcord <-
    # data.frame(gplot.layout.kamadakawai(m, NULL))
    colnames(plotcord) = c("X1", "X2")
    edglist <- as.matrix.network.edgelist(net)
    edges <- data.frame(plotcord[edglist[, 1], ], plotcord[edglist[, 2], ])
    plotcord$elements <- as.factor(get.vertex.attribute(net, "elements"))
    colnames(edges) <- c("X1", "Y1", "X2", "Y2")
    edges$midX <- (edges$X1 + edges$X2)/2
    edges$midY <- (edges$Y1 + edges$Y2)/2
    pnet <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2), 
        data = edges, size = 0.5, colour = "grey") + geom_point(aes(X1, X2, 
        colour = elements), data = plotcord) + scale_colour_brewer(palette = "Set1") + 
        scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) + 
        # discard default grid + titles in ggplot2
    theme(panel.background = element_blank()) + theme(legend.position = "none") + 
        theme(axis.title.x = element_blank(), axis.title.y = element_blank()) + 
        theme(legend.background = element_rect(colour = NA)) + theme(panel.background = element_rect(fill = "white", 
        colour = NA)) + theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())
    return(print(pnet))
}


g <- network(50, directed = FALSE, density = 0.03)
classes <- rbinom(50, 1, 0.5) + rbinom(50, 1, 0.5) + rbinom(50, 1, 0.5)
set.vertex.attribute(g, "elements", classes)

plotg(g)

plot of chunk unnamed-chunk-1