Playing with Leaflet (and Radar locations)

September 30, 2015

(This article was first published on Freakonometrics » R-english, and kindly contributed to R-bloggers)

Yesterday, my friend Fleur did show me some interesting features of the leaflet package, in R.


In order to illustrate, consider locations of (fixed) radars, in several European countries. To get the data, use

radar=read.table(file=paste("destinator/",nf,sep=""), sep = ",", header = FALSE, stringsAsFactors = FALSE)
 radar$type <- sapply(radar$V3, function(x) {z=as.numeric(unlist(strsplit(x, " ")[[1]])); return(z[!])})
  radar <- radar[,c(1,2,4)]
  names(radar) <- c("lon", "lat", "type")
id=which(substr(L,4,8)=="Radar" & substr(L,nl-2,nl)=="csv")
for(i in id) radar_E=rbind(radar_E,ext_radar(L[i]))

(to be honest, if you run that code, you will get several countries, but not France… but if you want to add it, you should be able to do so…). The first tool is based on popups. If you click on a point on the map, you get some information, such as the speed limit where you can find a radar. To get a nice pictogram, use

fileUrl <- ""
download.file(fileUrl,"radar.png", mode = 'wb')
RadarICON <- makeIcon(  iconUrl = fileUrl,   iconWidth = 20, iconHeight = 20)

And then, use to following code get a dynamic map, mentionning the variable that should be used for the popup

m <- leaflet(data = radar_E) 
m <- m %>% addTiles() 
m <- m %>% addMarkers(~lon, ~lat, icon = RadarICON, popup = ~as.character(type))

Because the picture is a bit heavy, with almost 20K points, let us focus only on France,

The other interesting (not to say amazing) tool is a tool that creates clusters.

We have here the number of radars in a neighborhood of the point we see on the map. So far, it looks like polygons used are abitrary…

The code is extremely simple

m <- leaflet(radar_E) 
m <- m %>% addTiles()
m <- m %>% addMarkers(~lon, ~lat, clusterOptions = markerClusterOptions())

I still wonder how we can change the polygons (e.g. using more standard administrative regions). But I have to admit that this option is awesome.

To leave a comment for the author, please follow the link and comment on their blog: Freakonometrics » R-english. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)