I'm having trouble rasterizing a data.frame using the raster package in R.
My data.frame contains environmental data for the world oceans (temperature etc.) with their coordinates (grid 0.5*0.5), decimal longitude from -90 to 90 and decimal latitude from -180 to 180. So the base contains 90*2*2 x 180*2*2 = 360*720 = 259200 rows, and 59 colones (57 variables + 2 colones of coordinates).
After rasterizing, this is what I get with plot(r)
: http://postimg.org/image/rqocxcbi3/
So, a duplicate image, in the wrong direction.
My code is:
FILE_ENV = read.csv('ENV_DATABASE.csv')
coordinates(FILE_ENV) <- ~LON+LAT
proj4string(FILE_ENV3)=CRS("+init=epsg:4326")
FILE_ENV = spTransform(FILE_ENV,CRS("+init=epsg:4326"))
gridded(FILE_ENV) = TRUE
r = raster(FILE_ENV)
plot(r)
Can anyone see what I'm missing/screwing up here?
Thanks
Edit :
head(FILE_ENV)
LON LAT BAT BAT_CLASSE SLOPE SEDIMENT SST SST_SEAFLOOR SST_SUM SST_WIN SAL_SURF SAL_SEAFLOOR...
1 -179.75 89.75 2804 NA 0.14031838 NA NA NA NA NA NA NA
2 -179.25 89.75 2941 NA 0.12495525 NA NA NA NA NA NA NA
3 -178.75 89.75 3048 NA 0.07784129 NA NA NA NA NA NA NA
4 -178.25 89.75 3093 NA 0.03123910 NA NA NA NA NA NA NA
5 -177.75 89.75 3109 NA 0.01536359 NA NA NA NA NA NA NA
6 -177.25 89.75 3063 NA 0.15619729 NA NA NA NA NA NA NA
Since you did not show head(FILE_ENV)
(d
in my code) it hard to say how exactly you should to this. But in essence you should be able to do something like the below:
library(raster)
d <- read.csv('ENV_DATABASE.csv')
r <- rasterFromXYZ(d, crs="+proj=longlat +datum=WGS84")