moving.block: Cycle through an R object and plot each subset of elements

View source: R/moving.block.R

moving.blockR Documentation

Cycle through an R object and plot each subset of elements

Description

For a long numeric vector or matrix (or data frame), we can plot only a subset of its elements to take a closer look at its structure. With a moving “block” from the beginning to the end of a vector or matrix or any R objects to which we can apply subset, all elements inside the block are plotted as a line or scatter plot or any customized plots.

Usage

moving.block(dat = runif(100), block, FUN, ...)

Arguments

dat

a numeric vector or two-column matrix

block

block length (i.e. how many elements are to be plotted in each step)

FUN

a plot function to be applied to the subset of data

...

other arguments passed to FUN

Details

For a vector, the elments from i + 1 to i + block will be plotted in the i-th step; similarly for a matrix or data frame, a (scatter) plot will be created from the i + 1-th row to i + block-th row.

However, this function is not limited to scatter plots or lines – we can customize the function FUN as we wish.

Value

NULL

Note

There will be ani.options('nmax') image frames created in the end. Ideally the relationship between ani.options('nmax') and block should follow this equality: block = length(x) - ani.options('nmax') + 1 (replace length(x) with nrow(x) when x is a matrix). The function will compute block according to the equality by default if no block length is specified.

The three arguments dat, i and block are passed to FUN in case we want to customize the plotting function, e.g. we may want to annonate the x-axis label with i, or we want to compute the mean value of dat[i + 1:block], etc. See the examples below to learn more about how to make use of these three arguments.

Author(s)

Yihui Xie

References

Examples at https://yihui.org/animation/example/moving-block/


yihui/animation documentation built on March 27, 2023, 2:50 p.m.