class: center, middle, inverse, title-slide # R for Antarctic and Southern Ocean science ### Michael Sumner, Ben Raymond, Anton Van de Putte ### MEASO18 --- class: middle background-image: url(extra/Rlogo.png) background-size: auto 65% background-position: 50% 50% # R for Antarctic and Southern Ocean Science - context and rOpenSci (Ben) - modern state of R (Mike) - some of our stuff (Ben, Mike) - the biodiversity.aq Antarctic virtual research environment (Anton) - general chat --- # rOpenSci <img style="background-color: white; padding:10px; float:right;clear:none; margin:20px;" width="30%" src="extra/ropensci_logo.png" alt="ropensci logo" /> rOpenSci: R for reproducible science, open data & software https://ropensci.org/community/ Tutorials, use cases, discussion forum, open calls, physical meetings ### For R developers/regular users: - slack: sign up at http://slack.ropensci.org/ - package onboarding SCAR/rOpenSci joint initiative to build the Antarctic R community: see the #antarctic channel on slack. Note that the slack channel tends to be fairly technical, and probably most suited for regular R users and package developers. Mechanisms for broader engagement (discussion fora, how-to's etc) will happen as this initiative progresses. --- # Antarctic/SO R packages See https://github.com/SCAR/antarctic-r-packages - taxonomy - mapping - environmental data access - ocean colour processing - biodiversity, diet data - animal tracking What packages do we need? What tasks/analyses should be easier than they currently are? SCAR's github: https://github.com/SCAR --- class: middle background-image: url(extra/Rlogo.png) background-size: auto 65% background-position: 50% 50% # Modern state of --- # The tidyverse https://www.tidyverse.org A family of modern, fast, consistent packages. The single best guide http://r4ds.had.co.nz/ (free online!) Key packages * **dplyr** - data frame creation, manipulation, subsetting, and merging as`filter`, `select`, `mutate`, `rename`, `join`, consistent with database workflows * **readr**, **readxl** - read data frames from text and Excel files * **purrr** - functional programming, tools needed for unstructured, list data * **ggplot2** - grammar of graphics, consistent visual exploration and plot making from data frame data These packages work extremely well together. --- # Key spatial R * sp * raster * sf * rgdal These work together, somewhat. Use google, stackoverflow, gis.stackexchange.com. Twitter has a friendly and helpful R community: #rstats --- class: middle background-image: url(extra/Rlogo.png) background-size: auto 65% background-position: 50% 50% # Some of our stuff --- # Local techniques - a toolbox approach We're creating R packages and collating related resources. SOKI.aq overview of our local tools and approaches: http://soki.aq/display/SET/1.+General+Strategy * [angstroms](https://CRAN.R-project.org/package=angstroms) - bending raster for working with ROMS * [rbgm](https://CRAN.R-project.org/package=rbgm) - working with Atlantis BGM Box Geometry Models --- # A toolbox approach: <u>blueant</u> and raadtools https://github.com/AustralianAntarcticDivision/blueant - pre-configured definitions for environmental/satellite data sets - functions for maintaining local copies of those data ```r library(blueant) ## define where we want to store our data my_data_dir <- "c:/my/big/data/collection" cf <- bb_config(local_file_root=my_data_dir) ## add a few data sets to our configuration my_sources <- c("NSIDC SMMR-SSM/I Nasateam sea ice concentration", "IBCSO bathymetry") cf <- cf %>% bb_add(sources(my_sources)) ## go! bb_sync(cf) ``` --- # A toolbox approach: <u>blueant</u> and raadtools https://github.com/AustralianAntarcticDivision/blueant - *anyone* can use this to maintain their own copy of these data - we use this to maintain a central data collection for a community of researchers - with a scheduled task to keep it updated - together with `raadtools` and virtual machine capability provides us with a consistent, community-level framework - separation of data-getting (blueant) with data-access (raadtools) --- # A toolbox approach: blueant and <u>raadtools</u> - the `raadtools` package complements `blueant` - it provides functions for reading and manipulating environmental data in a simple and consistent way https://github.com/AustralianAntarcticDivision/raadtools ```r library(raadtools) ?readice ?readsst ?readssh ?readcurr ?readtopo Functions that work on the blueant data collection: http://australianantarcticdivision.github.io/raadtools/reference/index.html ``` --- # A toolbox approach: blueant and <u>raadtools</u> ```r (ice <- readice(latest = TRUE)) ``` ``` ## class : RasterStack ## dimensions : 332, 316, 104912, 1 (nrow, ncol, ncell, nlayers) ## resolution : 25000, 25000 (x, y) ## extent : -3950000, 3950000, -3950000, 4350000 (xmin, xmax, ymin, ymax) ## coord. ref. : +proj=stere +lat_0=-90 +lat_ts=-70 +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs ## names : nt_20180409_f18_nrt_s.bin ## min values : 0 ## max values : 100 ## time : 2018-04-09 ``` --- # A toolbox approach: blueant and <u>raadtools</u> ```r (ssh <- readssh("2017-04-10", xylim = extent(120, 150, -70, -30))) ``` ``` ## class : RasterBrick ## dimensions : 160, 120, 19200, 1 (nrow, ncol, ncell, nlayers) ## resolution : 0.25, 0.25 (x, y) ## extent : 120, 150, -70, -30 (xmin, xmax, ymin, ymax) ## coord. ref. : +proj=longlat +rf=298.257 +a=6378136.3 ## data source : in memory ## names : Absolute.dynamic.topography ## min values : -1.296 ## max values : 0.955 ## time : 2017-04-10 ``` --- # Converting between projections Raster can reproject between grids to match disparate data, like sea ice in a polar projection, sea surface temperature in longitude/latitude. ```r sst <- readsst("2017-04-10", xylim = extent(-180, 180, -90, 0)) polar_sst <- projectRaster(sst, ice) plot(polar_sst, col = palr::sstPal(64)) ``` <img src="extra/unnamed-chunk-5-1.png" width="672" /> --- # Converting between projections Or in the opposite direction. ```r longlat_ice <- projectRaster(ice, sst) plot(longlat_ice) ``` <img src="extra/unnamed-chunk-6-1.png" width="672" /> --- class: middle background-image: url(extra/Rlogo.png) background-size: auto 65% background-position: 50% 50% # More emerging R stuff --- # New spatial framework The "simple features"" package `sf` provides very fast and more consistent spatial data support, for map data, points, lines, polygons. * a large shapefile? try `sf::read_sf`. * convert to old style `as(x, "Spatial")` for use with `raster`, `spdep` ... * `sf` support in `ggplot2` is coming Still in development, but package `ggplot2` from Github has very good plotting for `sf`. Use `devtools::install_github("tidyverse/ggplot2"))` --- # geom_sf ```r ## a map data set prepared earlier, the CCAMLR statistical areas ccamlr <- readRDS("/home/shared/data/assessment/sectors/ccamlr_statareas.rds") prj <- raster::projection(ccamlr) library(ggplot2) library(sf) library(raster) ice <- raadtools::readice(latest = TRUE)[[1L]] ccamlr <- st_transform(st_as_sf(ccamlr), projection(ice)) ## convert gridded sea ice to data frame names(ice) <- "iceconc" iced <- as.data.frame(ice, xy = TRUE) ``` (See plot on next slide) --- ```r ggplot(ccamlr) + geom_raster(data = iced, aes(x, y, fill = iceconc)) + geom_sf(fill = NA, aes(colour = name)) ``` <img src="extra/geom_sf_plot-1.png" width="672" /> --- # fasterize Merging vector (polygons) and raster (gridded) data can be difficult! Fasterize is super fast for this. Being able to generate a gridded version of a polygon data set can be extremely powerful - classifying a gridded data set - looking up values per polygon ```r library(fasterize) ccamlrgrid <- fasterize(ccamlr, ice, field = "name") ``` (Plot on next slide) --- # fasterize ```r plot(ccamlrgrid, col = sample(rainbow(nrow(ccamlr)+1)[-1])) ``` <img src="extra/fastplot-1.png" width="672" /> --- # mapview, mapedit, plotly Create interactive maps and even edit map data. * [mapview](https://r-spatial.github.io/mapview/) * [mapedit](https://CRAN.R-project.org/package=mapedit/) * [plotly](https://CRAN.R-project.org/package=plotly) --- class: middle background-image: url(extra/Rlogo.png) background-size: auto 65% background-position: 50% 50% # The Antarctic component of EU-LifeWatch --- class: middle background-image: url(extra/avdp1.png) background-size: 100% auto background-position: center center --- class: middle background-image: url(extra/avdp2.png) background-size: 100% auto background-position: center center --- # For more info http://marine.lifewatch.eu/ http://lifewatch.be/ http://www.lifewatch.eu --- class: middle background-image: url(extra/avdp4.png) background-size: 100% auto background-position: center center --- class: middle background-image: url(extra/avdp5.png) background-size: 100% auto background-position: center center --- class: middle background-image: url(extra/avdp6.png) background-size: 100% auto background-position: center center --- class: middle background-image: url(extra/avdp7.png) background-size: 100% auto background-position: center top <br /> <br /> <br /> # R packages [solong](https://github.com/SCAR/solong) Allometric equations that relate the body size of Southern Ocean taxa to their body part measurements [sohungry](https://github.com/SCAR/sohungry) Access to the SCAR Southern Ocean Diet and Energetics Database [antanym](https://github.com/SCAR/antanym) Antarctic geographic place name information.