Search code examples
rlattice

Heatmap like plot with Lattice


I can not figure out how the lattice levelplot works. I have played with this now for some time, but could not find reasonable solution.

Sample data:

Data <- data.frame(x=seq(0,20,1),y=runif(21,0,1))
Data.mat <- data.matrix(Data)

Plot with levelplot:

rgb.palette <- colorRampPalette(c("darkgreen","yellow", "red"), space = "rgb")

levelplot(Data.mat, main="", xlab="Time", ylab="", col.regions=rgb.palette(100),   
          cuts=100, at=seq(0,1,0.1), ylim=c(0,2), scales=list(y=list(at=NULL)))

This is the outcome:

enter image description here

Since, I do not understand how this levelplot really works, I can not make it work. What I would like to have is the colour strips to fill the whole window of the corresponding x (Time).

Alternative solution with other method.

Basically, I'm trying here to plot the increasing risk over time, where the red is the highest risk = 1. I would like to visualize the sequence of possible increase or clustering risk over time.


Solution

  • From ?levelplot we're told that if the first argument is a matrix then "'x' provides the 'z' vector described above, while its rows and columns are interpreted as the 'x' and 'y' vectors respectively.", so

    > m = Data.mat[, 2, drop=FALSE]
    > dim(m)
    [1] 21  1
    > levelplot(m)
    

    plots a levelplot with 21 columns and 1 row, where the levels are determined by the values in m. The formula interface might look like

    > df <- data.frame(x=1, y=1:21, z=runif(21))
    > levelplot(z ~ y + x, df)
    

    (these approaches do not quite result in the same image).