R2leaflet (v0.1) – make interactive online maps from R

[This article was first published on Robert Grant's stats blog » R, 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.

I have been working on a simple R function to take latitude and longitude of points of interest, and text for pop-up labels, and produce an interactive online map. Interactive graphics are incredibly useful in getting people interested in your work and communicating your data effectively, but very few statisticians / data analysts have the skills needed to make them. My aims are in this order:

  1. encourage more data people to find out about JavaScript and teach themselves some basic skills
  2. give you a really easy way of making an online interactive map

Introducing JavaScript is what it’s all about for me, so I have aimed it at newcomers. The function is called R2leaflet because it uses the popular JavaScript package leaflet to construct the map. It just takes your data and constructs a lot of text around it to form a new HTML file which contains your map. You could just upload the whole thing if you know nothing about HTML (and don’t mind having a page with a map and nothing else!), or if you are a web whizz then you will know which bits to copy and paste into your own page. It also includes a comment against each line, and the aim of that is to encourage even casual users to open the HTML file in a text editor and see how it works.

It is at a very early stage, and I have a long list of things to do to improve it, but I would value any feedback on it. You can download it from here and then load it into R by typing:
source("R2leaflet.R")

The code is at Github if you want to see how it is made and collaborate. My to-do list is there too. I encourage anyone interested in this to join me and contribute so we can make a whole suite of simple R2JS visualization functions!

Let’s look at a simple example. Below is a screenshot of the map which links to my website where you will find the real interactive version. This is simply because this blog is hosted on WordPress.com and they do not allow any JavaScript from the blogger.

mapsnap1

Here’s how it was made:

lat<-c(51.426694, 51.523242, 54.008727, 54.977433) long<-c(-0.174015, -0.119959, -2.785206, -1.607604) label<-c("St George's Hospital", "UCL Institute of Child Health <br> Harvard GCSRT venue", "Lancaster University", "University of Northumbria <br> RSS 2013 venue")

R2leaflet(lat=lat,long=long,label=label,filename="mymap.html")

The defaults have been retained for some other arguments: map.lat and map.long, which center the map, map.zoom, which zooms in and out, and popup, which is a Boolean vector of the same length as lat, long and label, which is TRUE if you want a popup and FALSE if you don’t.

There are some neat R functions out there to make latitude and longitude from various co-ordinate systems. If you need to look up latitude and longitude for a specific few points, this website is handy. Notice how the line breaks in the popups are produced by
, which an HTML tag.  Want to format your text inside the popup? You can simply add HTML tags to the character vector called ‘label’. If you don’t know about HTML tags, visit this page and you’ll soon be a web coding fiend.

Remember that any vectors you supply to the arguments ‘lat’, ‘long’, ‘label’ and ‘popup’ will be recycled R-style if they are not the same length, with potentially disastrous results in your map! The function returns nothing at present, but you can easily change it to select from the various parts of text written to the file in a return command if that would be useful for you.


To leave a comment for the author, please follow the link and comment on their blog: Robert Grant's stats blog » R.

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.

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)