Last active
November 5, 2020 23:34
-
-
Save tylermorganwall/2f3ca112b9cd13972e02e1062670b735 to your computer and use it in GitHub Desktop.
John Snow's cholera clusters, visualized in 3D with rayshader and ggplot2
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
#theme and ggplot derived from David Kretch, his code: https://github.com/davidkretch/london_cholera_map/blob/master/london_cholera_map.R | |
library(HistData) | |
library(ggplot2) | |
library(ggpointdensity) | |
library(rayshader) | |
deaths = Snow.deaths | |
streets = Snow.streets | |
themeval = theme(panel.border = element_blank(), | |
panel.grid.major = element_blank(), | |
panel.grid.minor = element_blank(), | |
axis.line = element_blank(), | |
axis.ticks = element_blank(), | |
axis.text.x = element_blank(), | |
axis.text.y = element_blank(), | |
axis.title.x = element_blank(), | |
axis.title.y = element_blank(), | |
legend.key = element_blank(), | |
plot.margin = unit(c(0.5, 0, 0, 0), "cm")) | |
js = ggplot() + | |
geom_path(data=streets,aes(x=x, y=y, group = street), color="grey50") + | |
geom_pointdensity(data = deaths,aes(x=x, y=y), size=1,adjust=0.1) + | |
coord_fixed() + | |
scale_color_viridis_c() + | |
theme_bw() + | |
themeval | |
js2 = ggplot() + | |
geom_path(data = streets, aes(x=x, y=y, group = street), color="white") + | |
geom_pointdensity(data = deaths, aes(x=x, y=y), size=1, adjust=0.1) + | |
coord_fixed() + | |
scale_color_viridis_c() + | |
theme_bw() + | |
themeval | |
ggheight = plot_gg(list(js,js2), multicore = TRUE, raytrace=TRUE, | |
height_aes = "color", shadow_intensity = 0.3, | |
width=8,height=7, soliddepth = -100, save_height_matrix = TRUE, | |
background = "#f5e9dc", shadowcolor= "#4f463c",windowsize=c(1000,1000)) | |
#Add label | |
render_label(ggheight, "Water Pump", x=1110,y=1020,z=390, textsize = 2) | |
#Generate animation | |
for(i in 1:360) { | |
render_camera(phi=20,theta=45+i,fov=70,zoom=0.25) | |
render_depth(focus=0.77, | |
title_text = "John Snow's London cholera map, # of nearest neighbors", | |
title_size = 35, | |
filename = glue::glue("snow{i}")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone else chasing down a way to reproduce this, a few tips:
files <- fs::dir_ls("folder")
num <- as.numeric(str_extract(files, "\\d+")
frame <- tibble(files, num) %>% arrange(num)
gifski::gifski(frame$files, gif_file = "snow.gif", delay = 0.04, loop = TRUE, progress = TRUE)