networkD3

I’ve been working on a project that has a network analysis component, and am finally getting my head around how I can visualize the data. The package I’ve been using is networkD3, not to be confused with d3Network … same author but the former is now the actively developed version, and makes it easier to integrate the plots with something like Shiny.

The vignette is encouraging but kind of opaque, especially in terms of data preparation. With that said, it’s not too hard to wrangle your data frame into something that looks like the built in Les Miserables dataset.

simpleNetwork() takes a data frame, as well as “Source” and “Target” column specifications to create the node-link relationships. What’s worth noting here is that each row represents a relationship. So to fully visualize a network you need a “long” data frame that walks through every combination or connection.

The code below creates the plot above. First it reads in a dataset that contains the Museum of Modern Art (MOMA) collection database. For legibility, the data is filtered by the year 1945, and does not include “various” artist designations. Because this is data frame has a “long” format (i.e. it repeats artist name if he or she has been collected by multiple departments) you can use pass it straight into simple network plot. The “source” in this case will be the artist and the “target” will be the department, and all of which will be nodes that are visualized with a link if they appear in the same row.

library(networkD3)
library(dplyr)
library(readr)
library(lubridate)

# https://github.com/MuseumofModernArt/collection

moma <- read_csv("https://media.githubusercontent.com/media/MuseumofModernArt/collection/master/Artworks.csv")

moma$YearAcquired <- year(moma$DateAcquired)

moma1945 <-
    moma %>%
    # filter a year
    filter(YearAcquired == 1945) %>%
    # get rid of 'various' artist pieces
    filter(!grepl("Various", Artist)) %>%
    select(Artist, Department)

# clean up workspace
rm(moma)

simpleNetwork(moma1945, 
              Source = "Artist", 
              Target = "Department",  
              nodeColour = "firebrick",
              charge = -75,
              opacity = .5, 
              zoom = TRUE)

Here’s code for a Shiny app that creates visualizations very similar to the one above.