Learn R Programming

moveVis (version 0.9.5)

animate_stats: Animate movement data statistics

Description

animate_stats animates statistic plot from movement data provided as move class objects or a list of them and basemap data provided as raster. It extracts basemap values of pixels that are part of the movement paths and visualizes frequencies per value. The function creates an animated GIF file and saves it into the output directory. See also animate_move.

Usage

animate_stats(data_ani, out_dir, conv_dir = "convert", layer = "basemap",
  layer_dt = "basemap", layer_int = FALSE, layer_type = "",
  val_limits = NA, paths_col = "auto", paths_mode = "true_data",
  stats_type = "", stats_gg = "", stats_digits = 1, stats_tframe = 5,
  stats_title = "", frames_layout = 0, frames_nmax = 0,
  frames_interval = 0.04, frames_nres = 1, frames_width = 600,
  frames_height = 600, out_name = "final_gif", log_level = 1,
  log_logical = FALSE, ...)

Arguments

data_ani

list or moveStack class object. Needs to contain one or several move class objects (one for each individual path to be displayed) containing point coordinates, timestamps, projection and individual ID.

out_dir

character. Output directory for the GIF file creation.

conv_dir

character. Command or directory to call the ImageMagick convert tool (default to be convert). You can use conv_dir = get_imconvert() to search for the right command/tool directory and/or get the required software.

layer

raster, list or character. Single raster object or list of raster objects to be used as (dynamically changing) basemap layer. Default is "basemap" to download a static basemap layer. Use a rasterBrick class object and set layer_type to "RGB" to compute a RGB basemap.

layer_dt

POSIXct or list. Single POSIXct date/time stamp or list of POSIXct date/time stamps corresponding to the acquisition dates of the layer raster objects.

layer_int

logical. Whether to interpolate the basemap layer objects over time, if several are provided (TRUE), or to display them one after another depending on the animation time frame that is displayed (FALSE). Default is FALSE.

layer_type

charachter. Layer type. Can be either "RGB" (if layer is a rasterBrick class onejct), "gradient" or "discrete". Default is "RGB". Ignored, if layer = "basemap".

val_limits

numeric vector. Fixed minimum and maximum limit values of the basemap value range (gradient layer type). Default is NA for data-depending minimum and maximum values. Ignored, if layer_type is "discrete" or "RGB".

paths_col

character vector. Colours of the individual animation paths. If set to "auto", a predfined colour set will be used. If single colour, all paths will be displayed by the same colour. If more individuals then colours, the colours are repeated.

paths_mode

character vector. Mode to be used for dealing with time information when displaying multiple individual paths. See animate_move for details. Default is "true_data".

stats_type

character. Defines which standard plot design should be used. Select either "line" or "bar". Ignored, if stats_gg is used.

stats_gg

character. Enables usage of ggplot2 syntax for plot design. If set, stats_type is ignored. See details for information on the statistic data structure to be used by the user defined plot function.

stats_digits

numeric. Defines how detailed the statistic plot should be as number of decimals. Values with more decimals are rounded. Default is 1 for one decimal.

stats_tframe

numeric. Defines the temporal range of the periodic stats plot. Default is 5 meaning that five time frames back from the displayed frame are evaluated.

stats_title

character vector. Optional plot titles. Two character strings within a vector.

frames_layout

matrix. Optional layout. Define, which plots should be placed where using a matrix represnting the GIF frame. Matrix elements can be the following plot identifiers: "map" for the spatial plot, "st_all", "st_per" for the overall and periodic stats plot or "st_allR", "st_perR", "st_allG", "st_perG", "st_allB", "st_perB" for the overall and periodic stats plots per band, when using layer_type = "RGB", and 'st_leg' for a stats legend. Alternatively, integers from 1 to 8 corresponding to the described order can be used. Plots not mentioned using frames_layout identifiers are not displayed. If set to 0, layout is generated automatically. Default is 0.

frames_nmax

numeric. Number of maximum frames. If set, the animation will be stopped, after the specified number of frames is reached. Default is 0 (displaying all frames).

frames_interval

numeric. Duration, each frame is displayed (in seconds). Default is .04.

frames_nres

numeric. Interval of which frames of all frames should be used (nth elements). Default is 1 (every frame is used). If set to 2, only every second frame is used.

frames_width

numeric. Number of pixels of frame width. Default is 600.

frames_height

numeric. Number of pixels of frame height. Defualt is 600.

out_name

character. Name of the output file. Default is "final_gif".

log_level

numeric. Level of console output given by the function. There are three log levels. If set to 3, no messages will be displayed except erros that caused an abortion of the process. If set to 2, warnings and errors will be displayed. If set to 1, a log showing the process activity, wanrnings ans errors will be displayed.

log_logical

logical. For large processing schemes. If TRUE, the function returns TRUE when finished processing succesfully.

...

optional arguments.

Value

None or logical (see log_logical). The output GIF file is written to the ouput directory.

Details

animate_stats is a wrapper function of animate_move to create single statistic plots without spatial plotting. For statistic plot animations sidy-by-side with spatial plot animations, use animate_move (see stats_create argument). The function can handle all arguments taken by animate_stats as well. Use stats_gg to provide an own ggplot2 plot design as shown in the examples. The statistics are stored for both plots (periodic and accumulated) withn the variable pdat (list of two, indexed by k ranging from 1 to 2 for each plot). Both pdat lists contain the stats elements framewise for each time step. For this, see the stats_gg example. The variable cols (list of two, one per plot) contains the defined colour values and namings.

See Also

get_imconvert

Examples

Run this code
# NOT RUN {
#Load move and moveVis packages
library(move)
library(moveVis)

#Get the sample data from the moveVis package
data("move_data")
move_data$dt <- as.POSIXct(strptime(move_data$dt, "%Y-%m-%d %H:%M:%S", tz = "UTC"))

#Create moveStack object including multiple individuals
data_ani <- move(move_data$lon, move_data$lat, proj=CRS("+proj=longlat +ellps=WGS84"),
                 time = move_data$dt, animal=move_data$individual, data=move_data)
 
#Load basemap MODIS NDVI data
data("basemap_data")
layer = basemap_data[[1]]
layer_dt = basemap_data[[2]]

#Find command or directory to convert tool of ImageMagick
conv_dir <- get_imconvert()

#Specify the output directory, e.g.
out_dir <- "/out/test"
#or to a temporary directory:
out_dir <- paste0(tempdir(),"/test")
dir.create(out_dir)


#Call animate_stats()
animate_stats(data_ani, out_dir, conv_dir = conv_dir,
              layer=layer, layer_dt = layer_dt, layer_type = "gradient",
              stats_digits = 1, stats_type = "bar", out_name = "final_gif",
              log_level = 1,frames_nmax = 60)
              
#Define your own ggplot2 plot design
stats_gg <- 'ggplot(data = pdat[[k]][[i]], aes_(x = ~val, y = ~value, colour = ~variable)) + 
             geom_smooth() + geom_point() + theme_bw() + theme(aspect.ratio=1) +
             scale_y_continuous(expand = c(0,0),limits = c(0,stats_max[k])) +
             scale_x_continuous(expand = c(0,0)) + 
             scale_color_manual(name="",values = cols[[k]]) +
             labs(x = "Basemap Value", y="Frequency",
                  title=stats_title[[k]], label=c("123","456")) +
             theme(plot.title = element_text(hjust = 0.5),
                   plot.subtitle = element_text(hjust = 0.5))'
                  
#Call animate_stats() with stats_gg
animate_stats(data_ani, out_dir, conv_dir = conv_dir,
              layer=layer, layer_dt = layer_dt, layer_type = "gradient",
              stats_digits = 1, stats_gg = stats_gg, out_name = "final_gif",
              log_level = 1,frames_nmax = 60)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab