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

The inclusion of 'library(rgdal)' above suggests that your plot method needs 
something from 'rgdal'. If that something is a plot method from that package, 
it may not be visible from your function (due to changes introduced in R-4.0.0, 
search NEWS for R-4.0.0 'plot'), you would need to import plot methods from 
rgdal if that is the case (and maybe move rgdal from Suggests to Imports). 

Georgi Boshnakov


-----Original Message-----
From: R-package-devel <r-package-devel-boun...@r-project.org> On Behalf Of 
Duncan Murdoch
Sent: 14 April 2021 13:45
To: Jacob Nabe-Nielsen <j...@bios.au.dk>; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel] Problem with S4 method for plotting a raster file

On 14/04/2021 5:41 a.m., Jacob Nabe-Nielsen wrote:
> 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:
> https://cran.r-project.org/web/packages/DEPONS2R/index.html
> 
> 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:
> 
> library(DEPONS2R)
> 
> data("bathymetry")
> plot(bathymetry)
> data("coastline")
> library(rgdal)
> # 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)
>              on.exit(graphics::par(oldpar))
>              if (missing(main)) {
>                main <- paste(x@landscape, x@type, sep=" - ")
>              }
>              # Define colours specific or 'type'
>              if(missing(col) && x@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@crs=="NA") {
>                crs2 <- sp::CRS(as.character(NA))
>              } else {
>                crs2 <- sp::CRS(x@crs)
>              }
>              rdata <- raster::raster(x=x@data, xmn=x@ext$xleft, 
> xmx=x@ext$xright,
>                                      ymn=x@ext$ybottom, ymx=x@ext$ytop,
>                                      crs=crs2)
>              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@crs)
> rdata <- raster::raster(x = x@data, xmn = x@ext$xleft, xmx = 
> x@ext$xright, ymn = x@ext$ybottom, ymx = x@ext$ytop, crs = crs2)
> raster::plot(rdata)
> 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.
> 
> 

I'm not sure exactly what is going wrong, but if you plot the coastline first 
and the bathymetry second, things do show up.  Maybe that's enough of a hint 
for you:

library(DEPONS2R)
data("coastline")
library(rgdal)
coastline2 <- spTransform(coastline, crs(bathymetry)) plot(coastline2, 
col="lightyellow2")

data("bathymetry")
plot(bathymetry, add=TRUE)

This puts the legend on top of the plotted coastline data, so my guess is that 
the limits of the bathymetry plot aren't being made available to the coastline 
plot when you do things in the original order.

Duncan Murdoch

______________________________________________
R-package-devel@r-project.org mailing list 
https://stat.ethz.ch/mailman/listinfo/r-package-devel

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to