Get startedGet started for free

Interactive javascript plots

Another widely used framework for creating interactive plots is D3.js. It has a specific standard for creating network plots that we can automatically generate in R. On the one hand, this is highly convenient because, with just a few lines of code, you'll be able to create fully interactive D3.js plots. The drawback is that real customization only comes when you directly edit the output javascript source code from R (which is beyond the scope of this course). Nonetheless, it is quick and easy to create a nice D3.js network plot in R using the d3network library. In this lesson we'll load up the #rstats Twitter dataset and add community membership. Then we'll create a subgraph of just a few communities and render a D3.js network graph.

The tweet graph object, retweet_samp, is available.

This exercise is part of the course

Case Studies: Network Analysis in R

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Run this to see the static version of the plot
ggplot(ggnetwork(retweet_samp, arrow.gap = 0.01), 
       aes(x = x, y = y, xend = xend, yend = yend)) + 
  	   geom_edges(color = "black") + 
	   geom_nodes(aes(color = as.factor(comm))) + 
       theme_blank()   

# Convert retweet_samp to a networkD3 object
nd3 <-igraph_to_networkD3(___, V(___)$___)

# View the data structure
str(nd3)
Edit and Run Code