您可以使用image.plot
从中fields
向图像添加图例。这里有一个例子:
首先我生成一些数据:
set.seed(1234)
x<- 1:5; y<- 1:5
z<- matrix(sample(c(1,2,3),25,rep=TRUE),ncol=5,byrow=TRUE)
然后使用fields
你可以拥有这个,它使用通常的image
参数添加一个图例。
# fields
library(fields)
image.plot(x,y,z,col = c("blue" , "red" ,"yellow"),
interpolate=TRUE)
请注意,如果要将光栅矩阵转换为颜色矩阵,可以执行以下操作:
## raster
r <- raster(ncol=5, nrow=5)
values(r) <- z
mm <- matrix(c("blue" , "red" ,"yellow")[values(r)],
ncol=5,byrow=TRUE)
[,1] [,2] [,3] [,4] [,5]
[1,] "blue" "red" "red" "red" "yellow"
[2,] "red" "blue" "blue" "red" "red"
[3,] "yellow" "red" "blue" "yellow" "blue"
[4,] "yellow" "blue" "blue" "blue" "blue"
[5,] "blue" "blue" "blue" "blue" "blue"
image
您无法绘制需要数值的颜色矩阵的问题。但是你可以grid.raster
从grid
包中使用:
library(grid)
grid.raster(mm,interpolate=FALSE)
编辑
axis.args
要手动修复图例,您可以使用 plot.image
## fields
image.plot(x,y,z,
col = c("red" , "green" ,"blue"),
axis.args=list( at=0:3, labels=0:3 ))