[英]Add vector world map to raster animation in R
I have created an animation with the animate
function of the raster
package and would like to add a vector map of the world on it. 我用
raster
包的animate
功能创建了一个动画,并希望在上面添加世界的矢量地图。
This is my code where I create a raster brick from a 3D array of monthly mean temperatures before animating it: 这是我的代码,在此之前,我会根据3D的月平均温度数组创建一个动画砖,然后对其进行动画处理:
r <- brick(ncols=nb.cols, nrows=nb.rows)
r <- setValues(r, monthly.mean)
animate(r)
The raster animation works great but how can I add a vector map of the world? 栅格动画效果很好,但是如何添加世界矢量地图?
Many thanks 非常感谢
I doubt you can, but you can use the animation
package to create a web page animation with play/stop/loop controls etc. 我怀疑您可以,但是您可以使用
animation
包创建带有播放/停止/循环控件等的网页动画。
You will have to write a function to draw each frame of your animation with the raster layer and the vector map, and then you stick that in the saveHTML
function of the animation
package. 您将必须编写一个函数来绘制带有栅格图层和矢量贴图的动画的每一帧,然后将其粘贴到
animation
包的saveHTML
函数中。
Example (not tested): 示例(未经测试):
saveHTML( { for(f in 1:12){plot(r[[i]]; plot(world, add=TRUE)} })
should create an HTML file and open your browser to view it. 应该创建一个HTML文件并打开浏览器进行查看。 To make this example work you will need a
world
vector object in the same coordinate system. 要使此示例工作,您将需要在相同坐标系中的
world
矢量对象。 You might also want to set the zlim
when you plot the raster so the scale doesn't change. 绘制栅格时,您可能还想设置
zlim
,以使比例不变。
You can use the addfun
argument 您可以使用
addfun
参数
library(raster)
# example data
r <- raster(nrows=10, ncols=10)
s <- stack(replicate(10, setValues(r, runif(ncell(r)))))
xy <- cbind(-180 + runif(10) * 360, -90 + runif(10) * 180)
# create a function to add some vector data
fun <- function() {
points(xy, cex=2)
points(xy, pch=3, col='red')
}
# use it
animate(s, addfun=fun)
Thank you! 谢谢! both methods worked like a charm!
两种方法都像魅力一样!
A quick answer for those interested: 对那些感兴趣的人的快速解答:
With saveHTML: 使用saveHTML:
require(animation)
require(rgdal)
require(raster)
countries <- readOGR(dsn=".", layer="Countries") # Shapefile of world countries
r <- brick(ncols=144, nrows=72)
r <- setValues(r, monthly.mean) # monthly.mean is a 3D array 144x72x420
saveHTML({
for(m in 1:420) { # serie of 420 months
plot(r[[m]], zlim=c(min, max)) # min, max of the legend
plot(countries, add=T)
}
})
With FUN: 与乐趣:
require(rgdal)
require(raster)
countries <- readOGR(dsn=".", layer="Countries")
r <- brick(ncols=144, nrows=72)
r <- setValues(r, monthly.mean)
fun <- function() {
plot(countries, add=T)
}
animate(r, zlim=c(min, max), addfun=fun)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.