2011-12-21 7 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") 

私は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 
) 

あなたはむしろ20より、2ポイントのidを指定しました。ビネットの読み方は、grobごとに1つのx値で複数のgrobsをアニメートするには、idを指定する必要があります。

+0

ああ、私はそれが単純でなければならないことを知っていました。ありがとう。 –

関連する問題