moving.block {animation}R Documentation

Cycle through an R object and plot each subset of elements


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.


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



a numeric vector or two-column matrix


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


a plot function to be applied to the subset of data


other arguments passed to FUN


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.




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.


Yihui Xie


Examples at

[Package animation version 2.5 Index]