动态图生成方法
静态图有时候无法完整表现数据的动态变化过程,现有R包ImageMagick可以生成动态gif图,方便数据分析师查看数据变化过程。更多知识分享请到 https://zouhua.top/。
准备包
- ImageMagick :产生动画
install.packages("installr")
installr::install.ImageMagick("http://www.imagemagick.org/script/download.php")
- gganimate:制作动画,可以映射
install.packages("devtools")
devtools::install_github("dgrtwo/gganimate")
- animation : 保存gif等
install.packages("animation")
运行
library(gapminder)
library(gganimate)
library(animation)
library(ggplot2)
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, color=continent,frame=year))+
geom_point()+
scale_x_log10()
magickPath <- shortPathName("C:\\Program Files\\ImageMagick-7.0.7-Q16\\magick.exe") # 设置路径
ani.options(convert=magickPath) # 设置convert参数
gganimate(p, "out.gif") # 生成且保留gif
总结
1. c:\program 路径无法找到:环境变量设置
Sys.setenv(PATH = paste("C:/Program Files/ImageMagick-7.0.7-Q16/bin",
Sys.getenv("PATH"), sep = ";"))
2. ImageMagick旧版本的convert不可用(将png转成gif的工具),使用谢大神的新版本animation
devtools::install_github("yihui/animation").
参考
参考文章如引起任何侵权问题,可以与我联系,谢谢。
网友评论