Skip to content

Commit

Permalink
fixes #1422
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Feb 13, 2024
1 parent 6744c10 commit 256b765
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
23 changes: 13 additions & 10 deletions R/inset.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ setMethod("inext", signature(x="SpatVector"),
)


.inset <- function(x, e, loc="", scale=0.2, background="white", perimeter=TRUE, pper, box=NULL, pbox, add=TRUE, xpd=NA, ...) {
.inset <- function(x, e, loc="", scale=0.2, background="white", perimeter=TRUE, pper, box=NULL, pbox, add=TRUE, xpd=NA, offset=0.1, ...) {

usr <- unlist(get.clip()[1:4])
if (missing(e)) {
e <- ext(usr)
r <- diff(e[1:2]) / diff(e[3:4])
e[2] <- e[1] + scale * diff(e[1:2])
e[3] <- e[4] - scale * diff(e[3:4]) * r
xe <- ext(x)
r <- diff(xe[1:2]) / diff(xe[3:4])
n <- scale * diff(e[1:2])
e[2] <- e[1] + n
e[3] <- e[4] - n / r
}

offset <- 0.9
#offset <- max(0.1, min(1, offset))
#offset <- 0.9
offset <- 1 - offset
offset <- max(0.1, min(1, offset))
scale <- offset * min(e / ext(x))

y <- rescale(x, scale)
Expand Down Expand Up @@ -110,14 +113,14 @@ setMethod("inext", signature(x="SpatVector"),


setMethod("inset", signature(x="SpatVector"),
function(x, e, loc="", scale=0.2, background="white", perimeter=TRUE, box=NULL, pper, pbox, add=TRUE, ...) {
.inset(x, e, loc=loc, scale=scale, background=background, perimeter=perimeter, pper=pper, box=box, pbox=pbox, add=add, ...)
function(x, e, loc="", scale=0.2, background="white", perimeter=TRUE, box=NULL, pper, pbox, offset=0.1, add=TRUE, ...) {
.inset(x, e, loc=loc, scale=scale, background=background, perimeter=perimeter, pper=pper, box=box, pbox=pbox, offset=offset, add=add, ...)
}
)


setMethod("inset", signature(x="SpatRaster"),
function(x, e, loc="", scale=0.2, background="white", perimeter=TRUE, box=NULL, pper, pbox, add=TRUE, ...) {
.inset(x, e, loc=loc, scale=scale, background=background, perimeter=perimeter, pper=pper, box=box, pbox=pbox, add=add, ...)
function(x, e, loc="", scale=0.2, background="white", perimeter=TRUE, box=NULL, pper, pbox, offset=0.1, add=TRUE, ...) {
.inset(x, e, loc=loc, scale=scale, background=background, perimeter=perimeter, pper=pper, box=box, pbox=pbox, offset=offset, add=add, ...)
}
)
7 changes: 5 additions & 2 deletions man/inset.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Make an inset map or scale the extent of a SpatVector

\usage{
\S4method{inset}{SpatVector}(x, e, loc="", scale=0.2, background="white",
perimeter=TRUE, box=NULL, pper, pbox, ...)
perimeter=TRUE, box=NULL, pper, pbox, offset=0.1, add=TRUE, ...)

\S4method{inset}{SpatRaster}(x, e, loc="", scale=0.2, background="white",
perimeter=TRUE, box=NULL, pper, pbox, ...)
perimeter=TRUE, box=NULL, pper, pbox, offset=0.1, add=TRUE, ...)

\S4method{inext}{SpatVector}(x, e, y=NULL, gap=0)
}
Expand All @@ -35,6 +35,8 @@ Make an inset map or scale the extent of a SpatVector
\item{box}{SpatExtent or missing, to draw a box on the inset, e.g. to show where the map is located in a larger area}
\item{pper}{list with graphical parameters (arguments) such as \code{col} and \code{lwd} for the perimeter line}
\item{pbox}{list with graphical parameters (arguments) such as \code{col} and \code{lwd} for the box (line)}
\item{offset}{numeric. Value between 0.1 and 1 to indicate the relative distance between what is mapped and the bounding box}
\item{add}{logical. Add the inset to the map?}
\item{...}{additional arguments passed to plot for the drawing of \code{x}}
\item{y}{SpatVector. If not NULL, \code{y} is scaled based with the parameters for \code{x}. This is useful, for example, when \code{x} represent boundaries, and \code{y} points within these boundaries}
\item{gap}{numeric to add space between the SpatVector and the SpatExtent}
Expand All @@ -43,6 +45,7 @@ Make an inset map or scale the extent of a SpatVector

\seealso{\code{\link{sbar}}, \code{\link{rescale}}, \code{\link{shift}}}


\value{scaled and shifted SpatVector or SpatRaster (returned invisibly)}

\examples{
Expand Down

0 comments on commit 256b765

Please sign in to comment.