Skip to content

Instantly share code, notes, and snippets.

@wolfganghuber
Forked from aoles/bombs.Rmd
Last active July 2, 2018 09:11
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 wolfganghuber/523622aff6a156d26e77a87ccbe7bd0d to your computer and use it in GitHub Desktop.
Save wolfganghuber/523622aff6a156d26e77a87ccbe7bd0d to your computer and use it in GitHub Desktop.
Visualization Blitz Bombs on map of London - Fig. 5.2 of Modern Statistics for Modern Biology. By Andrzej Oles. A rendered version is shown here: http://rpubs.com/WolfgangHuber/401178
---
title: "London Blitz Bombs"
author: "Andrzej K. Oleś (main), Wolfgang Huber (contributions)"
output: BiocStyle::html_document
---
<font style="color:red"> Is there a good way to list the dependencies (packages) upfront?</font>
This gist produces a visualization of the location of the bombs that were dropped on London in the
night of September, 7th, 1940 based on data provided by the Guardian Data Store
& London Fire Brigade Records referenced by the website of the British National
Archives http://bombsight.org.
This is an alternative method to arrive at something like Figure 5.2 of the book [*Modern Statistics for Modern Biology*](http://www.huber.embl.de) by Susan Holmes and Wolfgang Huber.
Instead of running Steps 1 and 2, you can also use the precomputed dataframe `coordinates` and charge ahead to Step 3 directly.
# Download the street addresses
```{r data_source, message = FALSE, warning = FALSE, eval = !file.exists("Blitz-19400907-latlng.RData")}
library("googlesheets")
addresses_url <- "https://docs.google.com/spreadsheets/d/1rL68hnF9bHHg3p72ti_reSvwK12VEdABN5Q7YADXm3I"
addresses <- gs_read(gs_url(addresses_url))
```
# Query Google Maps to convert addresses into (latitude, longitude) coordinates
In order to query Google Maps, you need to set up the API key first, see `?googleway::set_key`. As an alternative, if you don't want to do this, you can use the dataframe `coordinates` that we provide along with this gist, see next step.
```{r geocode, eval = !file.exists("Blitz-19400907-latlng.RData")}
coordinates <- lapply(addresses$Location, function(loc) {
x <- googleway::google_geocode(loc)
x$results$geometry$location
}) %>% dpylr::bind_rows
save(coordinates, file = "Blitz-19400907-latlng.RData")
```
# Alternatively, load a precomputed dataframe
This step is alternative to Steps 1 and 2.
```{r load_data}
load("Blitz-19400907-latlng.RData")
```
# Display the locations on an online map and make a screenshot
We use the PhantomJS browser in order to do this. If it is not yet installed on your system, run the following chunk to do so:
```{r install_phantomjs, eval = FALSE}
## can we automaticallh check whether this needs to be run?
webshot::install_phantomjs()
```
Now we are ready to go:
```{r leaflet, message = FALSE}
library("leaflet")
bombsmap <- leaflet(coordinates) %>%
addProviderTiles(providers$OpenStreetMap) %>%
addCircleMarkers(color = "red",
radius = 5,
stroke = FALSE,
fillOpacity = 0.3) %>%
setView(-0.1278, 51.5074, 12)
mapview::mapshot(bombsmap, file = "Blitz-19400907-latlng.png")
```
And here is the image:
![Blitz-19400907-latlng.png](Blitz-19400907-latlng.png)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment