building my path in R, So I am still very bad especially in running loops and complexes functions...
What I am interested about is to automatically read a series of rasters in a particular folder and generate a simple ggplot visualization out of it (for each one of the rasters). Possibly exporting it to some folder would be also nice.
The further idea would be to overlap these visualizations and create an animated GIF, but this I can perhaps do outside of R... Just creating the maps would already be really nice...
With this I can load all rasters in my R enviroment:
myfiles = list.files(pattern = "*.asc")
for (i in 1:length(myfiles)) assign(myfiles[i], raster::raster(myfiles[i]))
While the basic ggplot code to build one of the map would be:
#Loading an example raster
hflow = raster::raster("D:/R.avaflow_graphs/Erla_E1_F01_results/Erla_E1_F01_ascii/Erla_E1_F01_hflow0000.asc")
hflow_spdf <- as(hflow, "SpatialPixelsDataFrame") # Creating a spdf
hflow_df <- as.data.frame(hflow_spdf) # Creating a df
colnames(hflow_df) <- c("value", "x", "y") # Assign x,y,values
ggplot() + # plotting the map
geom_tile(data=hflow_df, aes(x=x, y=y, fill=value), alpha=0.8) +
coord_equal()
For inspiration, I've been checking these 3 links, but still couldn't make it happen... R apply raster function to a list of characters https://gis.stackexchange.com/questions/377444/plotting-a-raster-stack-with-ggplot2 https://www.r-bloggers.com/2020/07/writing-functions-to-automate-repetitive-plotting-tasks-in-ggplot2/
Thanks beforehand! PL