如何使用直接栅格属性表来传递栅格,并仅在栅格中显示的类显示图例? [英] How to legend a raster using directly the raster attribute table and displaying the legend only for class displayed in the raster?

查看:324
本文介绍了如何使用直接栅格属性表来传递栅格,并仅在栅格中显示的类显示图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用栅格属性表信息来创建光栅的图例,例如栅格。

解决方案

如果您愿意使用 lattice 图形, levelplot 方法
定义在 rasterVis

管理光栅* 其RAT级别不全部存在于
数据中:

  rcrop<  -  crop(r,extent(r,6,10,1,10))
levelplot(rcrop)

  rcrop<  -  crop(r,extent(r,1,5,1,10 ))
levelplot(rcrop)


I would like to use the raster attribute table information to create the legend of a raster such as the raster 1 and display the legend only for the class displayed in the raster. I build an example to explain what I would like to get.

1/ Build the raster

r <- raster(ncol=10, nrow=10)
values(r) <-sample(1:3,ncell(r),replace=T)

2/ Add the Raster Attribute Table

r <- ratify(r) # build the Raster Attibute table
rat <- levels(r)[[1]]#get the values of the unique cell frot the attribute table
rat$legend <- c('Class A', 'Class B', 'Class C')
levels(r) <- rat

3/ Plot the raster Fig 1

my_col=c('blue','red','green')
plot(r,col=my_col,legend=F,box=F,axes=F)
legend(x='top', legend =rat$legend,fill = my_col)

I would like to replace the legend =rat$legend parammeter by a properties of the raster linked to the ratser attribute table. I have tried different combination using levels() such as c(levels(r)[[1]][1]) but I generate a list and not a character not usable in the legend parameter.

4/ Crop and plot the raster to a part with only 2 classes (here the 4 pixels at the down right extent) Fig 2

rcrop<-crop(r,extent(r,9,10,9,10))
plot(rcrop,col=my_col,legend=F,box=F,axes=F)

For this second fig, I thus would like to diplay automatically only the legend of the class displayed on the raster 2.


Here is the solution proposed by Roman 4.

解决方案

If you are willing to use lattice graphics, the levelplot method defined in the rasterVis package is able to display categorical data with a legend based on the RAT:

library(raster)
library(rasterVis)

r <- raster(ncol=10, nrow=10)
values(r) <- rep(1:4, each=25)

r <- ratify(r) 
rat <- levels(r)[[1]]
rat$legend <- c('Class A', 'Class B', 'Class C', 'Class D')
levels(r) <- rat

levelplot(r)

Besides, I have commited some changes in the development version available at GitHub to manage a Raster* whose RAT levels are not all present in the data:

rcrop <- crop(r,extent(r,6,10,1,10))
levelplot(rcrop)

rcrop <- crop(r,extent(r,1,5,1,10))
levelplot(rcrop)

这篇关于如何使用直接栅格属性表来传递栅格,并仅在栅格中显示的类显示图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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