I have a raster that looks like this:
library(raster)
# Create data
set.seed(123)
r <- raster(ncols=10, nrows=10)
r[] <- sample(10:360, ncell(r),replace = T)
crs(r) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
projection(r)
plot(r)
For each grid, the raster contains a day of the year as values. I am looking for a method that generates a map (animation maybe) which goes from day 1 to day 365 and when the map reaches the day which matches with the day of a grid, the grid should turn green.
I am sorry but I cannot produce what I have tried since I have no clue whether such thing is possible in R.
Thank you
EDIT
One way I could think of is run a loop like this:
for(i in 2:365){
breakpoints <- c(1,i,365)
colors <- c("green4","white")
plot(r, breaks = breakpoints,col=colors)
}
Save the ouptut of each loop as .png and then develop a .gif image of it
Using a loop, you can reclassify
your raster into a binary raster (0 if lower or equal to the day, 1 if higher). Then, you can use the animation
package to create the gif.
library(animation)
ani.options(interval=.05)
saveGIF({
for (i in 1:365){
m <- c(1, i, 0, i, 365, 1)
rclmat <- matrix(m, ncol=3, byrow=TRUE)
rc <- reclassify(r, rclmat)
plot(rc, col=c("green3", "white"), legend=FALSE, main = paste("Day", i))
}
})
Demo (200 days)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With