Created
June 16, 2017 17:04
-
-
Save vpnagraj/b6c79f1a9b51166668649fa7ff28d3fb to your computer and use it in GitHub Desktop.
script to demonstrate outbreak animation with a subset of mers_korea_2015 data
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
# script to demonstrate outbreak animation with a subset of mers_korea_2015 data | |
# must install github release of threejs package | |
# devtools::install_github("bwlewis/rthreejs") | |
library(threejs) | |
library(outbreaks) | |
library(dplyr) | |
# use dplyr to subset results to only include hospital visit exposure | |
# prep edge attributes | |
hospvisit <- | |
mers_korea_2015$contacts %>% | |
filter(exposure == "Visit hospital") %>% | |
select(from,to) | |
# prep node attributes | |
hospvisitll <- | |
mers_korea_2015$linelist %>% | |
filter(id %in% hospvisit$from | id %in% hospvisit$to) %>% | |
select(id) | |
# create graph object from data frame | |
g <- graph_from_data_frame(hospvisit, vertices = hospvisitll) | |
# all data | |
g <- graph_from_data_frame(mers_korea_2015$contacts, vertices = mers_korea_2015$linelist) | |
# remove edges from graph | |
gnoe <- g - edges(1:ecount(g)) | |
buildexp <- function(x) { | |
foo <- paste0(expression(ends(g, es = E(g), names = FALSE)), "[", x,",]", collapse = "") | |
foo <- paste0(expression(edge), "(", foo, ")") | |
foo | |
# edge(eval(parse(text = foo))) | |
} | |
l <- lapply(1:ecount(g), buildexp) | |
graphlist <- list(gnoe) | |
for (i in 1:length(l)) { | |
graphlist[[i + 1]] <- eval(parse(text = paste(expression(gnoe), paste(l[1:i], collapse = " + "), sep = " + "))) | |
} | |
graphjs(graphlist, | |
vertex.shape="sphere", | |
vertex.label = names(V(gnoe)), | |
edge.width=3, | |
fpl = 100, | |
cumulative = T) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment