0

I want to create an animated plot from a text file with two columns: x = timepoints, y = values.

time     values
0       -70.627
1e-5    -68.5964
6e-5    -67.6575
1e-4    -66.6575
1.1e-4  -65.627
0.882119999999  -67.0542
0.882129999999  -67.0847
0.882139999999  -67.0542
0.882149999999  -67.0542

I have more than 10000 rows and want 20 frames.

I have tried the following code:

data <- read.table("my.txt", header = TRUE)
library(animation)
library(magick)

# Also installed ImageMagick 6.9.9.14 on windows that worked perfectly, checked by convert command

saveGIF({
  for(i in 1:20){
    x <- data$time
    y <- data$values
    plot(x,y)
  }
}, ani.width = 1000, ani.height = 800, movie.name = "data.gif", img.name = "data_Rplot")

But, it gives me a static png plot which I can get from:

plot(x,y)

without any loop.

Please guide. Thanks.

OzanStats
  • 2,756
  • 1
  • 13
  • 26
bio8
  • 176
  • 2
  • 15
  • 1
    You need [to make your example reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) by supplying sufficient data to reproduce your plots. – alistaire Feb 27 '18 at 21:23
  • What kind of animation are you trying to create? The expression inside the `for` loop returns the same plot every time (`x` and `y` stays the same). You probably want the expression to depend on `i`, for instance defining `x <- data$time[1:i]` and `y <- data$values[1:i]` – ozacha Feb 27 '18 at 22:16
  • You could do something like `library(ggplot2); ggplot(df, aes(time, values, frame = time, cumulative = TRUE)) + geom_point() + geom_line(); gganimate::gganimate()`, but if your actual data has more points than you want rows, you'll need to create a variable to control the frame. – alistaire Feb 27 '18 at 22:37
  • @ozacha: Thanks, I tried and it worked but showing points and not line. I want to make like this https://commons.wikimedia.org/wiki/File:Hodgkins_Huxley_Plot.gif – bio8 Mar 01 '18 at 12:24
  • I want 20 frames means I want to plot full data in 20 frames. – bio8 Mar 01 '18 at 12:39
  • @alistaire I used it, but getting following error: `Error in if (is.waive(data) || empty(data)) return(cbind(data, PANEL = integer(0))) : missing value where TRUE/FALSE needed` – bio8 Mar 02 '18 at 08:45
  • I am still struggling to get the solution. Any help would be great. Thanks. – bio8 Mar 15 '18 at 11:08

0 Answers0