Case Studies: Network Analysis in R
Edmund Hart
Instructor
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)
# 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))
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)
# Convert the igraph object
nd3 <- igraph_to_networkD3(rand_g)
# Create a simple network
simpleNetwork(nd3$links)
# 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