Wra*_*raf 5 r raster map legend categorical-data
我想使用栅格属性表信息来创建栅格的图例,例如栅格1,并仅为栅格中显示的类显示图例.我建立了一个例子来解释我想要得到什么.
1 /构建栅格
r <- raster(ncol=10, nrow=10)
values(r) <-sample(1:3,ncell(r),replace=T)
Run Code Online (Sandbox Code Playgroud)
2 /添加栅格属性表
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
Run Code Online (Sandbox Code Playgroud)
3 /绘制光栅图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)
Run Code Online (Sandbox Code Playgroud)
我想用legend =rat$legend
链接到ratser属性表的栅格属性替换参数.我尝试使用levels()
这样的不同组合,c(levels(r)[[1]][1])
但我生成一个列表,而不是在legend参数中不可用的字符.
4 /裁剪并将光栅绘制到只有2个类的部分(这里是右下角的4个像素)图2
rcrop<-crop(r,extent(r,9,10,9,10))
plot(rcrop,col=my_col,legend=F,box=F,axes=F)
Run Code Online (Sandbox Code Playgroud)
对于第二个图,我因此想自动只显示光栅2上显示的类的图例.
这是Roman 4提出的解决方案.
如果您愿意使用lattice
图形,包levelplot
中定义的方法可以根据 RAT 显示带有图例的分类数据:rasterVis
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)
Run Code Online (Sandbox Code Playgroud)
此外,我
在 GitHub 上提供的开发版本
中
进行了一些更改
,以管理数据中未全部出现的 RAT 级别:Raster*
rcrop <- crop(r,extent(r,6,10,1,10))
levelplot(rcrop)
Run Code Online (Sandbox Code Playgroud)
rcrop <- crop(r,extent(r,1,5,1,10))
levelplot(rcrop)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2475 次 |
最近记录: |