2

I have a large data set which I would like to make a 3D surface from. I would like the x-axis to be the date, the y-axis to be the time (24h) and the z-axis (height) to be a value I have ($). I am a beginner with R, so the simpler the better!

http://www.quantmod.com/examples/chartSeries3d/ has a nice example, but the code is way to complicated for my skill level!

Any help would be much appreciated - anything I have researched so far needs to have the data sorted, which is not suitable I think.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Mark McMullan
  • 21
  • 1
  • 2
  • 3
    I would suggest trying to figure out how that figure was made and then asking more specific questions based on where you get stuck. – Stedy Jun 21 '11 at 23:04
  • Check out the `lattice` package, in particular see the examples for `wireframe` (you can do this by loading the `lattice` package then running `example(wireframe)`). – nullglob Jun 22 '11 at 06:27
  • If you post some sample data it will be much easier to help you. See http://stackoverflow.com/q/5963269/602276 for tips on asking a great question – Andrie Jun 22 '11 at 08:22

1 Answers1

7

Several options present themselves, persp() and wireframe(), the latter in package lattice.

First some dummy data:

set.seed(3)
dat <- data.frame(Dates = rep(seq(Sys.Date(), Sys.Date() + 9, by = 1), 
                              each = 24),
                  Times = rep(0:23, times = 10),
                  Value = rep(c(0:12,11:1), times = 10) + rnorm(240))

persp() needs the data as the x and y grid locations and a matrix z of observations.

new.dates <- with(dat, sort(unique(Dates)))
new.times <- with(dat, sort(unique(Times)))
new.values <- with(dat, matrix(Value, nrow = 10, ncol = 24, byrow = TRUE))

and can be plotted using:

persp(new.dates, new.times, new.values, ticktype = "detailed", r = 10, 
      theta = 35, scale = FALSE)

The facets can be coloured using the col argument. You could do a lot worse than study the code for chartSeries3d0() at the page you linked to. Most of the code is just drawing proper axes as neither persp() nor wireframe() handle Date objects easily.

As for wireframe(), we

require(lattice)
wireframe(Value ~ as.numeric(Dates) + Times, data = dat, drape = TRUE)

You'll need to do a bit or work to sort out the axis labelling as wireframe() doesn't work with objects of class "Date" at the moment (hence the cast as numeric).

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • @Mark McMullan If you are satisfied with an Asnwer please marker it accepted by checking the big tick mark to the left of the answer. No worries if this isn't a final complete answer, you don't need to accept such answers. – Gavin Simpson Jun 23 '11 at 20:47