0

I would seek help to revise this script. I need to generate multiple plots.

For example, the following code works well ggplot and gsave but I would prefer to use raster, and I tried something such as above, but I am new with this package/library

      setwd ('~/Documents/test')
      file_list <- list.files(path='~/Documents/test')
      filenames <- gsub('\\.dat$','', list.files(pattern='\\.dat$'))
  for(i in filenames){
  df &lt;- read.table(paste(i, '.dat', sep=''))
  pl = ggplot(df, aes(x = V2, y = V3, color = V5)) + 
  geom_point(shape=22,size=3,alpha = 1,na.rm=TRUE,aes(fill=V5))+  
  #scale_color_continuous(low = 'blue', high = 'red')  
  ggtitle('Temperature - 2010-2020') +
  ggsave(plot = pl, filename = paste0('tp_', i, '.png'))
  }


################################### second option with 'Raster'

    for(i in filenames){
    df0 &lt;- read.table(paste(i, '.dat', sep=''))  
    e &lt;- extent(150,200,150,200)
    r &lt;- raster(e, ncol=10, nrow=20)
    x &lt;- rasterize(df0[,2:3], r, df0[,5], fun=mean)  
    rass &lt;- writeRaster(x, filename[i])  ' ## This line (ras) cause the error
    }


I got the error- Error in h(simpleError(msg, call)) : error in evaluating the argument 'filename' in selecting a method for function 'writeRaster': object of type 'closure' is not subsettable

Using GNU R Version 4.0.2.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Amber
  • 39
  • 4
  • What line causes that error? If you don't know, take lines out and add one line at a time until you get the error. We don't have your data so unless you can help us with that we can't run your code which means we can't replicate your error easily. – Spacedman Jul 19 '21 at 20:18
  • @Spacedman, I just edited. It is too heavy to share. I hope this helps. – Amber Jul 19 '21 at 20:34
  • writeRaster(x, filename[i]) should be writeRaster(x, filenames[i]) ? – Spacedman Jul 19 '21 at 20:49
  • 2
    Or even maybe writeRaster(x, paste0(filenames[i], ".tif")) if you want to save as a TIF file. – Spacedman Jul 19 '21 at 20:51
  • @Spacedman thanks, yet, hasn't work with neither, but I got now another problem to solve. I have been checking how to get rid of the problem: In .gd_SetProject(object, ...) : NOT UPDATED FOR PROJ >= 6 Checking your suggestions here: https://gis.stackexchange.com/questions/302831/save-raster-tif-with-r-writeraster-function Anyway, It seems this could be a good way: writeRaster(x, paste0(filenames[i], ".tif")) as you suggested. – Amber Jul 19 '21 at 21:42

0 Answers0