I have some point data, with longitude, latitude and a value, in .csv file format.
This is how the file looks in QGIS:
I wanted to create a raster of this map, with each of the points representing the center of a cell, in R, as practice. I first created an empty raster in R, and then added the values to the raster, like so:
##This is the datafile with the lon, lat and values:
moisture_data <- read.csv("Suf_tab_lon_lat.csv", sep = ",", header = T)
soil_raster <- extent(c(min(moisture_data$lon) - 0.0625,
max(moisture_data$lon) + 0.0625,
min(moisture_data$lat) - 0.0625,
max(moisture_data$lat) + 0.0625))
soil_raster <- raster(soil_raster)
res(soil_raster) <- 0.125
projection(soil_raster) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
values(soil_raster) <- moisture_data$soil_moisture
However, when I plot the raster, this is how it looks:
I am unable to figure out why the data is getting flipped, and how to set this right. I am not sure how to post my original data, which might be more helpful, apart from simply emailing the CSV file to anyone who requests it.
rasterFromXYZ
will go from a data frame or matrix of rows with (long, lat, value) and build a raster over the bounds with NA in the missing pixels. Try it?