Skip to content

Commit 2bcdcf4

Browse files
committed
add_grid
1 parent 375d0ca commit 2bcdcf4

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

R/plot_legend.R

+29
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,33 @@ add_box <- function(...) {
506506
}
507507

508508

509+
add_grid <- function(nx = NULL, ny = nx, col = "lightgray", lty = "dotted", lwd = par("lwd"), equilogs = TRUE) {
510+
511+
512+
p <- terra:::get.clip()
513+
514+
## adapted from graphics::grid
515+
g.grid.at <- function (side, n, log, equilogs, axp, usr2) {
516+
if (is.null(n)) {
517+
stopifnot(is.numeric(ax <- axp), length(ax) == 3L)
518+
if (log && equilogs && ax[3L] > 0)
519+
ax[3L] <- 1
520+
axTicks(side, axp = ax, usr = usr2, log = log)
521+
}
522+
else if (!is.na(n) && (n <- as.integer(n)) >= 1L) {
523+
at <- seq.int(usr2[1L], usr2[2L], length.out = n + 1L)
524+
(if (log)
525+
10^at
526+
else at)[-c(1L, n + 1L)]
527+
}
528+
}
529+
530+
atx <- if (is.null(nx) || (!is.na(nx) && nx >= 1))
531+
g.grid.at(1L, nx, log = par("xlog"), equilogs, axp = par("xaxp"), usr2 = p[1:2])
532+
aty <- if (is.null(ny) || (!is.na(ny) && ny >= 1))
533+
g.grid.at(2L, ny, log = par("ylog"), equilogs, axp = par("yaxp"), usr2 = p[3:4])
534+
abline(v = atx, h = aty, col = col, lty = lty, lwd = lwd)
535+
invisible(list(atx = atx, aty = aty))
536+
}
537+
509538

man/box.Rd

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
\title{draw a box}
66

77
\description{
8-
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.
8+
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.
99
}
1010

1111
\usage{
@@ -17,6 +17,8 @@ add_box(...)
1717
}
1818

1919

20+
\seealso{\code{\link{add_legend}}, \code{\link{add_grid}}}
21+
2022
\examples{
2123
v <- vect(system.file("ex/lux.shp", package="terra"))
2224
plot(v)

man/grid.Rd

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
\name{add_grid}
2+
3+
\alias{add_grid}
4+
5+
\title{add a grid to an existing terra plot}
6+
7+
\description{
8+
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.
9+
10+
Also see \code{\link{graticule}}
11+
}
12+
13+
\usage{
14+
add_grid(nx = NULL, ny = nx, col = "lightgray", lty = "dotted",
15+
lwd = par("lwd"), equilogs = TRUE)
16+
}
17+
18+
\arguments{
19+
\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}
20+
\item{col}{character or (integer) numeric; color of the grid lines}
21+
\item{lty}{character or (integer) numeric; line type of the grid lines}
22+
\item{lwd}{non-negative numeric giving line width of the grid lines}
23+
\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}
24+
}
25+
26+
\seealso{\code{\link{add_box}}, \code{\link{add_grid}}, \code{\link{graticule}}
27+
28+
\examples{
29+
v <- vect(system.file("ex/lux.shp", package="terra"))
30+
plot(v)
31+
add_grid()
32+
}
33+
34+
\keyword{methods}
35+
\keyword{spatial}
36+

man/legend.Rd

+3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ Wrapper around \code{\link{legend}} that allows adding a custom legend to a map
1212
add_legend(x, y, ...)
1313
}
1414

15+
1516
\arguments{
1617
\item{x}{The keyword to be used to position the legend (or the x coordinate)}
1718
\item{y}{The y coordinate to be used to position the legend (is x is also a coordinate)}
1819
\item{...}{arguments passed to \code{\link{legend}}}
1920
}
2021

2122

23+
\seealso{\code{\link{add_box}}, \code{\link{add_grid}}}
24+
2225
\examples{
2326
v <- vect(system.file("ex/lux.shp", package="terra"))
2427
plot(v)

0 commit comments

Comments
 (0)