0

I am using levelplot() in R to generate a colored image of matrix (36 X 32). The matrix contains many NaN values in it. When I try to generate the levelplot using the following commands

range = c(815.4, 816.2)
matpalette <- colorRampPalette(c("blue", "black", "red"))(100)
levelplot(t(m1),scales = list(draw = FALSE), col.regions = matpalette, xlab = "", ylab = "", at = seq(min(m1), max(m1), length = 100), main=main)

I get the following error message:

Error in seq.default(min(m1), max(m1), length = 100) : 'from' cannot be NA, NaN or infinite

This error is understood. Instead of using min(m1) and max(m1) in the levelplot(), when I try using the following:

levelplot(m1,scales = list(draw = FALSE), col.regions = matpalette, xlab = "", ylab = "", at = seq(range[1], range[2], length = 100), main=main)

I get an empty plot, just with the colorbar and with the range specified as tick values.

enter image description here

What I need is a plot of the values in the matrix and the same colorbar and identical tick values as in the levelplot provided. Can anyone help me in this?

# Variant 2

 s <- seq(from=range[1], to=range[2], by=0.1)

 levelplot(t(m1), scales=list(tick.number=0), xlab=" ", ylab=" ", colorkey = list(at=s, labels=as.character(s)),col.regions = two.colors(n=256, start='blue', end='red', middle='black'), main=main)

The colorbar which I get is like this

enter image description here

Note: Range used is different

novicegeek
  • 773
  • 2
  • 9
  • 29
  • 2
    Clearly your matrix `m1` has `NA` values. Perhaps you should use `min(m1, na.rm=TRUE)` and `max(m1, na.rm=TRUE)`. – shadow Dec 02 '14 at 16:01
  • Also: Are you sure that `m2` has any values between 815.4 and 816.2? Because that would explain the problem... – shadow Dec 02 '14 at 16:04
  • Yes, thats the problem. I tried your suggestion and it worked, but now on the tick marks of the colorbar I get the values of the range which is in the dataset. Initially, I had used #variant2 (commands inserted in the question) to generate the lattice plot. But, in that case I get the image, but donot get a smooth colorbar. – novicegeek Dec 02 '14 at 16:15
  • How to get a smooth colorbar in this case? – novicegeek Dec 02 '14 at 16:18
  • 1
    Please add the matrix to make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – shadow Dec 02 '14 at 16:22
  • Here is the link to the csv file containing the matrix http://www.filedropper.com/test_34 – novicegeek Dec 02 '14 at 16:56

1 Answers1

1

Like this??

test <- read.csv("test.csv")
library(lattice)
library(colorRamps)  # for colorRampPalette
matpalette <- colorRampPalette(c("blue", "black", "red"))(100)
levelplot(as.matrix(test[,-1]),col.regions=matpalette)

Here's more or less the same thing using ggplot

library(ggplot2)
library(reshape2)    # for melt(...)
library(grid)        # for unit(...)
gg <- melt(test,id="X")
ggplot(gg,aes(x=X,y=variable,fill=value))+
  geom_tile()+
  scale_fill_gradientn(name="",colours=matpalette,na.value="white")+
  scale_x_continuous(expand=c(0,0))+
  scale_y_discrete(expand=c(0,0))+
  coord_fixed()+ theme_bw()+
  theme(legend.key.height=unit(.15,"npc"))

jlhoward
  • 58,004
  • 7
  • 97
  • 140