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

 Languages tweeted around Germany: red, blue, green, yellow, grey are for German, French, English, Dutch and other  respectively. See here for a zoomable version.
Motivated by the project twitter languages of New York I wanted to do map of tweets too.
For a different purpose (sentiment analysis) I am collecting tweets around Germany anyway. In another blog entry I describe how I collected the data.

With the package openmap, it is easy to create a map of the languages tweeted. Such a map is shown on the left. However, in post-google-map times if the user sees a map, he just starts to spin his mouse wheel to zoom into the map. Lets see how one can create such a map.

#### Creating the map

I assume that the data is in the following format, I need coordinates and colors. See the blog how to extract the data from the json-files and do the color coding.

long      lat  lang
1  4.901844 52.37762    en
2  6.255914 52.51602    nl
3 13.736128 51.04736    en

....

[1] “#FF000080” “#00FF0080” “#0000FF80” …

Zoomable maps are created with socalled tiles. You start with one resolution (zoomlevel), say zoomlevel 4. You create one tile. In the next zoom level this tile is split in 4 tiles and so on. These tiles are have to be placed in a certain directory structure. To create a zoomable map you simply have to create this directory structure with those tiles. There are plenty of javascript libraries with take all those tiles and create a map. I use one called leaflet (see below). Usually these libraries require 256×256 png images.

So lets see how this works. The region of the above figure and zoom-level 4 corresponds to the path 4/8/5.png. For the next zoom level 5 one has to create the 4 files 5/16/10.png, 5/16/11.png, 5/17/10.png and 5/17/12.png. The directory corresponds to x-axis (longitude) and the file name to the y-axis (latitude), the figure below shows how the split is done.

 The tile on the left is split into 4 tiles shown on the right.
In the next zoom level we would have to render 16 images starting with 6\32\20.png.

The script below starts with the tile 4\8\5.png and recursively creates the images up to the desired zoom levels. To get the bounding boxes in terms of longitude and latitude the function tile2boundingBox is used. The maps are obtained using the map function of the OpenStreetMap package. The points have to be transformed  using projectMercator(lat = sd$lat, lon = sd$lon)and can then be drawn using e.g. the points function.

Once you have created the tiles, its just a few lines of javascript to have the maps ready. See below for the index.html file using leaftlet library.