[R-sig-Geo] Unit of parameters in the expandBB argument

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Wed Feb 28 10:57:52 CET 2024


On Wed, 28 Feb 2024, Roger Bivand wrote:

> On Wed, 28 Feb 2024, Edzer Pebesma wrote:
>
>> 
>>
>>  On 28/02/2024 06:04, Xiang Ye via R-sig-Geo wrote:
>>>   Dear community,
>>>
>>>   I recently revisited the expandBB argument in the plot() function for sf
>>>   objects. It conveys a numeric vector of length 4 to expand the default
>>>   canvas (in the order of bottom, left, top, right) when drawing an sf
>>>   object. A quick refresh is here:
>>>
>>>   https://r.geocompx.org/spatial-class#:~:text=to%20geographic%20data.-,expandBB,-%2C%20for%20example%2C%20can
>>>   [https://r.geocompx.org/images/cover.png]<https://r.geocompx.org/spatial-class#:~:text=to%20geographic%20data.-,expandBB,-%2C%20for%20example%2C%20can>
>>>   Chapter 2 Geographic data in R | Geocomputation with
>>>   R<https://r.geocompx.org/spatial-class#:~:text=to%20geographic%20data.-,expandBB,-%2C%20for%20example%2C%20can>
>>>   Prerequisites This is the first practical chapter of the book, and
>>>   therefore it comes with some software requirements. You need access to a
>>>   computer with a recent version of R installed (R 4.3.2...
>>>   r.geocompx.org
>>>
>>>   I am curious about the unit for the four values of the expandBB
>>>   argument.
>>>   In the help document, it only says the values are "fraction values to
>>>   expand the bounding box with":
>>>
>>>   https://r-spatial.github.io/sf/reference/plot.html#:~:text=fractional%20values%20to%20expand%20the%20bounding%20box%20with
>>>   [https://r-spatial.github.io/sf/logo.png]<https://r-spatial.github.io/sf/reference/plot.html#:~:text=fractional%20values%20to%20expand%20the%20bounding%20box%20with>
>>>   plot sf object ��
>>>   plot<https://r-spatial.github.io/sf/reference/plot.html#:~:text=fractional%20values%20to%20expand%20the%20bounding%20box%20with>
>>>   plot one or more attributes of an sf object on a map Plot sf object
>>>   r-spatial.github.io
>>>
>>>   However, it did not explicitly mention the unit adopted for the values.
>>>
>>>   My guess:
>>>   For the 1st and 3rd values in the argument, the unit is the height of
>>>   the
>>>   bounding box of the sf object; therefore a value of 0.2 expands
>>>   (downward
>>>   and upward, respectively) the canvas by 20% of the height of the
>>>   bounding
>>>   box.
>>>   For the 2nd and 4th values in the argument, the unit is the width of the
>>>   bounding box of the sf object; therefore a value of 0.2 expands
>>>   (leftward
>>>   and rightward, respectively) the canvas by 20% of the width of the
>>>   bounding box.
>>>
>>>   Is my understanding correct?
>>
>>  Yes. It takes 4 values for each of the sides (bottom, left, top, right).
>>  You can use xlim and ylim to set the plot region in spatial coordinates.
>> 
>
> The code is at: 
> https://github.com/r-spatial/sf/blob/bd1940f68dd7607b7e9098ccc32c99e273c30a60/R/plot.R#L598-L611
>
> based on the equivalent in sp: 
> https://github.com/edzer/sp/blob/e60dedf3ff3d06d9ebd9d38818b072e4475758cf/R/Spatial-methods.R#L211-L226
>
> and added in a commit 8 years ago: 
> https://github.com/edzer/sp/commit/d65045951bfebf28fbd5d0ad56bfff3cb48d57a8.
>
> So the units are implicitly those of sp::bbox() and sf::st_bbox().

That is, the *output* units are map units, expandBB are proportions, as 
in:

expBB = function(lim, expand) c(lim[1] - expand[1] * diff(lim), lim[2] + 
expand[2] * diff(lim))

called for example as: xlim <- expBB(bbox[1,], expandBB[c(2,4)]).

Roger

>
> Roger
>
>
>
>>>
>>>   Thank you in advance!
>>>
>>>   Ҷ�� YE, Xiang
>>>   THINKING
>>>   SPATIALLY<http://www.linkedin.com/in/spatialyexiang>.
>>>   Ph.D. in Spatial Statistics
>>>
>>>    [[alternative HTML version deleted]]
>>> 
>>>
>>>   _______________________________________________
>>>   R-sig-Geo mailing list
>>>   R-sig-Geo using r-project.org
>>>   https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> 
>> 
>
>

-- 
Roger Bivand
Emeritus Professor
Department of Economics, Norwegian School of Economics,
Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway.
e-mail: Roger.Bivand using nhh.no


More information about the R-sig-Geo mailing list