# How to display concentration data in space?

I have a data frame of chemical concentrations that are measurements taken from 12 locations all in the same vicinity. I have manually assigned x and y coordinates to each location (on a 0 to 20 scale) and each observation has a concentration value. a small sample is given below:

dat<-data.frame(x = rep(c(2, 5.5, 8, 14), each=2),
y = rep(c(3, 9, 14, 17), 2),
z = log(rnorm(8, 10, .75))


I want to display the concentration over space in an image by sort of averaging over the 2 dimensions so as to fill the whole plot space. I think what I want to do here is a contour plot, but I can't figure out how to make this work, so maybe this isn't the right kind of data for such a plot? I'm trying to implement this in R, so any R advice on how to do this would be awesome.

-
This really is a programming rather than a statistical question and I've seen an answer on StackOverflow. –  rolando2 Oct 4 '12 at 11:59
Could you please tell us what these data mean and what the plot is intended to represent? –  whuber Oct 4 '12 at 14:16
could you add tag "interpolation" please –  denis Nov 7 '12 at 11:48

It sounds to me like an interpolated raster "heat map" may be what you are looking for. I typically use the spatstat library for analysis of point patterns, so here is an example using that library. Here I use inverse distance weighting, but whether that is appropriate or not I would need more domain knowledge.

set.seed(10)
library(spatstat)

dat<-data.frame(x = rep(c(2, 5.5, 8, 14), each=2),
y = rep(c(3, 9, 14, 17), 2),
z = log(rnorm(8, 10, .75)))
mywin <- owin(c(0,16),c(1,19))

X_ppp <- ppp(dat$x,dat$y, window = mywin, marks = dat\$z)

X_idw <- idw(X_ppp) #inverse distance weighting
plot(X_idw)

#making circles of more varying size
m <- marks(X_ppp)
marks(X_ppp) <- (m - min(m))/(max(m) - min(m))*6+1

If you still want, you can also make a contour plot from the density object the above image using the command contour(X_idw) (both types of plots require interpolating the point pattern). Another option would be a plot displaying the circles sized according to the value of z, or a 3d plot (see pesp.im(X_idw)). Here are some nice examples of contour plots.
Probably someone with better knowledge of your substantive area can weigh in on how to better interpolate the chemical values. The spatstat library is more aimed for point process modelling, and so other libraries like gstat may be more appropriate. But, the logic behind the plot is still the same.