Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active December 26, 2016 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valentinitnelav/a7871128d58097e9d227f7a04e00134f to your computer and use it in GitHub Desktop.
Save valentinitnelav/a7871128d58097e9d227f7a04e00134f to your computer and use it in GitHub Desktop.
# ======================================================================================
# 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