2016-08-06 8 views
3

私は解決できない問題があります。私はgeom_contourでgganimateを使ってアニメーションを作成しようとしています。データフレームを単一の「フレーム」としてプロットすると、問題なく動作します。しかし、私は "フレーム"の美学を追加し、gganimateでそれを実行しようとすると、geom_contourは動作しません。私はまったく同じデータフレームであるので、何が起きているのかわからない。さらに、geom_rasterでうまく動作します。私は小規模ながらも実際に何をしようとしているかを表す非常に小さな例を提供しました。geom_contourはgganimateで失敗しますが、ggplot2で動作します

ご協力いただければ幸いです。ありがとう!

library(mvtnorm) 
library(ggplot2) 
library(gganimate) 

generateLattice <- function(theta,offset,increment){ 
    dim1 <- c(seq(from=theta[1]-offset,to=theta[1]-increment,by=increment),seq(from=theta[1],to=theta[1]+offset,by=increment)) 
    dim2 <- c(seq(from=theta[2]-offset,to=theta[2]-increment,by=increment),seq(from=theta[2],to=theta[2]+offset,by=increment)) 
    lattice <- expand.grid(dim1,dim2) 
    return(lattice) 
} 

testLattice <- generateLattice(c(5,5),10,0.05) 
testPDF <- apply(testLattice,1,function(x){ 
    dmvnorm(x=x,mean=c(5.5,4.5),sigma=matrix(c(1,0.25,0.25,1),2,2)) 
}) 
testLattice$PDF <- testPDF 
testLattice$iter <- 1 

testLattice1 <- generateLattice(c(6,6),10,0.05) 
testPDF1 <- apply(testLattice1,1,function(x){ 
    dmvnorm(x=x,mean=c(5.0,4.75),sigma=matrix(c(0.9,0.15,0.15,1.2),2,2)) 
}) 
testLattice1$PDF <- testPDF 
testLattice1$iter <- 2 

testLatticeGIF <- rbind(testLattice,testLattice1) 

ggplot(testLatticeGIF[testLatticeGIF$iter==1,],aes(x=Var1,y=Var2,z=PDF)) + 
    geom_contour() 

#works 
p <- ggplot(testLatticeGIF,aes(x=Var1,y=Var2,fill=PDF,frame=iter)) + 
    geom_raster() 
gganimate::gg_animate(p) 

#fails 
p <- ggplot(testLatticeGIF,aes(x=Var1,y=Var2,z=PDF,frame=iter)) + 
    geom_contour() 
gganimate::gg_animate(p) 
+0

~~私は混乱し、あなたが '#のworks''下のプロットとアンクルマーク1の両方に指定されたframe'を持っているんですr '#fails'。~~ – rensa

+0

Nevermind〜あなたの投稿を再読してください。 – rensa

答えて

3

理由はgeom_contour()は、あなたがそれに渡す形式でデータを処理するために知っていないということだけです。次の二つのプロットは動作しません。両方:

ggplot(testLatticeGIF,aes(x=Var1,y=Var2,z=PDF)) + 
    geom_contour() 
## Warning message: 
## Computation failed in `stat_contour()`: 
## dimensions of 'x', 'y' and 'z' do not match 
ggplot(testLatticeGIF,aes(x=Var1,y=Var2,z=PDF,colour=iter)) + 
    geom_contour() 
## Warning message: 
## Computation failed in `stat_contour()`: 
## dimensions of 'x', 'y' and 'z' do not match 

私は、多くの場合、たとえば、使用することが有益colour代わりのframeを見つけます。これが機能しない場合、それは問題はgganimateではない強力なヒントがあります。

ggplot(testLatticeGIF,aes(x=Var1,y=Var2,z=PDF,group=iter,colour=iter)) + 
    geom_contour() 

enter image description here

そして今、それはまた、アニメーションで動作します:

p <- ggplot(testLatticeGIF,aes(x=Var1,y=Var2,z=PDF,group=iter,frame=iter)) + 
     geom_contour() 
gganimate::gg_animate(p) 
ここで、問題はあなたが geom_contourポイントが一緒に属するデータを知っているよう group美学を使用する必要があるということです

enter image description here

+1

私の質問にお答えいただきありがとう@Stibu!私は基本的なグラフィックスを使用して私が望んだことをしました(結果のためのhttp://i.imgur.com/XoYfbD2.gifv)が、ggplot2を使ってそれを行っていきます。 – slwu89

関連する問題