Skip to content

Commit

Permalink
add_grid
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed May 17, 2024
1 parent 375d0ca commit 2bcdcf4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
29 changes: 29 additions & 0 deletions R/plot_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,33 @@ add_box <- function(...) {
}


add_grid <- function(nx = NULL, ny = nx, col = "lightgray", lty = "dotted", lwd = par("lwd"), equilogs = TRUE) {


p <- terra:::get.clip()

## adapted from graphics::grid
g.grid.at <- function (side, n, log, equilogs, axp, usr2) {
if (is.null(n)) {
stopifnot(is.numeric(ax <- axp), length(ax) == 3L)
if (log && equilogs && ax[3L] > 0)
ax[3L] <- 1
axTicks(side, axp = ax, usr = usr2, log = log)
}
else if (!is.na(n) && (n <- as.integer(n)) >= 1L) {
at <- seq.int(usr2[1L], usr2[2L], length.out = n + 1L)
(if (log)
10^at
else at)[-c(1L, n + 1L)]
}
}

atx <- if (is.null(nx) || (!is.na(nx) && nx >= 1))
g.grid.at(1L, nx, log = par("xlog"), equilogs, axp = par("xaxp"), usr2 = p[1:2])
aty <- if (is.null(ny) || (!is.na(ny) && ny >= 1))
g.grid.at(2L, ny, log = par("ylog"), equilogs, axp = par("yaxp"), usr2 = p[3:4])
abline(v = atx, h = aty, col = col, lty = lty, lwd = lwd)
invisible(list(atx = atx, aty = aty))
}


4 changes: 3 additions & 1 deletion man/box.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
\title{draw a box}

\description{
Similar to \code{\link{box}} allowing adding a box around a map. This function will place the legend in the locations within the mapped area as delineated by the axes.
Similar to \code{\link[graphics]{box}} allowing adding a box around a map. This function will place the legend in the locations within the mapped area as delineated by the axes.
}

\usage{
Expand All @@ -17,6 +17,8 @@ add_box(...)
}


\seealso{\code{\link{add_legend}}, \code{\link{add_grid}}}

\examples{
v <- vect(system.file("ex/lux.shp", package="terra"))
plot(v)
Expand Down
36 changes: 36 additions & 0 deletions man/grid.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
\name{add_grid}

\alias{add_grid}

\title{add a grid to an existing terra plot}

\description{
Wrapper around \code{\link[graphics]{grid}} that allows adding a grid to a map. This function will place the legend in the locations within the mapped area as delineated by the axes.

Also see \code{\link{graticule}}
}

\usage{
add_grid(nx = NULL, ny = nx, col = "lightgray", lty = "dotted",
lwd = par("lwd"), equilogs = TRUE)
}

\arguments{
\item{nx, ny}{number of cells of the grid in x and y direction. When NULL, as per default, the grid aligns with the tick marks on the corresponding default axis (i.e., tickmarks as computed by axTicks). When NA, no grid lines are drawn in the corresponding direction}
\item{col}{character or (integer) numeric; color of the grid lines}
\item{lty}{character or (integer) numeric; line type of the grid lines}
\item{lwd}{non-negative numeric giving line width of the grid lines}
\item{equilogs}{logical, only used when log coordinates and alignment with the axis tick marks are active. Setting equilogs = FALSE in that case gives non equidistant tick aligned grid lines}
}

\seealso{\code{\link{add_box}}, \code{\link{add_grid}}, \code{\link{graticule}}

\examples{
v <- vect(system.file("ex/lux.shp", package="terra"))
plot(v)
add_grid()
}

\keyword{methods}
\keyword{spatial}

3 changes: 3 additions & 0 deletions man/legend.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ Wrapper around \code{\link{legend}} that allows adding a custom legend to a map
add_legend(x, y, ...)
}


\arguments{
\item{x}{The keyword to be used to position the legend (or the x coordinate)}
\item{y}{The y coordinate to be used to position the legend (is x is also a coordinate)}
\item{...}{arguments passed to \code{\link{legend}}}
}


\seealso{\code{\link{add_box}}, \code{\link{add_grid}}}

\examples{
v <- vect(system.file("ex/lux.shp", package="terra"))
plot(v)
Expand Down

0 comments on commit 2bcdcf4

Please sign in to comment.