problems creating plot animations with gganimate in R

6.9k views Asked by At

I am trying to create animation using gganimate

To make the animation I wanted, I followed this tutorial: https://github.com/dgrtwo/gganimate which does basically what I want.

data can be found below

here is the code for my plot

library(ggplot2)
library(gganimate)
install.packages("cowplot")
library(cowplot)

devtools::install_github("dgrtwo/gganimate")

theme_set(theme_bw())
v <- ggplot(Average_position_salary, aes(x = Average_WAR, y = Average_Salary, frame = year)) +   
geom_point()

gganimate(v, title_frame = FALSE, interval = 0.1)

The problem is when I run this code, I keep getting this error,

Error in check.options(new, name.opt = ".X11.Options", envir = .X11env) : 
  invalid argument name ‘interval’ in 'grDevices::png(..., res = dpi, units = "in")'

I'm not sure I understand this error, but I tried removing interval = 0.1 and when I do that I get this error:

sh: convert: command not found
Error in cmd.fun(sprintf("%s --version", convert), intern = TRUE) : 
  error in running command
I cannot find ImageMagick with convert = 'convert'
Error in file(file, "rb") : cannot open the connection
In addition: Warning messages:
1: In animation::im.convert(filenames[-1], basename(filename), extra.opts = opts,  :
  Please install ImageMagick first or put its bin path into the system PATH variable
2: In file(file, "rb") :
  cannot open file '/var/folders/ft/7dytfk1d6v50xmp71h6txgrh0000gn/T//RtmpekDeyj/gganimate/file38f501e67b9.gif': No such file or directory

Does anybody know what I'm doing wrong?

Thank you

Here is a sample of the data:

    Position year Average_Salary Average_WAR
24         2 1985       508398.2   1.5117647
25         2 1986       375056.4   0.9309091
26         2 1987       369100.2   0.6736842
27         2 1988       350883.5   0.6741935
28         2 1989       395239.8   0.5896552
29         2 1990       369823.7   0.6753623
30         2 1991       524865.5   0.6210526
31         2 1992       726446.3   0.6779661
32         2 1993       693320.1   0.8317460
33         2 1994       731309.5   0.4952381
34         2 1995       657469.8   0.5902778
35         2 1996       739594.1   0.7774194
36         2 1997       886851.8   0.8698413
37         2 1998       961986.3   0.5849315
38         2 1999      1112187.0   0.6171053
39         2 2000      1521829.2   1.0368421
40         2 2001      1794504.4   0.6830508
41         2 2002      2155796.8   0.6537037
42         2 2003      2310309.2   0.9947368
43         2 2004      1777468.4   0.6814815
44         2 2005      2053411.3   0.6758621
45         2 2006      2076933.3   0.7677966
46         2 2007      2148252.9   0.6316667
47         2 2008      2122456.8   0.8619048
48         2 2009      1849889.7   0.7250000
49         2 2010      1959554.4   0.7833333
50         2 2011      1707032.1   0.9651515
51         2 2012      2095852.0   0.9984127
52         2 2013      2285045.4   0.9940299
53         2 2014      2402914.8   1.0936508
54         2 2015      2598519.3   0.6887097
55         3 1985       580910.9   1.2560976
56         3 1986       593094.4   1.1116279
57         3 1987       580782.4   1.6777778
58         3 1988       527230.1   0.9952381
59         3 1989       700261.7   1.3487179
60         3 1990       682741.7   1.0232143
61         3 1991      1131729.7   1.1216216
62         3 1992      1615097.2   1.5416667
63         3 1993      1350687.5   1.4583333
64         3 1994      1443432.7   1.1733333
65         3 1995      2002190.4   1.3914894
66         3 1996      1913476.7   1.4160000
67         3 1997      2739371.2   1.9611111
68         3 1998      2188290.6   1.6437500
69         3 1999      1894013.6   1.4460000
70         3 2000      3058889.0   1.7181818
71         3 2001      3322784.5   1.6097561
72         3 2002      3923671.2   1.6065217
73         3 2003      3947089.0   1.1311111
74         3 2004      4328143.5   1.4369565
75         3 2005      3755095.9   1.4875000
76         3 2006      3048467.7   1.3591837
77         3 2007      3354178.6   0.8843137
78         3 2008      5355219.8   1.3130435
79         3 2009      4264821.3   1.4732143
80         3 2010      5519959.9   1.5813953
81         3 2011      4736312.7   1.4162791
82         3 2012      4738315.8   0.7580000
83         3 2013      4838826.5   1.1980392
84         3 2014      5433374.2   1.1129630
85         3 2015      5965488.6   1.4191489
4

There are 4 answers

0
Grace Mahoney On BEST ANSWER

The other two answers provided are good for Windows, but if you are a Mac user you can install Image Magick just using brew install imagemagick.

I did run into additional problems with this method after upgrading to Sierra/High Sierra because I no longer could write to /usr/local/Cellar, but was able to get around this issue by reinstalling Homebrew (/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)") and then updating my permissions with sudo chown -R $(whoami) $(brew --prefix)/*. Hope this helps!

1
FilipW On

Use the installr package to install ImageMagick on your computer.

install.packages("installr")
install.ImageMagick()
0
Mc Everest On

This is what I did to get over that problem on windows; I downnloaded my computer compatible ImageMagick executable from their website https://www.imagemagick.org/script/download.php and installed it. Then I set an environment path in R-studio using the code below;

Sys.setenv(PATH = paste("C:/PROGRA~1/ImageMagick-7.0.7-Q8",
Sys.getenv("PATH"), sep = ";"))

I then installed the animation package and loaded it

install.packages("animation")
library(animation)

I then configured the animation options as;

ani.options(convert = 'C:/PROGRA~1/ImageMagick-7.0.7-Q8/convert.exe')

and i then re-run my gganimation as

gganimate(g, interval=0.2)

This worked for me

0
johnm On

If using Windows, when installing Image Magick ensure you tick the "install legacy utilities" box, (which may explain the "I cannot find ImageMagick with convert = 'convert'" error. Also you should ensure you select the option to add ImageMagick to the system Path variable.

Assuming you have an up to date R install, I found similar plotting issues/error message were resolved by reinstalling Image Magick and selecting both of these options during the process.