Created
August 8, 2018 01:43
-
-
Save beemyfriend/8923d8c8eeb7720a3fd7fff90443e7a2 to your computer and use it in GitHub Desktop.
Simple Contagion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(igraph) | |
| library(animation) | |
| #===============================# | |
| #===== Simple ==================# | |
| #===============================# | |
| saveGIF({ | |
| ani.options(ani.width = 800, | |
| ani.width = 800, | |
| interval = 1) | |
| set.seed(4321) | |
| g <- sample_islands(5, 25, 5/10, 3) | |
| l <- layout_nicely(g) | |
| #Vertex | |
| V(g)$name = paste0('n_', V(g)) | |
| V(g)$size = 5 | |
| V(g)$label.cex = 1 | |
| V(g)$activated = F | |
| V(g)['n_1']$activated = T | |
| V(g)$label = '' | |
| #Edge | |
| E(g)$width = .5 | |
| E(g)$color = '#E0E0E0' | |
| par(bg = "#252525") | |
| plot(g, | |
| layout = l, | |
| vertex.color = ifelse(V(g)$activated, '#FFFF40', '#C040C0'), | |
| bg = 'grey', | |
| asp = 0 | |
| ) | |
| title("Simple Contagion: Step 0", col.main = "#E0E0E0") | |
| lapply(seq(4), function(i){ | |
| activated <- V(g)[activated]$name %>% | |
| as.character() | |
| E(g)$fromActivated = tail_of(g, E(g))$activated | |
| E(g)$toActivated = head_of(g, E(g))$activated | |
| sub_graph <- g %>% | |
| {. - E(.)[!(fromActivated | toActivated)]} %>% | |
| {. - V(.)[degree(.) == 0]} | |
| toActivate <- V(sub_graph) %>% | |
| .[!name %in% activated] %>% | |
| .$name | |
| print(toActivate) | |
| V(g)[toActivate]$activated <- T | |
| g <<- g | |
| par(bg = "#252525") | |
| plot(g, | |
| layout = l, | |
| vertex.color = ifelse(V(g)$activated, '#FFFF40', '#C040C0'), | |
| bg = 'grey', | |
| asp = 0 | |
| ) | |
| title(paste0("Simple Contagion: Step ", i), col.main = "#E0E0E0") | |
| }) | |
| }, movie.name = 'simple_contagion.gif') | |
| #===============================# | |
| #===== Simple Tolerance=========# | |
| #===============================# | |
| saveGIF({ | |
| ani.options(ani.width = 800, | |
| ani.width = 800, | |
| interval = 1) | |
| set.seed(4321) | |
| g <- sample_islands(5, 25, 5/10, 3) | |
| l <- layout_nicely(g) | |
| #Vertex | |
| V(g)$name = paste0('n_', V(g)) | |
| V(g)$tolerance = sample(1:4, vcount(g), replace = T) | |
| V(g)$size = 5 | |
| V(g)$label.cex = 1 | |
| V(g)$activated = F | |
| V(g)['n_1']$activated = T | |
| #Edge | |
| E(g)$width = .5 | |
| E(g)$color = '#E0E0E0' | |
| par(bg = "#252525") | |
| plot(g, | |
| layout = l, | |
| vertex.label = V(g)$tolerance, | |
| vertex.label.color = ifelse(V(g)$activated, '#252525', '#E0E0E0'), | |
| vertex.color = ifelse(V(g)$activated, '#FFFF40', '#C040C0'), | |
| bg = 'grey', | |
| asp = 0 | |
| ) | |
| title("Simple Contagion \\w Tolerance: Step 0", col.main = "#E0E0E0") | |
| lapply(seq(9), function(i){ | |
| activated <- V(g)[activated]$name %>% | |
| as.character() | |
| E(g)$fromActivated = tail_of(g, E(g))$activated | |
| E(g)$toActivated = head_of(g, E(g))$activated | |
| sub_graph <- g %>% | |
| {. - E(.)[!(fromActivated | toActivated)]} %>% | |
| {. - V(.)[degree(.) == 0]} | |
| toActivate <- V(sub_graph) %>% | |
| .[tolerance <= degree(sub_graph)] %>% | |
| .[!name %in% activated] %>% | |
| .$name | |
| print(toActivate) | |
| V(g)[toActivate]$activated <- T | |
| g <<- g | |
| par(bg = "#252525") | |
| plot(g, | |
| layout = l, | |
| vertex.label = V(g)$tolerance, | |
| vertex.label.color = ifelse(V(g)$activated, '#252525', '#E0E0E0'), | |
| vertex.color = ifelse(V(g)$activated, '#FFFF40', '#C040C0'), | |
| bg = 'grey', | |
| asp = 0 | |
| ) | |
| title(paste0("Simple Contagion \\w Tolerance: Step ", i), col.main = "#E0E0E0") | |
| }) | |
| }, movie.name = 'simple_contagion_tolerance.gif') | |
| #===============================# | |
| #== Simple Tolerance Recov======# | |
| #===============================# | |
| saveGIF({ | |
| ani.options(ani.width = 800, | |
| ani.width = 800, | |
| interval = 1) | |
| set.seed(4321) | |
| g <- sample_islands(5, 25, 5/10, 3) | |
| l <- layout_nicely(g) | |
| #Vertex | |
| V(g)$name = paste0('n_', V(g)) | |
| V(g)$tolerance = sample(1:4, vcount(g), replace = T) | |
| V(g)$size = 5 | |
| V(g)$label.cex = 1 | |
| V(g)$activated = F | |
| V(g)['n_1']$activated = T | |
| V(g)$timeSince = 0 | |
| V(g)['n_1']$timeSince = 1 | |
| #Edge | |
| E(g)$width = .5 | |
| E(g)$color = '#E0E0E0' | |
| par(bg = "#252525") | |
| plot(g, | |
| layout = l, | |
| vertex.label = V(g)$tolerance, | |
| vertex.label.color = ifelse(V(g)$activated, '#252525', '#E0E0E0'), | |
| vertex.color = ifelse(V(g)$activated, '#FFFF00', '#C040C0'), | |
| bg = 'grey', | |
| asp = 0 | |
| ) | |
| title("Simple Contagion \\w Tolerance & Recovery After 2: Step 0", col.main = "#E0E0E0") | |
| lapply(seq(15), function(i){ | |
| activated <- V(g)[activated]$name %>% | |
| as.character() | |
| E(g)$fromActivated = tail_of(g, E(g))$activated | |
| E(g)$toActivated = head_of(g, E(g))$activated | |
| sub_graph <- g %>% | |
| {. - E(.)[!(fromActivated | toActivated)]} %>% | |
| {. - V(.)[degree(.) == 0]} | |
| toActivate <- V(sub_graph) %>% | |
| .[tolerance <= degree(sub_graph)] %>% | |
| .$name | |
| V(g)[toActivate]$timeSince <- V(g)[toActivate]$timeSince + 1 | |
| V(g)[timeSince > 2]$timeSince <- 0 | |
| V(g)$activated <- ifelse(V(g)$timeSince > 0, T, F) | |
| g <<- g | |
| par(bg = "#252525") | |
| plot(g, | |
| layout = l, | |
| vertex.label = V(g)$tolerance, | |
| vertex.label.color = ifelse(V(g)$activated, '#252525', '#E0E0E0'), | |
| vertex.color = sapply(V(g)$timeSince, function(x){ | |
| if(x == 1) return('#FFFF00') | |
| if(x == 2) return ('#FFFFC0') | |
| return('#C040C0') | |
| }), | |
| bg = 'grey', | |
| asp = 0 | |
| ) | |
| title(paste0("Simple Contagion \\w Tolerance & Recovery After 2: Step ", i), col.main = "#E0E0E0") | |
| }) | |
| }, movie.name = 'simple_contagion_tolerance_and_recovery.gif') | |
| #===============================# | |
| #=Simple Raised Tolerance Recov=# | |
| #===============================# | |
| saveGIF({ | |
| ani.options(ani.width = 800, | |
| ani.width = 800, | |
| interval = 1) | |
| set.seed(4321) | |
| g <- sample_islands(5, 25, 5/10, 3) | |
| l <- layout_nicely(g) | |
| #Vertex | |
| V(g)$name = paste0('n_', V(g)) | |
| V(g)$tolerance = sample(1:4, vcount(g), replace = T) | |
| V(g)$size = 5 | |
| V(g)$label.cex = 1 | |
| V(g)$activated = F | |
| V(g)['n_1']$activated = T | |
| V(g)$timeSince = 0 | |
| V(g)['n_1']$timeSince = 1 | |
| #Edge | |
| E(g)$width = .5 | |
| E(g)$color = '#E0E0E0' | |
| par(bg = "#252525") | |
| plot(g, | |
| layout = l, | |
| vertex.label = V(g)$tolerance, | |
| vertex.label.color = ifelse(V(g)$activated, '#252525', '#E0E0E0'), | |
| vertex.color = ifelse(V(g)$activated, '#FFFF00', '#C040C0'), | |
| bg = 'grey', | |
| asp = 0 | |
| ) | |
| title("Simple Contagion \\w Improving Tolerance & Recovery After 2: Step 0", col.main = "#E0E0E0") | |
| lapply(seq(18), function(i){ | |
| activated <- V(g)[activated]$name %>% | |
| as.character() | |
| E(g)$fromActivated = tail_of(g, E(g))$activated | |
| E(g)$toActivated = head_of(g, E(g))$activated | |
| sub_graph <- g %>% | |
| {. - E(.)[!(fromActivated | toActivated)]} %>% | |
| {. - V(.)[degree(.) == 0]} | |
| toActivate <- V(sub_graph) %>% | |
| .[tolerance <= degree(sub_graph)] %>% | |
| .$name | |
| V(g)[toActivate]$timeSince <- V(g)[toActivate]$timeSince + 1 | |
| V(g)[timeSince > 2]$tolerance <- V(g)[timeSince > 2]$tolerance + 2 | |
| V(g)[timeSince > 2]$timeSince <- 0 | |
| V(g)$activated <- ifelse(V(g)$timeSince > 0, T, F) | |
| g <<- g | |
| par(bg = "#252525") | |
| plot(g, | |
| layout = l, | |
| vertex.label = V(g)$tolerance, | |
| vertex.label.color = ifelse(V(g)$activated, '#252525', '#E0E0E0'), | |
| vertex.color = sapply(V(g)$timeSince, function(x){ | |
| if(x == 1) return('#FFFF00') | |
| if(x == 2) return ('#FFFFC0') | |
| return('#C040C0') | |
| }), | |
| bg = 'grey', | |
| asp = 0 | |
| ) | |
| title(paste0("Simple Contagion \\w Improving Tolerance & Recovery After 2: Step ", i), col.main = "#E0E0E0") | |
| }) | |
| }, movie.name = 'simple_contagion_improve_tolerance_and_recovery.gif') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment