[R-sig-Geo] spatstat inverse-distance-weighted plot - change the axis range

Rolf Turner r.turner at auckland.ac.nz
Sun Aug 17 03:13:10 CEST 2014


On 17/08/14 07:08, Kristin Graves wrote:
> Hello --
>
> I am pretty new to R and to spatstat, so apologize for any ignorance.
>
> I have a dataset with three variables:  x, y, and avgidw where x and y are
> UTM coordinates and avgidw is a numeric mask of type "double".  My x range
> is (585000,610000) and my y range is (4473000,4523000).
>
> My end goal is to create an IDW plot that limits the plot to the (x,y)
> range.  I am able to obtain the plot, but the y-axis origin is zero, not
> 4473000 as desired.  My code is below.
>
>> library(spatstat)
>
> spatstat 1.38-0       (nickname: ‘Wicked Plot’)
> For an introduction to spatstat, type ‘beginner’
> Warning message:
> package ‘spatstat’ was built under R version 3.1.1
>> idw_df <- read.table(idw_input, header=TRUE)
>> attach(idw_df)
>> idw_pp <- ppp(x, y, c(585000,610000), c(4473000,4523000), marks=avgidw)
>> idw_win <- owin(c(585000,610000), c(4473000,4523000))
>> idw_chop <- idw_pp[idw_win]
>> x=585000:610000
>> y=4473000:4523000
>> idw_mask <- as.mask(idw_win,xy=list(x=x, y=y))
>> idw_pix2 <- idw(idw_chop, power=2, at="pixels", idw_mask)
> Error in ensure2vector(eps) : eps is not numeric
>>
>
>
> On the other hand, if I run this code I do obtain a plot (albeit with the
> wrong axes):
>> idw_pix2 <- idw(idw_chop, power=2, at="pixels")
>> plot(idw_pix2)
>
>
> Thanks in advance for any help you can provide!

Basically the problem arises from supply "idw_mask" as the final 
argument to idw().  The help for idw() (clearly!) states:

> ... Arguments passed to as.mask to control the pixel resolution of the result.


That is, you don't pass a mask as a "..." argument, you pass arguments 
that are needed by as.mask(), e.g. "dimyx" or "eps".  RTFM.

A few other comments:

(1) What do you think you are accomplishing by:

>>> idw_win <- owin(c(585000,610000), c(4473000,4523000))
>>> idw_chop <- idw_pp[idw_win]

???

You are not chopping anything.  The window "idw_win" already is the 
window of idw_pp, as you specified in the call to ppp() which created
idw_pp.  Thus idw_chop and idw_pp will be identical.

(2) Your pixellation for idw_mask is 25000 x 50000 which is 
***ridiculously*** fine.  I cannot imagine why you would want such a
fine pixellation.

(3) What do you mean by "with the wrong axes" in your last comment?  The 
*axes* won't change with the fineness of the pixellation. Your assertion

> but the y-axis origin is zero, not 4473000

is simply nonsense.  And incorrect too!  Where on earth did you get the 
idea the y-axis origin of your image was 0?

(4) If you want a finer pixellation, then pass dimyx or eps to idw() as 
the "..." argument.  E.g.

     idw_pix2 <- idw(idw_chop, power=2, at="pixels", dimyx=c(500,250))

or
     idw_pix2 <- idw(idw_chop, power=2, at="pixels", eps=100)

or (probably better)

     idw_pix2 <- idw(idw_chop, power=2, at="pixels", dimyx=c(512,256))  .

(5) Using huge numbers in your coordinates (order of hundreds of 
thousands) as you do is fraught with peril.  There can be numerical 
issues, the output is hard to read, and it's easy to get zeroes out of 
sync.  I suggest that you rescale your point pattern by a factor of 
1000.  Either:

     idw_df$x <- idw_df$x/1000
     idw_df$y <- idw_df$y/1000

before you form idw_pp, or

     idw_pp <- rescale(idw_pp,1000)

after you have formed idw_pp from the original idw_df.

(6) It is inadvisable to use attach(); all sorts of confusion can 
result.  Use with() instead:

     idw_pp <- with(idw_df,ppp(...))

(7) Saying `avgidw is a numeric mask of type "double"' is just plain 
bafflegab.  It (i.e. avgidw) is a numeric variate.  There is no "mask" 
involved and all real variates are stored to double precision in R.

cheers,

Rolf Turner


-- 
Rolf Turner
Technical Editor ANZJS



More information about the R-sig-Geo mailing list