Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# ======================================================================================
# Creating graticule lines and labels
# ======================================================================================
library(raster)
# first need to create a bounding box
b.box <- as(extent(180, -180, -90, 90), "SpatialPolygons")
# assign CRS
proj4string(b.box) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
# create gridlines baes on the bounding box
grid <- gridlines(b.box,
easts = seq(from=-180, to=180, by=20),
norths = seq(from=-90, to=90, by=10))
plot(grid)
# create the labels if needed
grid.lbl <- labels(grid, side = 1:4)
# ________________________________________________________________
# _____ Optional (in case you plot with ggplot)
# Transform gridlines from SpatialLines to SpatialLinesDataFrame
grid.2 <- SpatialLinesDataFrame(sl = grid, data = data.frame(ID=1:2), match.ID = FALSE)
# Transform the grid.lbl from SpatialPointsDataFrame to data frame
grid.lbl.df <- cbind(grid.lbl@coords, grid.lbl@data)
names(grid.lbl.df)[1:2] <- c("lon", "lat")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment