# `rtsVis`
[](https://CRAN.R-project.org/package=rtsVis)
[](https://CRAN.R-project.org/package=rtsVis)
A lightweight `R` package to visualize large raster time series, building on a fast temporal interpolation core.
rtsVis is linked to the `moveVis` package and their joint use is recommended.
## Installation
The released version of rtsVis can be installed from CRAN:
```r
install.packages("rtsVis")
```
The latest version of the package can be installed from github.
```r
devtools::install_github("JohMast/rtsVis")
```
## Concepts
rtsVis operates on lists of objects:
- Lists of `raster` stacks
- Lists of frames (`ggplot` objects of rasters or charts)
To process those lists in a pipeline, we recommend pipes such as provided by `magrittr`.
- Positions of points or polygons, which can be added to the frames or be used to extract values from the time series and create animated charts.
## Functions
### Preparing rasters
* `ts_raster` Assemble/interpolate a raster time series.
* `ts_fill_na` Fill NA values in a raster time series
### Creating Frames
* `ts_flow_frames` Create a series of charts of a raster time series.
* `ts_makeframes` Create spatial ggplots of a raster time series.
* `ts_add_positions_to_frames` Add points, coordinates, or polygons to a list of spatial plots.
## Example
In this example, we use a MODIS time series ` (download here) `to create typical visualisations which highlight vegetation dynamics on the African continent.
### Part 1: Preparing the Data
We read our inputs:
* A list of **MODIS raster files** in tif format, which contain 4 bands (Red, NIR, Blue, Green). These are monthly composites of the MCD43A4.006 product.
* **points** in gpkg format, which can be visualized and for which statistics can be extracted. These correspond to various places in and around Africa.
Optional, but useful to enhance the visualizations:
* an **outline** of the continent Africa.
* a global **hillshade** raster which can be applied to the spatial plots.
Some functions in `rtsVis` require that timestamps for the rasters are set. Often, these come with the metadata or can be derived from the filename. They can also be manually set. In this example, we have monthly medians, and no true acquisition time. Therefore, we manually create a series of dates.
In addition to the input times, we also set output times - for these, output dates will be created. We simply create a second, denser series of dates. Having a denser series will smooth the animation.
If images from the input series are missing, it is not an issue, at least not from the technical side of things. Images for `out_times` will be interpolated regardless of the regularity of the input data. To illustrate this, we remove one of the input images.
``` r
library(raster)
library(sf)
library(rtsVis)
library(RStoolbox)
library(tidyverse)
# Load the inputs
modis_tifs <- list.files("Beispieldaten/MODIS/Africa_MODIS_2017-2020/",full.names = T,pattern=".tif$")
points <- st_read("Beispieldaten/Ancillary/Africa/Africa_places.gpkg")
outline <- st_read("Beispieldaten/Ancillary/Outline_continents_africa.gpkg")
hillshade <- raster("Beispieldaten/Ancillary/SR_LR/SR_LR.tif") %>% readAll()
#Create in-times and out-times
in_dates <- as.POSIXct(seq.Date(as.Date("2017-01-15"),
as.Date("2020-12-15"),
length.out = length(modis_filled)))
out_dates <-seq.POSIXt(from = in_dates[1],
to = in_dates[length(in_dates)],
length.out = length(in_dates)*4 )
# simulate a missing image
in_dates <- in_dates[-13]
modis_tifs <- modis_tifs[-13]
```
### Part 2: Preparing the Rasters
For the preparation of the rasters, we use three functions:
* `stack` A `raster` function which we use with `lapply` to load the tifs into a list.
* `ts_fill_na` We fill, wherever possible, missing values using simple temporal interpolation. This is not strictly necessary, but improves the visualization.
* `ts_raster` We assemble a full time series. This will interpolate additional missing frames for every desired output date and can take a long time for large time-series.
Often, data requires additional preprocessing steps, such as reprojection, cropping, or masking.
These can be applied with `lapply` to retain the list format.
`ts_raster` returns a list of interpolated rasters, one for each desired output date, with timestamps attached.
``` r
#Read the images as stacks
modis_tifs_stacks <- modis_tifs %>% lapply(stack)
#fill NAs
modis_filled <- ts_fill_na(modis_tifs_stacks)
modis_ts <- ts_raster(
r_list = modis_filled,
r_times = in_dates,
out_times = out_dates,
fade_raster = T)
```
### Part 3: Creating Basic RGB animations
In this step, `ts_makeframes` is used to create a list of frames (`ggplot` objects) from the time series (`raster` objects).
We also add a outline to the plot area. This is one example of adding a *position* to a time series.
Using pipes is not necessary, but improves readability greatly.
``` r
#create the frames from the raster
modis_frames_RGB <-
ts_makeframes(x_list = modis_ts,
l_indices = c(1,4,3), # MODIS bands are Red, NIR, Blue, Green
minq = 0.0) # adjust the stretch slightly
# Add the outline of the continent for visual clarity
modis_frames_RGB_ol <-
modis_frames_RGB %>%
ts_add_positions_to_frames(
positions = outline,
psize = 1)
# Use moveVis utility to animate the frames into a gif
moveVis::animate_frames(modis_frames_RGB_ol,
overwrite = TRUE,
out_file = "modis_frames_RGB_ol.gif",
height=300,
width=300,
fps = 10,
res=75)
```


