2011-12-21 80 views
2

我开始学习如何使用gridSVG包创建动画SVG图。作为一项测试,我想要制作一组点,从开始位置移动到完成位置。该示例松散地基于包的first vignette中的那些示例。如何以矢量化方式为gridSVG动画点?

首先,一些数据:

n_points <- 20 
start <- data.frame(
    x = runif(n_points), 
    y = runif(n_points) 
) 
end <- data.frame(
    x = runif(n_points), 
    y = runif(n_points) 
) 

的基本想法似乎是“画一个新的页面,添加内容的第一帧动画,然后保存为SVG”。我第一次尝试在同一个地方绘制所有点。我不知道如何告诉grid.animate每个点需要单独移动。

grid.newpage() 
with(start, 
    grid.points(x, y, default.units = "npc", name = "points") 
) 
grid.animate(
    "points", 
    x = c(start$x, end$x), 
    y = c(start$y, end$y) 
) 
gridToSVG("point_test1.svg") 

我可以画在自己的GROB每个点,使用lapply避开这个问题。这工作,但感觉笨拙–应该有一个矢量化的方式来做到这一点。

grid.newpage() 
lapply(seq_len(n_points), function(i) 
{ 
    gname = paste("points", i, sep = ".") 
    with(start, 
    grid.points(x[i], y[i], default.units = "npc", name = gname) 
) 
    grid.animate(
    gname, 
    x = c(start$x[i], end$x[i]), 
    y = c(start$y[i], end$y[i]) 
) 
}) 
gridToSVG("point_test2.svg") 

我也尝试过使用animUnit,但我无法弄清楚如何指定id参数。

grid.newpage() 
with(start, 
    grid.points(x, y, default.units = "npc", name = "points") 
) 
x_points <- animUnit(
    unit(c(start$x, end$x), "npc"), 
    timeid = rep(1:2, each = n_points), 
    id = rep(1:2, n_points) 
)   
y_points <- animUnit(
    unit(c(start$y, end$y), "npc"), 
    timeid = rep(1:2, each = n_points), 
    id = rep(1:2, n_points) 
)   
grid.animate( #throws an error: Expecting only one value per time point 
    "points", 
    x = x_points, 
    y = y_points 
) 
gridToSVG("point_test3.svg") 

我可以动画的多个点的单个格罗内,或以其他方式动画点,而一个循环?

回答

2

这工作我使用animUnit为:

grid.newpage() 
with(start, 
    grid.points(x, y, default.units = "npc", name = "points") 
) 
x_points <- animUnit(
    unit(c(start$x, end$x), "npc"), 
    #timeid = rep(1:2, each = n_points), 
    id = rep(1:20, 2) 
)   
y_points <- animUnit(
    unit(c(start$y, end$y), "npc"), 
    #timeid = rep(1:2, each = n_points), 
    id = rep(1:20, 2) 
)   
grid.animate( #throws an error: Expecting only one value per time point 
    "points", 
    x = x_points, 
    y = y_points 
) 

你被指定ID的两点,而不是二十岁。我对这个小插曲的读法是,对于每个grob用单个x值动画多个grob,您只需要指定id。

+0

啊,是的,我知道它必须简单。谢谢。 – 2011-12-21 17:38:18