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 <- 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 <- read.table(paste(i, '.dat', sep=''))  
        e <- extent(150,200,150,200)
        r <- raster(e, ncol=10, nrow=20)
        x <- rasterize(df0[,2:3], r, df0[,5], fun=mean)  
        rass <- 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.
5
  • 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, 2021 at 20:18
  • @Spacedman, I just edited. It is too heavy to share. I hope this helps.
    – Amber
    Jul 19, 2021 at 20:34
  • writeRaster(x, filename[i]) should be writeRaster(x, filenames[i]) ?
    – Spacedman
    Jul 19, 2021 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, 2021 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: gis.stackexchange.com/questions/302831/… Anyway, It seems this could be a good way: writeRaster(x, paste0(filenames[i], ".tif")) as you suggested.
    – Amber
    Jul 19, 2021 at 21:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.