Skip to content

Instantly share code, notes, and snippets.

@oscarperpinan
Last active Apr 4, 2018
Embed
What would you like to do?
First attempt of using graticule with rasterVis
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