3

gridSVGパッケージを使用してアニメーション化されたSVGプロットを作成する方法を学び始めています。テストとして、開始位置から終了位置に移動して、一連のポイントをアニメートしたいと思います。この例は、パッケージの最初のビネットの例に大まかに基づいています。

まず、いくつかのデータ:

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")

単一のグロブ内の複数のポイントをアニメーション化できますか、それともループなしでポイントをアニメーション化できますか?

4

1 に答える 1

3

これは私が使用して動作します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を指定していました。私がビネットを読んだのは、グロブごとに1つのx値で複数のグロブをアニメーション化する場合、IDを指定するだけでよいということです。

于 2011-12-21T17:33:01.163 に答える