top of page
  • Writer's pictureAPECS Belgium

R: Visualize occurrence records by binning

Updated: Nov 28, 2022

Let’s get a dataset from OBIS using robis, R client for OBIS API.

library(robis)
occ <- occurrence(resourceid = 2296)

The function occurrence() get occurrence records from OBIS which has resourceid = 2296 and transform the results into an R dataframe. To quickly visualize the occurrences, we can do

library(obistools)
plot_map(occ)

There are a lot of points overlapping each other. We can bin these points into hexagons/squares and colour these hexagons/squares based on the number of points in each hexagon. To achieve this, we can use the ly_hexbin function from rbokeh.

# hexbin with rbokeh
library(rbokeh)
library(dplyr)
attach(occ)
figure(width = 900, height = 450) %>%
 ly_map("world", color = "#707070", alpha = 0.8) %>% 
 ly_hexbin(decimalLongitude, decimalLatitude, xbins = 100, shape = 0.2, 
 alpha = 0.8, palette = "Spectral6", trans = log, inv = exp) %>% 
 theme_plot(background_fill_color = "black") %>% # dark background
 theme_grid(grid_line_alpha = 0) # remove grid

Warmer colour means more occurrences within that hexagon. The colour is based on log scale of the count.

It is nicer, but we could have done better! A lot of points are scatter around antarctic/subantarctic zone. It could be nicer to make the plot in polar projection. In this case, we can do that with ggplot!

library(ggplot2)
library(viridis)
world <- map_data("world")
ggplot() + 
 geom_bin2d(data = occ, aes(x = decimalLongitude, y = decimalLatitude), bins = 100) +
 geom_path(data = world, aes(x = long, y = lat, group = group), colour = "#c0c0c0") +
 scale_y_continuous(name = "latitude", breaks = (-2:2) * 30) + 
 scale_x_continuous(name = "longitude", breaks = (-4:4) * 45) + 
 coord_map("ortho", orientation = c(-90, 0, 0)) + # orthographic projection from South Pole
 scale_fill_viridis(option = "viridis", trans = "log") + # log scale for bin count
 theme(panel.background = element_rect("black"), # dark background
 panel.grid = element_blank(), # remove panel grid
 axis.text.x = element_blank()) # remove x-axis value

Tada!! We will post more interesting tutorials later! Thanks for reading!!

Credit: British Antarctic Survey. SOMBASE PYCNOGONIDS. Occurrence Dataset https://doi.org/10.15468/qtm508 accessed via iobis.org on 2018-02-11.


Written by Yi Ming Gan

11 views0 comments

Recent Posts

See All

Aiding polar research, from the eye of a data scientist

Although polar week is over, #PolarWorld never ends. “We are drowning in information but starved for knowledge” — John Naisbitt Immense amount of data were generated and published by polar researchers

bottom of page