1
votes

Consider the following:

temp <- array(sample(1:100,12), dim=c(365,12))
Depth <- as.vector(seq(1,12,by=1))
time <- seq(from=as.Date("2011-01-01"), to=as.Date("2011-12-31"), by=1)

Data <- data.frame(Time = time, Temp = as.matrix(temp))
colnames(Data) <- c("Datetime", paste(Depth,"m",sep = "")) 

Don't take into account the 3 lines used in generating the data.frame as they are merely used to generate a similar example I am currently working with. The data.frame i.e. 'Data' consists of a Date-time vector and corresponding temperature measurements recorded at different depths where the depth of each measurement is given as the column names.

From this I would like to generate a filled contour plot of the temperature profile. So, I want to place the DateTime vector as the x values, the col headings as the y values and the temp as the z values. How would I go about achieving this?

I have only just converted from matlab to R so apologies for what is probably a very simple problem.

1

1 Answers

6
votes

Lattice makes this pretty easy is you have the data in the right structure (long and thin not wide and short - you have it in the latter at the moment)

First get the data into the required format using some simple data manipulations

## make the colnames numeric-ish
names(Data)[-1] <- sub("m", "", names(Data)[-1])
## stack the data
Data2 <- data.frame(Time = rep(Data$Datetime, times = ncol(Data)-1),
                    stack(Data[, -1]))
names(Data2)[-1] <- c("Temperature", "Depth")
## make Depth numeric
Data2 <- transform(Data2, Depth = as.numeric(as.character(Depth)))

This gives us:

> head(Data2)
        Time Temperature Depth
1 2011-01-01          84     1
2 2011-01-02          19     1
3 2011-01-03          25     1
4 2011-01-04          21     1
5 2011-01-05           1     1
6 2011-01-06          26     1

Load lattice and plot the data using the contourplot() function:

require(lattice)
contourplot(Temperature ~ Time * Depth, data = Data2)

For this example data set, it might help to use

contourplot(Temperature ~ Time * Depth, data = Data2, labels = FALSE,
            region = TRUE)

because the contours are essentially formed around tiny chunks of data.

See the ?contourplot page for more on the various options.