---
title: "EMODnet Biology robis demo"
output:
  html_document: default
  html_notebook: default
---

## Install the robis package

robis is not available on CRAN. It needs to be installed from GitHub. In the devtools package, you will find the function install_github() which does the installation for you. If devtools is not yet installed on your machine, do that first.

```{r installation, echo=TRUE, message=FALSE, warning=FALSE}
is_installed <- function(mypkg) is.element(mypkg, installed.packages()[,1])
if(!is_installed("devtools"))  install.packages("devtools")
if(!is_installed("robis"))  devtools::install_github("iobis/robis")
if(!is_installed("leaflet"))  install.packages("leaflet")
if(!is_installed("tidyverse"))  install.packages("tidyverse")
if(!is_installed("rworldxtra"))  install.packages("rworldxtra")
library(robis)
library(leaflet)
library(tidyverse)
library(rworldxtra)
library(sf)
```

## Download occurence data from OBIS

### By scientific name

```{r plotLeafletmap}

pol = st_sfc(st_polygon(list(cbind(c(2.3,2.3,8,8,2.3),c(51,56,56,51,51)))))
pol_ext = st_buffer(pol, dist = 1)

data1 <- occurrence(scientificname = c("Cetartiodactyla"), geometry = st_as_text(pol))

data2 <- occurrence(scientificname = c("Cetartiodactyla"), geometry = st_as_text(pol_ext))
                     

leafletmap(data2)


```


```{r}
sort(table(data$phylum))

data[grep("SCANS", data$datasetName),]

#filter mammals
#
data %>% filter(order == "Cetartiodactyla")
data %>% filter(institutionCode == "European Seabirds at Sea")



```




```{r}

leaflet(data) %>%
  addTiles(group = "OSM (default)") %>%
  addProviderTiles("Hydda.Base", group = "Hydda.Base") %>%
  addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
  addWMSTiles("http://ows.emodnet-bathymetry.eu/wms",
              layers = "emodnet:mean_multicolour",
              options = WMSTileOptions(version = "1.3.0", format="image/png", transparent = T),
              group = "EMODnet bathymetry") %>%
  addWMSTiles("http://geodata.nationaalgeoregister.nl/natura2000/ows",
              layers = "natura2000",
              options = WMSTileOptions(version = "1.3.0", format="image/png", transparent = T, EPSG = "28992"), group = "Natura2000") %>%
  addProviderTiles("OpenSeaMap", group = "OpenSeaMap") %>%
  addCircleMarkers(lat = ~decimalLatitude, lng = ~decimalLongitude, radius = ~log10(individualCount/100), group = "species") %>%
  addLayersControl(
    baseGroups = c("OSM (default)", "Hydda.Base", "Esri.WorldImagery"),
    overlayGroups =  c("EMODnet bathymetry", "OpenSeaMap", "observations", "Natura2000")
  ) %>%
  hideGroup(c("EMODnet bathymetry", "OpenSeaMap", "Natura2000"))


```


Or plot using ggplot


```{r plotGGplot}

data("countriesHigh")
world <- fortify(countriesHigh)
ggplot(data = data, aes(decimalLongitude, decimalLatitude)) +
  geom_polygon(data = world, aes(x = long, y = lat, group = group), fill = "darkgrey") +
  geom_point(aes(size = individualCount, color = scientificName)) +
  coord_quickmap(xlim = range(data$decimalLongitude), ylim = range(data$decimalLatitude))
```



```{r plot time series}

data %>% 
  ggplot (aes(x = yearcollected, y = individualCount)) + 
  geom_boxplot(aes(group = yearcollected)) + 
  scale_y_log10()

```



```{r}
data %>% group_by(yearcollected) %>% 
  summarize(average = mean(individualCount, na.rm = T)) %>%
  ggplot (aes(x = yearcollected, y = average)) + 
  geom_point(aes()) + 
  scale_y_log10()
```



