Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set the display bounds of a raster::plot?
    primarykey
    data
    text
    <h1>summary</h1> <p>How to limit display of an R <code>raster::plot</code> to the bounds of a Raster* object? Why I ask:</p> <p>I'm an R beginner who must</p> <ol> <li>convert unprojected (lon-lat) global spatial data from ASCII to <a href="http://en.wikipedia.org/wiki/Netcdf" rel="nofollow noreferrer">netCDF</a> (solved)</li> <li>"regrid" it: i.e., restricted the data to a region, project it, and downscale (decrease the size of the gridcells) it (mostly solved)</li> </ol> <p>Unfortunately, in scientific work, one mostly QAs data transformations such as this by visual inspection. Step 1 above looks good. But though step 2 now looks much better, I really want to plot <strong>only</strong> the bounds (or <code>extents</code>) of the regridded <code>RasterLayer</code>. I have R code driven by bash scripts in a public git repository (<a href="https://github.com/TomRoche/GEIA_to_netCDF" rel="nofollow noreferrer">here</a>) that does the conversion step, and plots the converted output, <a href="https://i.stack.imgur.com/SOmvZ.png" rel="nofollow noreferrer">apparently correctly</a>. However my attempts to plot the output of the regridding with <code>raster::plot</code> are not quite right (see first 3 pages of output <a href="https://github.com/downloads/TomRoche/GEIA_to_netCDF/GEIA_N2O_oceanic_regrid.pdf" rel="nofollow noreferrer">here</a>).</p> <p>How to fix?</p> <h1>details</h1> <h2>background</h2> <p>I need to take some data (a global marine emissions inventory (EI)), combine it with other data (mostly other EIs), and input that to a model. The model wants to consume netCDF, and it wants that netCDF over a spatial domain slightly bigger than the contiguous US (CONUS). The borders of this image <img src="https://github.com/TomRoche/cornbeltN2O/wiki/images/Cmaq_aqmeii_domain.png" alt="AQMEII-NA domain"> are the boundaries of the domain. (Note that part, but only part, of the domain is oceanic.) The model also wants that netCDF projected a certain way (<a href="http://en.wikipedia.org/wiki/Lambert_conformal_conic" rel="nofollow noreferrer">LCC</a> at 12-km resolution). I'm treating this as 2 independent problems:</p> <ol> <li>convert the global EI from its native ASCII format to netCDF</li> <li>"regrid" the netCDF from global/unprojected to the downscaled (finer-resolution), projected subdomain.</li> </ol> <p>I'm attempting to solve these problems with code I have archived at <a href="https://github.com/TomRoche/GEIA_to_NetCDF" rel="nofollow noreferrer">this public git repository</a>. If you clone that git repo, and then configure/run the bash driver <code>GEIA_to_netCDF.sh</code> as described for the "first example" <a href="https://github.com/TomRoche/GEIA_to_netCDF/blob/master/README.md" rel="nofollow noreferrer">in the README</a> (and presuming your R is appropriately configured, notably with packages=<code>raster</code>, <code>ncdf4</code>) it will output netCDF and plot (using <code>fields::image.plot</code>) that output to a <a href="https://github.com/downloads/TomRoche/GEIA_to_netCDF/GEIA_N2O_oceanic.pdf" rel="nofollow noreferrer">PDF</a>, which should look like this: <img src="https://github.com/downloads/TomRoche/GEIA_to_netCDF/GEIA_N2O_oceanic.png" alt="GEIA global marine emissions">. The distribution of the output data appears correct, so problem 1 seems solved.</p> <h2>problem</h2> <p>Solving problem 2 (aka the "second example" <a href="https://github.com/TomRoche/GEIA_to_netCDF/blob/master/README.md" rel="nofollow noreferrer">in the README</a>) seems to require R package=<code>raster</code>. My bash driver <a href="https://github.com/TomRoche/GEIA_to_netCDF/blob/master/regrid_GEIA_netCDF.sh" rel="nofollow noreferrer"><code>regrid_GEIA_netCDF.sh</code></a> calls <a href="https://github.com/TomRoche/GEIA_to_netCDF/blob/master/regrid.global.to.AQMEII.r" rel="nofollow noreferrer"><code>regrid.global.to.AQMEII.r</code></a> (both <a href="https://github.com/TomRoche/GEIA_to_NetCDF" rel="nofollow noreferrer">in repository</a>), which runs without error, and displays a PDF of its output. <a href="https://github.com/downloads/TomRoche/GEIA_to_netCDF/GEIA_N2O_oceanic_regrid.pdf" rel="nofollow noreferrer"><code>GEIA_N2O_oceanic_regrid.pdf</code></a> comprises 4 pages, corresponding to 4 blocks of code at the end of <code>regrid.global.to.AQMEII.r</code>. Only the first 3 pages are relevant here (the 4th page tries to use <code>fields::image.plot</code> and has bigger problems).</p> <p>Page=1/4 results from a simple <code>raster::plot</code> of the regridded output, plus a projected version of a CONUS map from <code>wrld_simpl</code>:</p> <pre><code>plot(out.raster) plot(map.us.proj, add=TRUE) </code></pre> <p>Unfortunately the output appears global, or nearly so: <img src="https://i.stack.imgur.com/XLIZk.png" alt="raster::plot bounds problem"> But the desired domain, to which the output was regridded, is much smaller: <img src="https://github.com/TomRoche/cornbeltN2O/wiki/images/Cmaq_aqmeii_domain.png" alt="AQMEII-NA domain">. So I have 3 questions (1 main question, 2 followups):</p> <ol> <li>Does the image displayed by <code>raster::plot</code> by default display <em>only</em> the extents of the <code>RasterLayer</code> given in its arguments?</li> <li>If so, what is wrong with my regrid? (more below)</li> <li>If not (i.e., if <code>raster::plot</code> by default displays <em>more</em> than the extents of its <code>RasterLayer</code>), how to make <code>raster::plot</code> display only the extents of that <code>RasterLayer</code>?</li> </ol> <p>The code in <code>regrid.global.to.AQMEII.r</code> (<a href="https://github.com/TomRoche/GEIA_to_netCDF/blob/master/regrid.global.to.AQMEII.r" rel="nofollow noreferrer">here</a>) that does the regridding seems to get the correct output:</p> <pre><code>out.crs &lt;- '+proj=lcc +lat_1=33 +lat_2=45 +lat_0=40 +lon_0=-97 +x_0=-2556000 +y_0=-1728000' </code></pre> <p>Note that CRS appears to match the definition of the output domain given <a href="https://github.com/TomRoche/cornbeltN2O/wiki/AQMEII-North-American-domain#wiki-EPA" rel="nofollow noreferrer">here</a>. (Once at the linked page, scroll past the map.)</p> <pre><code>out.raster &lt;- projectRaster( from=in.raster, to=template.raster, method='bilinear', crs=out.crs, overwrite=TRUE, progress='window', format='CDF', # args from writeRaster NAflag=-999.0, # match emi_n2o:missing_value,_FillValue (TODO: copy) varname=data.var.name, varunit='ton N2O-N/yr', longname='N2O emissions', xname='COL', yname='ROW', filename=out.fp) out.raster # made with CRS #&gt; class : RasterLayer #&gt; dimensions : 299, 459, 137241 (nrow, ncol, ncell) #&gt; resolution : 53369.55, 56883.69 (x, y) #&gt; extent : -14802449, 9694173, -6258782, 10749443 (xmin, xmax, ymin, ymax) # ??? why still proj=longlat ??? #&gt; coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 #&gt; data source : /home/rtd/code/R/GEIA_to_netCDF/GEIA_N2O_oceanic_regrid.nc #&gt; names : N2O.emissions #&gt; zvar : emi_n2o </code></pre> <p>But, as noted, the regrid output (<code>out.raster</code>) shows itself to be lon-lat in its CRS: I'm not sure why that is, or if it implies <code>out.raster</code> is global in extents.</p> <p>I also tried to restrict the plot itself in two ways:</p> <p>Firstly, I tried adding an <code>extents</code> to the plot, i.e.,</p> <pre><code>plot(out.raster, ext=template.raster) plot(map.us.proj, add=TRUE) </code></pre> <p>which generates page=2/4 of <a href="https://github.com/downloads/TomRoche/GEIA_to_netCDF/GEIA_N2O_oceanic_regrid.pdf" rel="nofollow noreferrer">the PDF</a>. Unfortunately, that does not change the output at all: AFAICS, it is completely identical to page=1/4 (above).</p> <p>Secondly, I tried using <code>raster::crop</code> to bound the regridded netCDF itself, before plotting that, using the same <code>RasterLayer</code> object (<code>template.raster</code>) that I used to bound the regrid:</p> <pre><code>template.raster.extent &lt;- extent(template.raster) out.raster.crop &lt;- # overwrite the uncropped file crop(out.raster, template.raster.extent, filename=out.fp, overwrite=TRUE) ... plot(out.raster.crop) plot(map.us.proj, add=TRUE) </code></pre> <p>which generates page=3/4 of <a href="https://github.com/downloads/TomRoche/GEIA_to_netCDF/GEIA_N2O_oceanic_regrid.pdf" rel="nofollow noreferrer">the PDF</a>. Unfortunately, that it is also apparently completely identical to page=1/4 (above).</p> <p>(In case you're wondering, page 4 of the PDF was generated using <code>fields::image.plot</code>, which has a different problem, described <a href="https://stackoverflow.com/questions/13022919/r-problems-visualizing-a-raster-with-fieldsimage-plot">here</a>, unless StackOverflow whacks that link.)</p> <p>Your assistance to this R newbie is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload