# [R] heatmap with levelplot

Antje niederlein-rstat at yahoo.de
Mon Jan 26 15:37:55 CET 2009

```I played a little bit around and got the following solution which works for
now, though it seems to be too complicated to me.
If anybody else know another solution - please let me know!!!

library(lattice)
my.mat <- matrix(rnorm(800), nrow = 40)

colorFun <- colorRampPalette(c("yellow","red"))

b <- boxplot(my.mat, plot = FALSE)
thr <- c(b\$stats[1],b\$stats[5])
col.bins <- 100
step <- abs(thr[2] - thr[1])/50

limit <- ifelse(min(my.mat) > thr[1] - step, min(my.mat) - step, min(my.mat))
lp <- rev(seq(thr[1] - step, limit - step, -step))
mp <- seq(thr[1], thr[2], step)
limit <- ifelse(max(my.mat) < thr[2] + step, max(my.mat) + step, max(my.mat))
up <- seq(thr[2] + step, limit + step, step)

my.at <- c(lp,mp,up)

my.col.regions <- c(rep("green", length(lp)), colorFun(length(mp)), rep("blue",
length(up)) )

levelplot(my.mat, at = my.at, col.regions = my.col.regions)

Antje schrieb:
> Hi there,
>
> I'd like to create a heatmap from my matrix with
> a) a defined color range (lets say from yellow to red)
> b) using striking colors above and below a certain threshold (above =
> "green", below = "blue")
>
> Example matrix (there should be a few outliers generated...) + simple
> levelplot without outliers marked:
>
> library(lattice)
> my.mat <- matrix(rnorm(800), nrow = 40)
> threshold <- c(-1,1) # should be used for the extreme colors
> colorFun <- colorRampPalette(c("yellow","red"))
> levelplot(my.mat, col.regions = colorFun(50))
>
>
> I don't know how to handle the extrem values...
>
> Can anybody help?
>
> Antje
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

```