具有分类数据的栅格图的图例 [英] Legend of a raster map with categorical data

查看:367
本文介绍了具有分类数据的栅格图的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个包含4个不同值的栅格( 1 )描述类别的分类文字图例,例如 2 ,但带有彩色框:

I would like to plot a raster containing 4 different values (1) with a categorical text legend describing the categories such as 2 but with colour boxes:

我尝试过使用图例:

legend( 1,-20,legend = c("land","ocean/lake", "rivers","water bodies"))

但我不知道如何将一个值与显示的颜色相关联。有没有办法检索plot显示的颜色并在图例中使用?

but I don't know how to associate one value to the displayed color. Is there a way to retrieve the colour displayed with 'plot' and to use it in the legend?

推荐答案

rasterVis 包中包含一个 levelplot()的栅格方法,绘制了分类变量,并生成了一个适当的图例:

The rasterVis package includes a Raster method for levelplot(), which plots categorical variables, and produces an appropriate legend:

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="")

这篇关于具有分类数据的栅格图的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆