Skip to content

Commit

Permalink
fixes #1395
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Jan 9, 2024
1 parent e8b66d9 commit 0dfcfe2
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions R/plot_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,10 @@ setMethod("plot", signature(x="SpatRaster", y="numeric"),
old.mar <- graphics::par()$mar
on.exit(graphics::par(mar=old.mar))


y <- round(y)
hasRGB <- FALSE

if (has.RGB(x) && ((is.null(type) && (y[1] < 0)))) {
type <- "rgb"
legend <- FALSE
Expand All @@ -775,9 +777,11 @@ setMethod("plot", signature(x="SpatRaster", y="numeric"),
hasRGB <- TRUE
y <- RGB(x)
}
stopifnot((min(y) > 0) & (max(y) <= nlyr(x)))
nlx <- nlyr(x)
stopifnot((min(y) > 0) & (max(y) <= nlx))

if ((!hasRGB) && (length(y) > 1)) {

x <- x[[y]]
if (inherits(alpha, "SpatRaster")) {
if (nlyr(alpha) > 1) {
Expand Down Expand Up @@ -874,18 +878,24 @@ setMethod("plot", signature(x="SpatRaster", y="numeric"),


setMethod("plot", signature(x="SpatRaster", y="missing"),
function(x, y, main, mar=NULL, nc, nr, maxnl=16, maxcell=500000, ...) {
function(x, y, main, mar=NULL, nc, nr, maxnl=16, maxcell=500000, add=FALSE, ...) {

if (has.RGB(x)) {
if (missing(main)) main = ""
p <- plot(x, -1, main=main, mar=mar, maxcell=maxcell, ...)
p <- plot(x, -1, main=main, mar=mar, maxcell=maxcell, add=add, ...)
return(invisible(p))
}

nl <- max(1, min(nlyr(x), maxnl))

if (add && (nl > 1)) {
nl <- 1
warn("plot", "adding the first layer of x")
}

if (nl==1) {
if (missing(main)) main = ""
out <- plot(x, 1, maxcell=maxcell, main=main[1], mar=mar, ...)
out <- plot(x, 1, maxcell=maxcell, main=main[1], mar=mar, add=add, ...)
return(invisible(out))
}

Expand All @@ -909,7 +919,7 @@ setMethod("plot", signature(x="SpatRaster", y="missing"),
main <- rep_len(main, nl)
}
for (i in 1:nl) {
plot(x, i, main=main[i], mar=mar, maxcell=maxcell, ...)
plot(x, i, main=main[i], mar=mar, maxcell=maxcell, add=add, ...)
}
}
)
Expand Down

0 comments on commit 0dfcfe2

Please sign in to comment.