Reputation: 27
I'm a beginner and I have worked a lot on my problem, but I am still stuck...
I have a matrix containing values of world temperature. The size is 360*720. The values are from -180, -90 (Longitude, latitude) to 180, 90 with a resolution of 0.5.
> head(matrix)
NaN. NaN..1 NaN..2 NaN..3 NaN..4, [...] NaN..718 NaN..719
[1,] Na Na Na Na Na Na
[2,] Na Na Na Na Na Na
[3,] Na Na Na Na Na Na
[4,] Na Na Na Na Na Na
[5,] Na Na Na Na Na Na
[6,] -1.6634 -1.6634 -1.6634 -1.6634 -1.6634 -1.7469
...
This matrix "looks like" a raster. My problem is that I don't know how to tell R the matrix resolution.
If I then rasterise and plot with plot(raster(matrix))
, I get this : http://postimg.org/image/gdnblob07/
Yet, I need the axis to be -180;180 (x) and -90;90 (y)
Do you know how I could do?
Thanks a lot
Upvotes: 0
Views: 1370
Reputation: 636
Using the raster
package is the right intuition but when you instantiate it with just the matrix it doesn't get the extents accurately. This will do it:
library(raster)
r <- raster(nrow=360,ncol=720,vals=matrix)
plot(r)
raster
will initialize a raster object with (by default), world boundaries. You can optionally specify the coordinate reference system (e.g., crs="+proj=longlat +datum=WGS84"), and the extents (ymx, ymn, xmx, xmn). cell size is implicit and calculated from the extents and rows/cols.
Upvotes: 2