Skip to content

Commit

Permalink
r
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed May 17, 2024
1 parent 8803805 commit 6fdaaa3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
17 changes: 6 additions & 11 deletions R/plot_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -506,31 +506,26 @@ add_box <- function(...) {
}


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

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

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

## adapted from graphics::grid
g.grid.at <- function (side, n, log, equilogs, axp, usr2) {
g.grid.at <- function (side, n, 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)
axTicks(side, axp=ax, usr=usr2, log=FALSE)
}
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)]
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])
g.grid.at(1L, nx, 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])
g.grid.at(2L, ny, axp = par("yaxp"), usr2 = p[3:4])
abline(v = atx, h = aty, col = col, lty = lty, lwd = lwd)
invisible(list(atx = atx, aty = aty))
}
Expand Down
4 changes: 1 addition & 3 deletions man/grid.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ Also see \code{\link{graticule}}
}

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

\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{graticule}}, \code{\link{add_box}}, \code{\link{add_grid}}}
Expand Down
4 changes: 2 additions & 2 deletions man/init.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
\title{Initialize a SpatRaster with values}

\description{
Create a SpatRaster with values reflecting a cell property: 'x', 'y', 'col', 'row', 'cell' or 'chess'. Alternatively, a function can be used. In that case, cell values are initialized without reference to pre-existing values. E.g., initialize with a random number (\code{fun=\link{runif}}). While there are more direct ways of achieving this for small objects (see examples) for which a vector with all values can be created in memory, the \code{init} function will also work for SpatRasters with many cells.
Create a SpatRaster with values reflecting a cell property: "x", "y", "col", "row", "cell" or "chess". Alternatively, a function can be used. In that case, cell values are initialized without reference to pre-existing values. E.g., initialize with a random number (\code{fun=\link{runif}}). While there are more direct ways of achieving this for small objects (see examples) for which a vector with all values can be created in memory, the \code{init} function will also work for SpatRasters with many cells.
}

\usage{
Expand All @@ -15,7 +15,7 @@ Create a SpatRaster with values reflecting a cell property: 'x', 'y', 'col', 'ro

\arguments{
\item{x}{SpatRaster}
\item{fun}{function to be applied. This must be a either single number, multiple numbers, a function, or one of a set of known character values. A function must take the number of cells as a single argument to return a vector of values with a length equal to the number of cells, such as \code{fun=runif}. Allowed character values are 'x', 'y', 'row', 'col', 'cell', and 'chess' to get the x or y coordinate, row, col or cell number or a chessboard pattern (alternating 0 and 1 values)}
\item{fun}{function to be applied. This must be a either single number, multiple numbers, a function, or one of a set of known character values. A function must take the number of cells as a single argument to return a vector of values with a length equal to the number of cells, such as \code{fun=runif}. Allowed character values are "x", "y", "row", "col", "cell", and "chess" to get the x or y coordinate, row, col or cell number or a chessboard pattern (alternating 0 and 1 values)}
\item{filename}{character. Output filename}
\item{...}{additional arguments for writing files as in \code{\link{writeRaster}}}
}
Expand Down

0 comments on commit 6fdaaa3

Please sign in to comment.