Skip to content

Instantly share code, notes, and snippets.

@cavedave
Created May 20, 2017 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cavedave/bd7902b9b992907b3d0cef83105e3432 to your computer and use it in GitHub Desktop.
Save cavedave/bd7902b9b992907b3d0cef83105e3432 to your computer and use it in GitHub Desktop.
#Minor change to the code at https://robwhickman.github.io/2017/05/18/where-europeans-live/ to make an animation instead of a grid
saveGIF({
for (percent in seq(05,95,5)){
percentage <- percent
sortingDF <- level3@data %>% select(NUTS_ID, pop2014, Density)
totalpop <- sum(sortingDF$pop2014, na.rm = TRUE)
#sort by density (i.e. find smallest area for max pop)
sortingDF <- sortingDF %>% arrange(-Density) %>%
mutate(cumsum = cumsum(pop2014))
sortingDF <- sortingDF %>%
mutate(lowhigh = ifelse(cumsum > totalpop*(percentage /100),
"low density", "high density")) %>%
select(NUTS_ID, lowhigh)
level3@data <- left_join(level3@data[1:6], sortingDF, by = "NUTS_ID")
#make plot
print(ggplot() +
geom_map(data = EUmap, map = EUmap,
aes(x = long, y = lat, map_id = id),
color = "black", size = 1, fill = NA) +
geom_map(data = level3@data, map = EUmap,
aes(fill = lowhigh, map_id = NUTS_ID),
color = NA) +
theme_map() +
labs(title= paste0("Map Showing ", percentage,
"% of the European Population in Yellow")) +
scale_fill_manual(values = c("yellow", "darkblue"),
name="Population Density") +
theme(legend.position = "right")
)}
}, interval=0.25,ani.width = 900, ani.height = 600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment