Q. How do I use a static scale or a fixed range for an axis?

#static #scale #axis

A.

  • Use par(xlim = ..., ylim = ...) to set a fixed range for the x-axis and y-axis.
  • Use par(xlim = NULL, ylim = NULL) to clear the setting.

Q. How to style the frame in which the plot is made?

#CSS #style #frame

A. The syntax to use is:

device <- animate$new(width, height, attr = list(style = MY_STYLE))

where MY_STYLE can be:

  • Border: MY_STYLE <- "border:1px solid black;"
  • Border with rounded corners: MY_STYLE <- "border:1px solid lightgray; border-radius:5px;"

Q. I heard there is a trick for developing animated plot in a code chunk of an R Markdown Document?

#trick #inline #R Markdown Document #RMD #code chunk #workflow

A. Yes, the function below is a handy trick to set up the device and render the output in a code chunk.

animate_it <- function(..., width = 600, height = 600, options = click_to_play()) {
  # Setup
  require(animate)     # 'require' is designed for use inside functions
  device <- animate$new(width, height, virtual = TRUE, 
                        attr = list(style = "border:1px solid lightgray"))
  attach(device)       # Make methods of the device available in the namespace
  pryr::f(...)()       # Main code
  rmd_animate(device, options = options)  # Embed animated plot in R Markdown Document
}

# Usage
animate_it({
  id <- new_id(1:10)
  plot(1:10, runif(10, 0, 1), id = id)
  plot(1:10, runif(10, 0, 1), id = id, transition = TRUE)
})

Q. Are there any tips to improve the workflow developing in the R console?

#trick #R console #workflow

A. Yes, I sometimes use the following for development in the R console.

setup <- function(width = 600, height = 600) {
  require(animate)
  device <- animate$new(width, height, attr = list(style = "border:1px solid lightgray"))
  attach(device)
}

cleanup <- function() {
  clear()
  off()
  detach(device)
}


# Usage 
setup()
id <- new_id(1:10)
plot(1:10, runif(10, 0, 1), id = id)
plot(1:10, runif(10, 0, 1), id = id, transition = TRUE)
cleanup()

Q. What does set_max_stacksize do? Why does the device need memory?

#memory #advanced usage

A. set_max_stacksize sets the cap of the internal memory of the device. Memory is needed when one wants to make an animated plot and then export it to a file. This is by default switched on, since the memory usage is generally low, and it makes more sense to have a plot ready to be exported when it is complete.