Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: "InvalidArgument `-delay' with animation and ggplot

I'm trying to make an animated plot in R from a large data set (from a cyclic scientific experiment), to visualise the change in two variables over time. I'm using the animation library with just simply:

saveGIF(
    for(i in 1:100){
    mygraph(i)
}, interval = 0.1, ani.width = 640, ani.height = 480)

where mygraph(i) just plots the graph for the cycle i. If I use plot() to make the graph, then it works perfectly fine, but if I instead use ggplot (which I would like to do as I eventually want to use this to make more complex plots), then it doesn't work and I get the following output:

Executing: 
'convert' -loop 0 -delay 'animation.gif'
convert: InvalidArgument `-delay': animation.gif @ error/convert.c/ConvertImageCommand/1161.
an error occurred in the conversion... see Notes in ?im.convert
[1] FALSE 

I'm very new to R so I'm a bit stuck, and I've not worked out a solution from looking at ?im.convert or from searching around. Any suggestions would be enormously appreciated...

Example with dummy data as requested:

library(animation)
library(ggplot2)

x <- 1:20
y <- 21:40
z <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
data <- data.frame(x,y,z)

mygraph <- function(i) {
  plot(data$x[data$z == i], 
       data$y[data$z == i], 
       title(title))
}

saveGIF(
  for(i in 1:4){
    title <- paste("Cycle", i, sep=" ")
    mygraph(i)
  }, interval = 0.5, ani.width = 640, ani.height = 480)

This works, but if the function mygraph is instead:

mygraph <- function(i) {
  ggplot() +
    geom_point(aes(x=data$x[data$z == i], y=data$x[data$z == i]))
}

...then it gives me the error as stated above.

like image 843
mjlacey Avatar asked Mar 02 '15 12:03

mjlacey


People also ask

Why is my ggplot2 plot not showing up in R?

This error occurs when you attempt to create a plot using the ggplot2 data visualization package in R, but accidently place the plus (+) sign at the beginning of a new line instead of the end of the current line. The following example shows how to fix this error in practice.

How to plot curves in ggplot2 without animating?

If you want to plot these curves without animating them, the code is extremely easy with ggplot2: ? If you want animate the plot, you can use gganimate.

How to add frames to a ggplot2 plot?

Now, along with data and layout, frames is added to the keys that figure allows. Your frames key points to a list of figures, each of which will be cycled through upon instantiation of the plot. To add options to the plot, first convert ggplot2 plot to Plotly variable with ggplotly () and then, add options to that variable.

Why does my plot return invalid graphics state?

However, it might happen that the error message “invalid graphics state” is returned. This can happen due to multiple reasons. However, usually you have tried to plot any other data before or you might have changed some plotting parameters using R codes such as par and mfrow.


1 Answers

This appears to work if you wrap ggplot in a print() statement, e.g.

mygraph <- function(i) {
  g <- ggplot() +
    geom_point(aes(x=data$x[data$z == i], y=data$x[data$z == i]))
  print(g)
}

This is a variant of R-FAQ 7.22, Why do lattice/trellis graphics not work?

like image 98
Ben Bolker Avatar answered Oct 11 '22 06:10

Ben Bolker