1

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.

enter image description here

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')
})
MyNameisTK
  • 209
  • 1
  • 2
  • 15
  • 3
    It would be easier to help you if provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Val Oct 10 '18 at 07:34
  • @Val Completely agree. I'm not sure how to provide something reproducible since I'm working with spatial data frames – MyNameisTK Oct 10 '18 at 15:02
  • You can always create a spatial dataframe with the same characteristics of your data just with random values ... or if the size permits, you can use `dput` – Val Oct 11 '18 at 07:17

1 Answers1

1

Problem appears to be, in your original sample code you are pasting the entire list of titles in title_list into title of every graph.

It's not possible to test without you having included a minimal reproducible example.

However, taking a stab at it you might try modifying your sapply statement to run over the index to the Rasts list. By using an index for the sapply loop, you can reference e.g. Rasts[i] and title_list[i] by the index number and match them as you go.

For example, I think you would need to modify your sapply call to be something like:

sapply(seq_along(Rasts), function(i) {
  new_rasts<-raster(vect2rast(Rasts[i], fname = names(Rasts[i])[2], cell.size=.05))
  pal <- brewer.pal(n = 9, name = "PuBu")
  scale_range <- c(0, 1)
  title <-paste("RF:",title_list[i],sp_name)
  plot(new_rasts, col=pal,zlim=scale_range, main=title)
  plot(wrld_simpl, add=TRUE, border='dark grey')
})

Above, (as you havn't defined it anywhere in your OP) I'm guessing from your sample output that sp_name is a character constant (i.e. for lack of other information it appears as the same text in all your graph title outputs).

If however sp_name is a list, simply please replace it in the function with sp_name[i] to loop within the sapply along with the other parallel lists. Same if wrld_simpl is a list, replace it in the function with wrld_simpl[i].

krads
  • 1,350
  • 8
  • 14
  • Thank you! This worked perfectly with only one small change i had to use Rasts[[i]] in double brackets. No idea why but this is how each spatial data frame appeared in the Rasts list. For anyone else -- With only one bracket I received the error: unable to find an inherited method for function ‘vect2rast’ for signature ‘"list"’ – MyNameisTK Oct 10 '18 at 15:17