如何解决R中的空levelplot()问题?

如何解决R中的空levelplot()问题?,r,matrix,nan,colorbar,levelplot,R,Matrix,Nan,Colorbar,Levelplot,我使用R中的levelplot生成矩阵36 X 32的彩色图像。矩阵中包含许多NaN值。当我尝试使用以下命令生成levelplot时 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 = "",

我使用R中的levelplot生成矩阵36 X 32的彩色图像。矩阵中包含许多NaN值。当我尝试使用以下命令生成levelplot时

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)
这个错误是可以理解的。当我尝试使用以下内容时,不要在levelplot中使用minm1和maxm1:

levelplot(m1,scales = list(draw = FALSE), col.regions = matpalette, xlab = "", ylab = "", at = seq(range[1], range[2], length = 100), main=main)
我得到一个空的绘图,只有颜色条和指定为刻度值的范围

我需要的是矩阵中的值的绘图,以及与levelplot中相同的颜色栏和相同的刻度值。有人能帮我吗

变式2

我得到的颜色条是这样的

注意:使用的范围与此不同

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)

显然,矩阵m1有NA值。也许您应该使用minm1,na.rm=TRUE和maxm1,na.rm=TRUE。另外:您确定m2的值在815.4和816.2之间吗?因为这可以解释问题……是的,这就是问题所在。我尝试了你的建议,效果很好,但现在在颜色栏的记号上,我得到了数据集中的范围值。最初,我使用问题中插入的variant2命令生成晶格图。但是,在这种情况下,我得到了图像,但没有得到平滑的色条。在这种情况下,如何获得平滑的色条?请添加矩阵来制作示例。
 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)
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)
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"))