简体   繁体   中英

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:

在此处输入图片说明

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.

Unfortunately I don't know much about lattice , but I noted your "Alternative solution with other method", so may I suggest another possibility:

library(plotrix)
color2D.matplot(t(Data[ , 2]), show.legend = TRUE, extremes = c("yellow", "red"))

Heaps of things to do to make it prettier. Still, a start. Of course it is important to consider the breaks in your time variable. In this very simple attempt, regular intervals are implicitly assumed, which happens to be the case in your example.

Update Following the advice in the 'Details' section in ?color2D.matplot : "The user will have to adjust the plot device dimensions to get regular squares or hexagons, especially when the matrix is not square". Well, well, quite ugly solution.

par(mar = c(5.1, 4.1, 0, 2.1))
windows(width = 10, height = 2.5)
color2D.matplot(t(Data[ , 2]),
                show.legend = TRUE,
                axes = TRUE,
                xlab = "",
                ylab = "",
                extremes = c("yellow", "red"))

在此处输入图片说明

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM