geom_contour在gganimate中失败,但在ggplot2中有效

geom_contour在gganimate中失败,但在ggplot2中有效,r,graphics,ggplot2,gganimate,R,Graphics,Ggplot2,Gganimate,我有一个无法解决的问题。我试图创建一个动画使用gganimate与几何轮廓。当我将数据帧绘制为单个“帧”时,它不会出现任何问题;但是,当我添加“帧”美学并尝试使用gganimate运行它时,geom_contour无法工作。我不确定谷歌搜索后会发生什么,因为它是完全相同的数据帧。此外,它还可以与geom_光栅配合使用。我提供了一个非常小的例子,它代表了我实际上试图做的事情,尽管规模较小 任何帮助都将不胜感激。谢谢 library(mvtnorm) library(ggplot2) library

我有一个无法解决的问题。我试图创建一个动画使用gganimate与几何轮廓。当我将数据帧绘制为单个“帧”时,它不会出现任何问题;但是,当我添加“帧”美学并尝试使用gganimate运行它时,geom_contour无法工作。我不确定谷歌搜索后会发生什么,因为它是完全相同的数据帧。此外,它还可以与geom_光栅配合使用。我提供了一个非常小的例子,它代表了我实际上试图做的事情,尽管规模较小

任何帮助都将不胜感激。谢谢

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)
库(mvtnorm)
图书馆(GG2)
库(gganimate)

generatelatice原因很简单,就是
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 
我经常发现使用,比如说,
color
而不是
frame
很有帮助。如果这不起作用,则强烈提示问题不在于
gganimate
。这里的问题是,您需要使用
美学,以便
几何轮廓
知道哪些数据点属于一起:

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

现在它也可以与动画一起工作:

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

p~~我很困惑您在
#works
下的绘图和
#fails
~~Nevermind~下标记的绘图中都指定了
frame
。请重新阅读您的帖子。谢谢您回答我的问题@Stibu!我最终使用基本图形来完成我想要的(结果),但我将尝试使用ggplot2。