【问题标题】:problems creating plot animations with gganimate in R在 R 中使用 gganimate 创建绘图动画的问题
【发布时间】:2018-02-13 15:36:11
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 你安装了 ImageMagick 吗?您没有interval 的代码对我有用
  • 我尝试安装 ImageMagick 但我收到此错误Warning in install.packages : package ‘ImageMagick’ is not available (for R version 3.3.1) 是否有我可以用来安装它的开发工具
  • ImageMagick 是一个不属于 R 的免费软件。我相信我最初是从 here 下载的。
  • 确保安装 IM 6 或 IM 7,以与您的 R 版本一致。您的平台是什么(Windows、Linux 或 Mac)?请务必为您的平台安装正确的版本。请参阅 imagemagick.org/script/download.php 或从 Homebrew 或 MacPorts 获取。

标签: r ggplot2 gganimate


【解决方案1】:

提供的其他两个答案适用于 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)/* 的权限。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    这就是我在 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)
    

    这对我有用

    【讨论】:

      【解决方案3】:

      使用安装程序包在您的计算机上安装 ImageMagick。

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

      【讨论】:

      • 只是想在此处添加注释,installr 包仅适用于 Windows(文档 here)。虽然从技术上讲,您可以使用 devtools::install_github("talgalili/installr") 通过 GitHub 为 Mac 下载它,但命令 install.ImageMagick 仍然不起作用,因为它会为 Win32 下载 .exe 文件
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-22
        • 2019-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-03
        • 1970-01-01
        • 2020-02-02
        相关资源
        最近更新 更多