# Interactive Maps for John Snow’s Cholera Data

**Freakonometrics » R-english**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This week, in Istanbul, for the second training on data science, we’ve been discussing classification and regression models, but also visualisation. Including maps. And we did have a brief introduction to the leaflet package,

devtools::install_github("rstudio/leaflet") require(leaflet)

To see what can be done with that package, we will use one more time the John Snow’s cholera dataset, discussed in previous posts (one to get a visualisation on a google map background, and the second one on an openstreetmap background),

library(sp) library(rgdal) library(maptools) setwd("/cholera/") deaths <- readShapePoints("Cholera_Deaths") df_deaths <- data.frame(deaths@coords) coordinates(df_deaths)=~coords.x1+coords.x2 proj4string(df_deaths)=CRS("+init=epsg:27700") df_deaths = spTransform(df_deaths,CRS("+proj=longlat +datum=WGS84")) df=data.frame(df_deaths@coords) lng=df$coords.x1 lat=df$coords.x2

Once installed the leaflet package, we can use the package at the RStudio console (which is what we will do here), or within R Markdown documents, and within Shiny applications. But because of restriction we got on this blog (rules of hypotheses.org) So there will be only copies of my screen. But if you run the code, in RStudio you will get interactvive maps in the **viewer** window.

First step. To load a map, centered initially in London, use

m = leaflet()%>% addTiles() m %>% fitBounds(-.141, 51.511, -.133, 51.516)

In the **viewer** window of RStudio, it is just like on OpenStreetMap, e.g. we can zoom-in, or zoom-out (with the standard + and – in the top left corner)

And we can add additional material, such as the location of the deaths from cholera (since we now have the same coordinate representation system here)

rd=.5 op=.8 clr="blue" m = leaflet() %>% addTiles() m %>% addCircles(lng,lat, radius = rd,opacity=op,col=clr)

We can also add some heatmap.

X=cbind(lng,lat) kde2d <- bkde2D(X, bandwidth=c(bw.ucv(X[,1]),bw.ucv(X[,2])))

But there is no heatmap function (so far) so we have to do it manually,

x=kde2d$x1 y=kde2d$x2 z=kde2d$fhat CL=contourLines(x , y , z)

We have now a list that contains lists of polygons corresponding to isodensity curves. To visualise of of then, use

m = leaflet() %>% addTiles() m %>% addPolygons(CL[[5]]$x,CL[[5]]$y,fillColor = "red", stroke = FALSE)

Of course, we can get at the same time the points *and* the polygon

m = leaflet() %>% addTiles() m %>% addCircles(lng,lat, radius = rd,opacity=op,col=clr) %>% addPolygons(CL[[5]]$x,CL[[5]]$y,fillColor = "red", stroke = FALSE)

We can try to plot many polygons on the map, as different layers, to visualise some kind of heatmaps, but it’s not working that well

m = leaflet() %>% addTiles() m %>% addCircles(lng,lat, radius = rd,opacity=op,col=clr) %>% addPolygons(CL[[1]]$x,CL[[1]]$y,fillColor = "red", stroke = FALSE) %>% addPolygons(CL[[3]]$x,CL[[3]]$y,fillColor = "red", stroke = FALSE) %>% addPolygons(CL[[5]]$x,CL[[5]]$y,fillColor = "red", stroke = FALSE) %>% addPolygons(CL[[7]]$x,CL[[7]]$y,fillColor = "red", stroke = FALSE) %>% addPolygons(CL[[9]]$x,CL[[9]]$y,fillColor = "red", stroke = FALSE)

An alternative is to hightlight (only) the contour of the polygon

m = leaflet() %>% addTiles() m %>% addCircles(lng,lat, radius = rd,opacity=op,col=clr) %>% addPolylines(CL[[1]]$x,CL[[1]]$y,color = "red") %>% addPolylines(CL[[5]]$x,CL[[5]]$y,color = "red") %>% addPolylines(CL[[8]]$x,CL[[8]]$y,color = "red")

Again, the goal of those functions is to get some maps we can zoom-in, or -out (what I call interactive)

And we can get at the same time the contour but also some polygon filled with some light red color

m = leaflet() %>% addTiles() m %>% addCircles(lng,lat, radius = rd,opacity=op,col=clr) %>% addPolygons(CL[[1]]$x,CL[[1]]$y,fillColor = "red", stroke = FALSE) %>% addPolygons(CL[[3]]$x,CL[[3]]$y,fillColor = "red", stroke = FALSE) %>% addPolygons(CL[[5]]$x,CL[[5]]$y,fillColor = "red", stroke = FALSE) %>% addPolygons(CL[[7]]$x,CL[[7]]$y,fillColor = "red", stroke = FALSE) %>% addPolygons(CL[[9]]$x,CL[[9]]$y,fillColor = "red", stroke = FALSE) %>% addPolylines(CL[[1]]$x,CL[[1]]$y,color = "red") %>% addPolylines(CL[[5]]$x,CL[[5]]$y,color = "red") %>% addPolylines(CL[[8]]$x,CL[[8]]$y,color = "red")

Copies of my RStudio screen is nice, but visualising it is just awesome. I will try to find a way to load that map on my blog, but it might be difficult (so far, it is possible to visualise it on http://rpubs.com/freakonometrics/)

**leave a comment**for the author, please follow the link and comment on their blog:

**Freakonometrics » R-english**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.