Last active
April 4, 2018 21:31
-
-
Save oscarperpinan/5d5bb51ff419621d5f63429b8f88d679 to your computer and use it in GitHub Desktop.
First attempt of using graticule with rasterVis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(raster) | |
library(rgdal) | |
library(rasterVis) | |
library(graticule) | |
## Only needed for downloading a raster example | |
library(meteoForecast) | |
today <- Sys.Date() | |
testDay <- today - 7 | |
## Retrieve raster data | |
r <- getRaster('temp', day = testDay, frames = 1) | |
## Here is where the graticule routine starts | |
crs.longlat <- CRS("+init=epsg:4326") | |
prj <- CRS(projection(r)) | |
extLL <- projectExtent(r, crs = crs.longlat) | |
lons <- pretty(c(xmin(extLL), xmax(extLL))) | |
lats <- pretty(c(ymin(extLL), ymax(extLL))) | |
## optionally, specify the extents of the meridians and parallels | |
## here we push them out a little on each side | |
xl <- range(lons) + c(-0.4, 0.4) | |
yl <- range(lats) + c(-0.4, 0.4) | |
## build the lines with our precise locations and ranges | |
grat <- graticule(lons, lats, proj = prj, | |
xlim = xl, ylim = yl) | |
## Labels | |
labs <- graticule_labels(lons, lats, | |
xline = lons[2], | |
yline = lats[2], | |
proj = prj) | |
labsLon <- labs[labs$islon,] | |
labsLat <- labs[!labs$islon,] | |
## Display the raster | |
levelplot(r) + | |
## and the graticule | |
layer(sp.lines(grat)) + | |
layer(sp.text(coordinates(labsLon), | |
txt = parse(text = labsLon$lab), | |
adj = c(1.1, -0.25), | |
cex = 0.6)) + | |
layer(sp.text(coordinates(labsLat), | |
txt = parse(text = labsLat$lab), | |
adj = c(-0.25, -0.25), | |
cex = 0.6)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment