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.
writeRaster(x, filename[i])
should bewriteRaster(x, filenames[i])
?writeRaster(x, paste0(filenames[i], ".tif"))
if you want to save as aTIF
file.