I am running a random forest model, predicting on a new dataset and then breaking the prediction into 20 year periods, turning this into a raster and mapping each time period. The below code works great except that I can't get the title of each plot to be unique. I tried creating a list and calling names or colnames, but so far nothing has worked. As is it prints each map with all 3 titles above.
If i try to something like Rasts[l] I get -- Error in Rasts[l] : invalid subscript type 'S4'
My guess is because because this is a raster it is not behaving like a typical dataframe would because none of the potential solutions I've found have worked.
#subsets by year
sp1960 <- predict_all[predict_all$year>1960 &predict_all$year<1980,]
sp1980 <- predict_all[predict_all$year>=1980 &predict_all$year<2000,]
sp2000 <- predict_all[predict_all$year>=2000 &predict_all$year<2020,]
#turn the data into a raster
library(plotKML)
library(RColorBrewer)
Rasts <- list("A"=sp1960,"B"=sp1980,"C"=sp2000)
title_list <- list("A", "B", "C")
par(mfrow = c(2, 2))
sapply(Rasts, function(l) {
new_rasts<-raster(vect2rast(l, fname = names(l)[2], cell.size=.05))
pal <- brewer.pal(n = 9, name = "PuBu")
scale_range <- c(0, 1)
title <-paste("RF:",title_list,sp_name)
plot(new_rasts, col=pal,zlim=scale_range, main=title)
plot(wrld_simpl, add=TRUE, border='dark grey')
})