3
$\begingroup$

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.

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

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
plot(X_ppp, add = T)

enter image description here

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.

$\endgroup$
  • 3
    $\begingroup$ This looks like the right graphical idea, but it seems unlikely that a smooth or density of chemical concentrations would be meaningful. It sounds more like some form of interpolation of the data is desired. Methods could range from regression (to fit a surface) through various forms of kriging to model-free approaches with splines, natural neighbors, etc. $\endgroup$ – whuber Oct 4 '12 at 14:15
  • 1
    $\begingroup$ Ahh I was confused about what the spatial smooth function was doing. I will update with an example of interpolation when I get a chance. $\endgroup$ – Andy W Oct 4 '12 at 14:39
  • $\begingroup$ This whole package is spectacular. Thanks for the tips! $\endgroup$ – rnorberg Oct 4 '12 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.