3

I want to produce a 3D surface in R where y is flux, x is Age and z is precipitation. I have a range of age between 0 and 152 and a range of precipitation 0 and 2600.

I also have two functions where flux is function of Age or Precipitation:

Flux= 0.387217066*(Age^0.328086337)*(exp(-0.004177033*Age)

and

Flux= 1.117997*(1-exp(-exp(-5.426564)*(Preci-(-220.745499))

What I want to achieve is something a bit like this:

enter image description here

I have tried to do it with the package plot3D in R without success (see below)

Age<- as.matrix(seq(0:152)) 
Preci<-as.matrix(seq(from=10, to=2600, by=17))
Flux= as.matrix(0.387217066*(Age^0.328086337)*(exp(-0.004177033*Age)) - 1.117997*(1-exp(-exp(-5.426564)*(Preci-(-220.745499)))))
surf3D(Age, Preci, Flux, colvar = Flux, colkey = FALSE, facets = FALSE)

I got this error message

Error in if (is.na(var)) ispresent <- FALSE else if (length(var) == 1) if (is.logical(var)) if (!var) ispresent <- FALSE : 
  argument is of length zero
Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
SimonB
  • 670
  • 1
  • 10
  • 25
  • 1
    `ggplot2` doesn't do 3D plots, but you can use `rgl` or `lattice::wireframe`. `emdbook::curve3d()` might be a useful wrapper. – Ben Bolker Oct 12 '15 at 17:33

1 Answers1

3

Here's a start, using emdbook::curve3d() as a wrapper for lattice::wireframe (see the sys3d argument to ?curve3d). It's not obvious to me why it would make sense to combine your functions of flux as a function of age vs precip by subtracting one from the other, but as that's what you did above ...

## for readability, put numeric values in a separate vector
params <- c(a0=0.387217066,a1=0.328086337,a2=0.004177033,
            p0=1.117997,p1=5.426564,p2=-220.745499)

library("emdbook")

curve3d(with(as.list(params),
          a0*(Age^a1)*exp(-a2*Age)-
            p0*(1-exp(-exp(-p1)*(Preci-p2)))),
        varnames=c("Age","Preci"),
        xlim=c(0,200),ylim=c(10,2600),
        sys3d="wireframe",
        col="gray",
        zlab="Flux")

enter image description here

curve3d also returns a list with components $x, $y, $z that you can use as input to other 3D graphing frameworks.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • @ Ben Bolker. I agree that it does not make sense to o combine my functions of flux as a function of age vs precip by subtracting one from the other. I am more looking at the interaction. Do you know how to do it? – SimonB Oct 13 '15 at 08:17
  • that's really a scientific/engineering question, not a statistical one. Depending on the situation adding or multiplying *might* make sense, or you might need more information to determine the right combination. – Ben Bolker Oct 14 '15 at 02:27