{"word_count": 554, "character_count": 4539}
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 &#60;- readShapePoints("Cholera_Deaths")df_deaths &#60;- data.frame(deaths@coords)coordinates(df_deaths)=~coords.x1+coords.x2proj4string(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.x1lat=df$coords.x2Once 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, usem = leaflet()%&#62;% addTiles() m %&#62;% 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=.5op=.8clr="blue"m = leaflet() %&#62;% addTiles()m %&#62;% addCircles(lng,lat, radius = rd,opacity=op,col=clr)We can also add some heatmap.X=cbind(lng,lat)kde2d &#60;- 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$x1y=kde2d$x2z=kde2d$fhatCL=contourLines(x , y , z)We have now a list that contains lists of polygons corresponding to isodensity curves. To visualise of of then, usem = leaflet() %&#62;% addTiles() m %&#62;% addPolygons(CL[[5]]$x,CL[[5]]$y,fillColor = "red", stroke = FALSE)Of course, we can get at the same time the points and the polygonm = leaflet() %&#62;% addTiles() m %&#62;% addCircles(lng,lat, radius = rd,opacity=op,col=clr) %&#62;%  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 wellm = leaflet() %&#62;% addTiles() m %&#62;% addCircles(lng,lat, radius = rd,opacity=op,col=clr) %&#62;%  addPolygons(CL[[1]]$x,CL[[1]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolygons(CL[[3]]$x,CL[[3]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolygons(CL[[5]]$x,CL[[5]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolygons(CL[[7]]$x,CL[[7]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolygons(CL[[9]]$x,CL[[9]]$y,fillColor = "red", stroke = FALSE)An alternative is to hightlight (only) the contour of the polygonm = leaflet() %&#62;% addTiles() m %&#62;% addCircles(lng,lat, radius = rd,opacity=op,col=clr) %&#62;%  addPolylines(CL[[1]]$x,CL[[1]]$y,color = "red") %&#62;%  addPolylines(CL[[5]]$x,CL[[5]]$y,color = "red") %&#62;%  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 colorm = leaflet() %&#62;% addTiles() m %&#62;% addCircles(lng,lat, radius = rd,opacity=op,col=clr) %&#62;%  addPolygons(CL[[1]]$x,CL[[1]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolygons(CL[[3]]$x,CL[[3]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolygons(CL[[5]]$x,CL[[5]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolygons(CL[[7]]$x,CL[[7]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolygons(CL[[9]]$x,CL[[9]]$y,fillColor = "red", stroke = FALSE) %&#62;%  addPolylines(CL[[1]]$x,CL[[1]]$y,color = "red") %&#62;%  addPolylines(CL[[5]]$x,CL[[5]]$y,color = "red") %&#62;%  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/)