COM 411

```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) knitr::opts_knit$set(root.dir = './') source("resources/preamble.R") f <- function (x) {formatC(x, format="d", big.mark=',')} bold <- function(x) {paste('{\\textbf{',x,'}}', sep ='')} gray <- function(x) {paste('{\\textcolor{gray}{',x,'}}', sep ='')} wrapify <- function (x) {paste("{", x, "}", sep="")} p <- function (x) {formatC(x, format='f', digits=1, big.mark=',')} library(igraph) ```## Today's Dad Joke - Why did the near-sighted man fall in the well? > - He couldn't see that well # Network Data and Network Types ## Homework >- Did you find someone with a Hawaiian driver's license? >- How did it go? >- What were the challenges? >- Were there overlapping networks? # Networks in R ## What is R? ## Why are we using R? >- Powerful >- Reproducible >- Extensible ## Network Data >- What are the three main ways of representing networks? ## Matrices ```{r, include = T} sw = watts.strogatz.game(2, 4, 1, .2) as_adjacency_matrix(sw) ``` ## Edgelists ```{r, include = T} as_edgelist(sw) ``` ## Graphs/plots/sociograms ```{r, include = T} plot(sw, vertex.label.cex = .5 ) ``` ## Where does social network data come from? >- Surveys >- Observation >- Trace data # Network Types ## Ego networks >- Typically created from surveys >- Your family networks were ego networks ```{r, include = T} plot(make_ego_graph(sw, nodes = 1)[[1]]) ``` ## Extended ego networks ```{r} plot( make_ego_graph(sw, nodes = 1, order = 2)[[1]]) ``` ## Bipartite Networks >- 20 random reddittors and their communities ```{r, echo = F, message=F, echo=F} library(tidyverse) r = read_csv('../one_k_redittors.csv') r = r %>% filter(author %in% sample(unique(r$author), 50)) r = r %>% filter(! subreddit %in% r$author) G = graph.data.frame(r, directed = T) V(G)$type = TRUE V(G)[name %in% r$subreddit]$type = FALSE l = layout.bipartite(G, ) colors = c('steelblue', 'orange') shapes = c('circle', 'square') ``` ```{r, echo=F} plot_graph = function(g){ plot(g, layout = layout.fruchterman.reingold(g), #vertex.color = colors[V(g)$type + 1], #vertex.shape = shapes[V(g)$type + 1], vertex.size = 4, vertex.label.cex = .2 ) } G %>% induced_subgraph(unlist(sapply(1:20, subcomponent, graph = G, mode = 'out'))) %>% plot( layout = layout.bipartite(.), vertex.color = colors[V(.)$type + 1], vertex.shape = shapes[V(.)$type + 1], vertex.size = 4, vertex.label = "" #edge.width = log1p(E(G)$posts) ) ``` ## What does this data look like? ```{r} head(r) ``` ## Bipartite networks can be "projected" >- People who comment in the same communities ```{r} projections = bipartite_projection(G) sr = projections[[1]] people = projections[[2]] people %>% delete.vertices(degree(.) < 1) %>% plot_graph() ``` ## Projections ```{r} sr %>% delete.vertices(degree(.) < 1) %>% plot_graph() ``` ## Nodes and edges can have attributes ```{r} V(sw)$gender = sample(c(1,2,3), length(V(sw)), replace = T) colors <- c('green','orange','red') V(sw)$color <- colors[as.integer(V(sw)$gender)] plot(sw ) ``` ## Edge attributes >- Usually this is weight ```{r} #tmp = delete.edges(sr, which(E(sr)$weight < 2)) people %>% delete.vertices(degree(.) < 1) %>% plot( layout = layout_with_fr(.), vertex.size = 9, vertex.label.cex = .4, edge.width = E(.)$weight ) ``` ## Assignments > - Homework 3 (extended family network) > - Install R and RStudio (optional)