[R] AKIMA: z values at a set coordinate

Paul Hiemstra p.hiemstra at geo.uu.nl
Sat Nov 21 11:57:40 CET 2009


Hi Anonymous, (Maybe next time include your name)

There are data objects in R that are designed for spatial data, look at 
the sp package. Casting them into this format gives you an enormous 
increase in flexibility with analyzing spatial data. See the example 
below using your example:

library("akima")
library(sp)
data(akima)
akima.li <- interp(akima$x, akima$y, akima$z)
# Change to sp object
# Note that we swap the x and y column [1]
y = rep(akima.li$x, each = length(akima.li$y))
x = rep(akima.li$y, length(akima.li$x))
z = as.numeric(akima.li$z)
akima.sp = data.frame(x, y, z)
# sp-function, which columns are the coordinates
coordinates(akima.sp) = ~x+y
# Tell sp that it is a grid
gridded(akima.sp) = TRUE

# Plot and compare
image (akima.li)
spplot(akima.sp)

# Use overlay from sp to get the value
# at a specific location
pt = data.frame(x = 11.25, y = 6.5)
coordinates(pt) = ~x+y
val = akima.sp at data[overlay(akima.sp, pt),]
val
# [1] 19.14752

Learning to use sp-objects is really worthwhile. See the spatial Task 
view for more information, or check out the R-wiki [2]. With these kind 
of geographic questions you might want to use the r-sig-geo mailing list 
instead of R-help.

cheers,
Paul

[1] We do this because (from details section of Image):

Notice that ‘image’ interprets the ‘z’ matrix as a table of
‘f(x[i], y[j])’ values, so that the x axis corresponds to row
number and the y axis to column number, with column 1 at the
bottom, i.e. a 90 degree counter-clockwise rotation of the
conventional printed layout of a matrix.

[2] http://wiki.r-project.org/rwiki/doku.php?id=tips:spatial-data

Rhelp wanted wrote:
> Dear all.
>
> I am using the akima function to produce 3d contour plots using interp based
> on irregular data.
>
> using the eg in the akima manual
>
> library("akima")
> data(akima)
> plot(y ~ x, data = akima, main = "akima example data")
> with(akima, text(x, y, formatC(z,dig=2), adj = -0.1))
> ## linear interpolation
> akima.li <- interp(akima$x, akima$y, akima$z)
> image (akima.li, add=TRUE)
> contour(akima.li, add=TRUE)
> points (akima, pch = 3)
>
> so with this in mind is there a way of obtaining the interpolated value at a
> particular coordinate eg at (11.25,6.5) I can see that it as an orange and
> should I look at the contour lines I can see what value it produces. However
> Is there a way of saying function[11.25,6.5] which provides a value for that
> coordinate.
>
> Hope someone can help
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   


-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul




More information about the R-help mailing list