Interactive visualizations

Case Studies: Network Analysis in R

Edmund Hart

Instructor

Generating some data

library(igraph)
library(ggnetwork)
library(ggiraph)
library(htmlwidgets)
library(networkD3)

# Create random graph
rand_g <- erdos.renyi.game(30, 0.12, "gnp", directed = FALSE)
rand_g <- simplify(rand_g)

V(rand_g)$cent <- betweenness(rand_g)
Case Studies: Network Analysis in R

Interactive plots with ggiraph

# Plot graph with ggplot2 and ggnetwork
g <- ggplot(ggnetwork(rand_g), 
            aes(x = x, y = y, xend = xend, yend = yend)) + 
     geom_edges(color = "black") +
     geom_nodes(aes(size = cent))+ theme_blank() + 
     guides(size = guide_legend(title = "Centrality"))

# Create ggiraph object my_gg <- g + geom_point_interactive(aes(tooltip = round(cent, 2)), size = 2)
# Display ggiraph object ggiraph(code = print(my_gg))
Case Studies: Network Analysis in R

Case Studies: Network Analysis in R

ggiraph customization

my_gg <- g + geom_point_interactive(aes(tooltip = round(cent, 2),
                                        data_id = round(cent, 2)), 
                                    size = 2) 

hover_css = "cursor:pointer;fill:red;stroke:red;r:5pt"

ggiraph(code = print(my_gg),
        hover_css = hover_css,
        tooltip_offx = 10, 
        tooltip_offy = -10)
Case Studies: Network Analysis in R

Case Studies: Network Analysis in R

Plotting with networkD3

# Convert the igraph object
nd3 <- igraph_to_networkD3(rand_g)

# Create a simple network
simpleNetwork(nd3$links)

Case Studies: Network Analysis in R

More complex networkD3

# Add attributes, group is community, and cent is centrality.
nd3$nodes$group = V(rand_g)$comm
nd3$nodes$cent = V(rand_g)$cent

# Plot the graph forceNetwork(Links = nd3$links, Nodes = nd3$nodes, Source = 'source', Target = 'target', NodeID = 'name', Group = 'group', Nodesize = 'cent', legend = T, fontSize = 20)
Case Studies: Network Analysis in R

Case Studies: Network Analysis in R

Let's practice!

Case Studies: Network Analysis in R

Preparing Video For Download...