2014-01-12

Mapping red-listed rainforest tree species

Rainforest Foundation Norway keeps a red list of tree species. Where do these trees grow?

One of the R packages developed by rOpenSci is rgbif. It's a handy wrapper to the Global Biodiversity Information Facility API. With geolocation data returned by the query, you can plot points on the world map.

Let's start with the list. Instead of using R all the way through, I scraped the HTML table rows with the Google Chrome extension Scraper, and saved data as a spreadsheet on Google Drive. This is the way Scraper works.

As I mentioned in my job blog the other day, one of the many good tutorials out there on using Scraper, is by Jens Finnäs.

Data needs some pruning in this exercise. What you need for the GBIF query, is basically just the Latin names. To make things somewhat simpler, I'll take only the first one mentioned along each species; many have several.

Here is the R code for pruning, and for querying GBIF. The script saves the return data by the tree status, and in two file sets: R data, and GeoJSON. The first ones are used as input for a Shiny web application, where they will be plotted both as an interactive gvisGeoChart by googleVis, and as a static map with the (only slightly modified) gbifmap function from rgbif. The GeoJSON files will be rendered straight from a GitHub Gist. All of this just to demonstrate (foremost to myself!) that there are many ways to plot and serve maps, and that they all have different pros and cons depending on the amount and type of data. The challenge here is that there will be multiple data points on the same geolocation, and the number of different species is rather big too.

Next, the web application. Here is the R code for it, and this is the app itself. The maps served by GitHub: Critically_Endangered, Endangered, Vulnerable, Near_Threatened and Other.

The status Other is named by me. It refers to those rows in the original Foundation table, where no exact two-character status was given.

On the googleVis map, both the size and color of the points reflect the amount of occurrences on that particular location. This is of course repetitive, but I haven't yet find a better solution. Optimally, the color would tell something else, maybe the species. Yet, the tooltip has this information already, so there you are. Note that the country name in the tooltip comes from a yet another scraped file, originating to Wikipedia. Initially, I had in mind fetching the name by querying the acronym against the DBpedia Linked Data, but reverted to scraping. The magnifying glass is a nifty tool of course, but IMHO doesn't add much on the informative side.

The static map gives a quick overview of all species and their location. This works OK when the number is relatively small. However, the more variety there is, the harder it is to discern between different colors. Transparency (alpha) does the best it can to show that indeed there are multiple points on the same spot. With my expanded color palette however, the colors became so elusively light that I was forced to reduce transparency. Although you can customize the gbifmap function, with my limited skills I didn't succeed in passing my own alpha value, so I modified the function accordingly. Note to self: find out the best practise of how this kind of modification should be done.

The GeoJSON maps were a positive surprise. Out of the Gist box, the JavaScript code produces nicely detailed maps, and in hot spots points are clustered. Marker symbols and colors could of course be different across species. Here, I simply use one red park2 marker in all.