[R-pkg-devel] Problem with S4 method for plotting a raster file

Jacob Nabe-Nielsen jnn @end|ng |rom b|o@@@u@dk
Wed Apr 14 11:41:28 CEST 2021

Dear all,

I have just discovered an error in a package that I have already uploaded to
CRAN, and cannot figure out how to fix it. All help will be greatly appreciated.
The package is called DEPONS2R, and is available from:

The problem is related to one of the included examples, where I plot the coastline
on top of a raster file. In the example, where the plotting is embedded in
an S4 method called plot.DeponsRaster, the coastline does not show. But if
I run the functions that the method is composed of, everything works fine (see
below). So I am wondering if the graphics devise is not passed on correctly
within the package, or what actually happens.

The example that does not run correctly is available from ?plot.DeponsRaster:


# Change projection of coastline to match that of bathymetry data
coastline2 <- spTransform(coastline, crs(bathymetry))
plot(coastline2, add=TRUE, col="lightyellow2")

Here the coastline is not plotted. The method is defined as:

setMethod("plot", signature("DeponsRaster", "ANY"),
          function(x, y, maxpixels=500000, col, alpha=NULL, colNA=NA, add=FALSE,
                   ext=NULL, useRaster=TRUE, interpolate=FALSE, addfun=NULL,
                   nc, nr, maxnl=16, main, npretty=0, axes=TRUE,
                   legend=TRUE, trackToPlot=1, ...)  {
            oldpar <- graphics::par(no.readonly = TRUE)
            if (missing(main)) {
              main <- paste(x using landscape, x using type, sep=" - ")
            # Define colours specific or 'type'
            if(missing(col) && x using type=="bathymetry") {
              tmp.col <- grDevices::rainbow(1000)[501:800]
              col <- c(tmp.col[1:100], rep(tmp.col[101:250], each=5))
            # Use {raster}-package for plotting
            if(x using crs=="NA") {
              crs2 <- sp::CRS(as.character(NA))
            } else {
              crs2 <- sp::CRS(x using crs)
            rdata <- raster::raster(x=x using data, xmn=x using ext$xleft, xmx=x using ext$xright,
                                    ymn=x using ext$ybottom, ymx=x using ext$ytop,
            raster::plot(rdata, col=col, main=main,
                         alpha=alpha, add=add, ext=ext, axes=axes, legend=legend)

But when I use the functions that are at the core of this method it is
not a problem adding the coastline correctly:

x <- bathymetry
crs2 <- raster::crs(x using crs)
rdata <- raster::raster(x = x using data, xmn = x using ext$xleft,
xmx = x using ext$xright, ymn = x using ext$ybottom, ymx = x using ext$ytop,
crs = crs2)
plot(coastline2, add=TRUE, col="lightyellow2")

If would be fantastic if someone can figure out how I should modify the method
to plot the coastline correctly on top of the map.

Best regards,

	[[alternative HTML version deleted]]

More information about the R-package-devel mailing list