使用不同Linux / Windows操作系统的R animation / gganimate包的Gif / Mp4质量

时间:2017-05-05 02:28:42

标签: r ggplot2 imagemagick gganimate ggforce

使用相同的代码脚本,R / Imagemagick根据运行脚本的操作系统生成不同图像质量的输出。值得注意的是,Windows版本中的geoms和text明显更加像素化。

以下是通过以下代码脚本创建的Linux (ubuntu 16.04)Windows (I believe 7 Professional) GIF的链接,通过Thomas Pederson的tweenr package自述文件:

library(ggplot2)
library(gganimate)
library(ggforce)
library(tweenr)

# this line is included to link to imagemagick in Windows, not needed in Linux
ani.options(convert = 'C:/Program Files/ImageMagick-7.0.4-Q16/magick.exe')

# Making up data
t <- data.frame(x=0, y=0, colour = 'forestgreen', size=1, alpha = 1, 
                stringsAsFactors = FALSE)
t <- t[rep(1, 12),]
t$alpha[2:12] <- 0
t2 <- t
t2$y <- 1
t2$colour <- 'firebrick'
t3 <- t2
t3$x <- 1
t3$colour <- 'steelblue'
t4 <- t3
t4$y <- 0
t4$colour <- 'goldenrod'
t5 <- t4
c <- ggforce::radial_trans(c(1,1), c(1, 12))$transform(rep(1, 12), 1:12)
t5$x <- (c$x + 1) / 2
t5$y <- (c$y + 1) / 2
t5$alpha <- 1
t5$size <- 0.5
t6 <- t5
t6 <- rbind(t5[12,], t5[1:11, ])
t6$colour <- 'firebrick'
t7 <- rbind(t6[12,], t6[1:11, ])
t7$colour <- 'steelblue'
t8 <- t7
t8$x <- 0.5
t8$y <- 0.5
t8$size <- 2
t9 <- t
ts <- list(t, t2, t3, t4, t5, t6, t7, t8, t9)

tweenlogo <- data.frame(x=0.5, y=0.5, label = 'tweenr', stringsAsFactors = F)
tweenlogo <- tweenlogo[rep(1, 60),]
tweenlogo$.frame <- 316:375

# Using tweenr
tf <- tween_states(ts, tweenlength = 2, statelength = 1, 
                   ease = c('cubic-in-out', 'elastic-out', 'bounce-out', 
                            'cubic-out', 'sine-in-out', 'sine-in-out', 
                            'circular-in', 'back-out'), 
                   nframes = 375)

# Animate with gganimate
p <- ggplot(data=tf, aes(x=x, y=y)) + 
  geom_text(aes(label = label, frame = .frame), data=tweenlogo, size = 13) + 
  geom_point(aes(frame = .frame, size=size, alpha = alpha, colour = colour)) + 
  scale_colour_identity() + 
  scale_alpha(range = c(0, 1), guide = 'none') +
  scale_size(range = c(4, 60), guide = 'none') + 
  expand_limits(x=c(-0.36, 1.36), y=c(-0.36, 1.36)) + 
  theme_bw()
animation::ani.options(interval = 1/15)
gganimate(p, "dancing ball.gif", title_frame = F, ani.width = 800, 
           ani.height = 800)

最后,以下是在Ubuntu上运行的动画选项:

> animation::ani.options()
$interval
[1] 0.06666667

$nmax
[1] 50

$ani.width
[1] 480

$ani.height
[1] 480

$imgdir
[1] "images"

$ani.type
[1] "png"

$ani.dev
[1] "png"

$title
[1] "Animations Using the R Language"

$description
[1] "Animations generated in R version 3.4.0 (2017-04-21) using the package animation"

$verbose
[1] TRUE

$loop
[1] TRUE

$autobrowse
[1] TRUE

$autoplay
[1] TRUE

$use.dev
[1] TRUE

$ffmpeg
[1] "ffmpeg"

由于在创建可视化方面存在相当多的依赖关系,因此我对在何处解决更改感到有些困惑。需要进行哪些修改才能提高Windows版本的质量?我听说Imagemagick中的转换已被弃用,可能与该连接有关吗?

1 个答案:

答案 0 :(得分:0)

尝试使用res值。 300的分辨率可能会改变所有内容的大小,因此请密切注意。

gganimate(p, "dancing ball.gif", title_frame = F, ani.width = 800, 
           ani.height = 800,res=300)