在R

时间:2016-12-20 20:44:19

标签: r ggplot2 gganimate

我正在尝试使用gganimate创建动画

为了制作我想要的动画,我遵循了这个教程: https://github.com/dgrtwo/gganimate基本上完全符合我的要求。

数据可以在下面找到

这是我的情节的代码

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)

问题是当我运行此代码时,我不断收到此错误,

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

我不确定我是否理解此错误,但我尝试删除interval = 0.1,当我这样做时,我收到此错误:

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

有人知道我做错了什么吗?

谢谢

以下是数据样本:

    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 个答案:

答案 0 :(得分:3)

提供的其他两个答案适用于Windows,但如果您是Mac用户,则可以使用brew install imagemagick安装Image Magick。

升级到Sierra / High Sierra之后,我确实遇到了这个方法的其他问题,因为我不能再写/usr/local/Cellar了,但能够通过重新安装Homebrew(/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)")解决这个问题然后使用sudo chown -R $(whoami) $(brew --prefix)/*更新我的权限。希望这有帮助!

答案 1 :(得分:1)

这就是我在Windows上解决这个问题的方法; 我从他们的网站https://www.imagemagick.org/script/download.php下载了与计算机兼容的ImageMagick可执行文件 并安装它。 然后我使用下面的代码在R-studio中设置环境路径;

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

然后我安装了动画包并加载了它

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

然后我将动画选项配置为;

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

然后我重新运行我的gganimation

gganimate(g, interval=0.2)

这对我有用

答案 2 :(得分:0)

使用installr软件包在您的计算机上安装ImageMagick。

install.packages("installr")
install.ImageMagick()

答案 3 :(得分:0)

如果使用Windows,在安装Image Magick时,请确保勾选“安装遗留实用程序”框,(这可能会解释“我无法找到带有convert ='convert'的ImageMagick”错误。 此外,您应确保选择将ImageMagick添加到系统Path变量的选项。

假设您有最新的R安装,我发现类似的绘图问题/错误消息已通过重新安装Image Magick并在此过程中选择这两个选项来解决。

相关问题