Legend of a raster map with categorical data
我想绘制一个包含4个不同值(1)的栅格,并使用描述类别(例如2)但带有颜色框的分类文字图例:
我尝试使用诸如:
的图例
1 | legend( 1,-20,legend = c("land","ocean/lake","rivers","water bodies")) |
但是我不知道如何将一个值与显示的颜色关联。有没有办法检索用\\'plot \\'显示的颜色并在图例中使用它?
rasterVis软件包包括用于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | library(raster) library(rasterVis) ## Example data r <- raster(ncol=4, nrow=2) r[] <- sample(1:4, size=ncell(r), replace=TRUE) r <- as.factor(r) ## Add a landcover column to the Raster Attribute Table rat <- levels(r)[[1]] rat[["landcover"]] <- c("land","ocean/lake","rivers","water bodies") levels(r) <- rat ## Plot levelplot(r, col.regions=rev(terrain.colors(4)), xlab="", ylab="") |
默认情况下,栅格图中使用的颜色由
1 2 | my_col = rev(terrain.colors(n = 4)) # my_col = c('beige','red','green','blue') |
首先使用颜色顺序绘制地图。
1 | plot(my_raster, legend = FALSE, col = my_col) |
在左下方添加自定义图例。使用
1 | legend(x='bottomleft', legend = c("land","ocean/lake","rivers","water bodies"), fill = my_col) |