Examples pulled from **Network Visualization with R**, [Polnet 2016](http://www.kateto.net/polnet2016) by Katya Ognyanova. ```{r} suppressPackageStartupMessages({ library(RColorBrewer) library(readr) library(dplyr) library(igraph) }) ``` ```{r} data_dir <- "Data files" ``` For igraph plotting parameters, node options start with `vertex.` and edge options start with `edge.`. Get options with ```{r eval=FALSE} ?igraph.plotting ``` Set node & edge options in two ways: 1. specify them in `plot()` function 2. add them to the `igraph` object ### Basic igraph with Dataset 1 ```{r} nodes <- read_csv(file.path(data_dir, "Dataset1-Media-Example-NODES.csv")) links <- read_csv(file.path(data_dir, "Dataset1-Media-Example-EDGES.csv")) %>% group_by(from,to,type) %>% summarize(weight = sum(weight)) %>% ungroup %>% arrange(from,to) ``` Convert dataset 1 to an igraph object. The `graph_from_data_frame()` function takes two data frames, `d` (edges of the network) and `vertices` (nodes). `d` has (at least) two columns with source and target node IDs for each network tie. `vertices` has (at least) a column of node IDs. Additional columns in either data frame are interpreted as attributes. ```{r} (net <- graph_from_data_frame(d=links, vertices=nodes, directed=T)) ``` Access nodes, edges, and attributes: ```{r} E(net) ``` ```{r} V(net) ``` ```{r} E(net)$type ``` ```{r} V(net)$media ``` Find specific nodes and edges by attribute ```{r} V(net)[media=="BBC"] ``` ```{r} E(net)[type=="mention"] ``` Extract an edge list or matrix from igraph networks. ```{r} tbl_df(as_edgelist(net, names=T)) ``` ```{r} as_adjacency_matrix(net, attr="weight") ``` Look at network matrix directly: ```{r} net[1,] ``` ```{r} net[5,7] ``` Plot graph, removing loops and labels, and reducing arrow size. ```{r} net <- simplify(net, remove.multiple = F, remove.loops = T) ``` ```{r} plot(net, edge.arrow.size = 0.4, vertex.label = NA) ``` ### Annotating a graph #### Using options to plot Plot with curved edges (`edge.curved=.1`) and reduce arrow size. Using curved edges reveals multiple links between two nodes (e.g. links going in either direction, or multiplex links). ```{r} plot(net, edge.arrow.size=.4, edge.curved=.1) ``` Set node color to `orange` and the border color to hex `#555555`. Replace the vertex label with the node names stored in `media`. ```{r} plot(net, edge.arrow.size=.4, edge.curved=0, vertex.color="orange", vertex.frame.color="#555555", vertex.label=V(net)$media, vertex.label.color="black", vertex.label.cex=.7) ``` Semantic network with only node labels. ```{r} plot(net, vertex.shape="none", vertex.label=V(net)$media, vertex.label.font=2, vertex.label.color="gray40", vertex.label.cex=0.7, edge.color="gray90") ``` #### Using graph attributes ```{r} # Generate colors based on media type: colrs <- c("gray50", "tomato", "gold") V(net)$color <- colrs[V(net)$media.type] # Compute node degree (#links) and use it to set node size: deg <- degree(net, mode="all") V(net)$size <- deg*3 V(net)$size <- V(net)$audience.size*0.6 # The labels are currently node IDs. # Setting them to NA will render no labels: V(net)$label.color <- "black" V(net)$label <- NA # Set edge width based on weight: E(net)$width <- E(net)$weight/6 #change arrow size and edge color: E(net)$arrow.size <- .2 E(net)$edge.color <- "gray80" plot(net) ``` Override attributes explicitly in the plot ```{r} plot(net, edge.color="orange", vertex.color="gray50") ``` #### Legend Add legend explaining color meaning. * `x,y`: legend coordinates * `pch`: element symbol * `pt.bg`: background color * `col`: border color, * `pt.cex`: symbol size * `bty`: type of box around legend * `ncol`: number of columns for legend ```{r} plot(net) legend(x = -1.1, y = -1.1, c("Newspaper","Television", "Online News"), pch = 21, col = "#777777", pt.bg = colrs, pt.cex = 2.5, bty = "n", ncol = 1) ``` ### Highlight Graph Aspects Color graph edges based on source node color. Here `ends()` gives us the start and end for each edge in `es`, and `names` controls whether `ends()` will return node names or IDs. ```{r} edge.start <- ends(net, es=E(net), names=F)[,1] # get the "from" node edge.col <- V(net)$color[edge.start] plot(net, edge.color = edge.col, edge.curved = .1) ``` Keep only edges with weight higher than network mean using `delete_edges(net, edges)`. ```{r} cut.off <- mean(links$weight) net.sp <- delete_edges(net, E(net)[weight