简体   繁体   English

栅格类未出现在图例中

[英]Raster classes not appearing in legend

I created this raster .tiff file.我创建了这个光栅.tiff文件。 It is available from here .它可以从这里获得。

I had created the countieslandcover.tiff through this code:我通过这段代码创建了countieslandcover.tiff

countieslandcover <- aggregate(landcover_countiesmap, fact = 3, fun = min, expand = F, na.rm = T)

whereby landcover_countiesmap was the initial raster with 13988 rows and 16304 columns before downsampling.其中landcover_countiesmap是在下采样之前具有 13988 行和 16304 列的初始栅格。 When I tried drawing this initial raster landcover_countiesmap with 'tmap` I got an error:当我尝试使用“tmap”绘制这个初始栅格landcover_countiesmap时,出现错误:

Warning in fetch(.x, ..., downsample = downsample) :
  with RasterIO defined, argument downsample is ignored
stars_proxy object shown at 16304 by 13998 cells.
Error: cannot allocate vector of size 870.6 Mb
Error during wrapup: cannot allocate vector of size 870.6 Mb
Error: no more error handlers available (recursive errors?); invoking 'abort' restart

This is when I decided to downsample the raster to countieslandcover.tiff using the aggregate function. Upon doing so, the raster was able to be drawn using tmap .这是我决定使用aggregate function 将栅格下采样到countieslandcover.tiff的时候。这样做后,可以使用tmap绘制栅格。 However, the issue is that my values do not appear distinctly on the legend.然而,问题是我的价值观并没有清楚地出现在图例上。 Instead of '1', '2', '3' and so on they appear as '1 to 2', '2 to 3' as in the image below.它们不是“1”、“2”、“3”等,而是显示为“1 到 2”、“2 到 3”,如下图所示。

图例值应该清楚地出现,而不是成对出现

Here is the raster metadata for countieslandcover.tiff , the result of making it coarser using the aggregate function.这是countieslandcover.tiff的栅格元数据,是使用aggregate function 使其变得更粗糙的结果。

> class      : RasterLayer 
> dimensions : 4666, 5434, 25355044  (nrow, ncol, ncell)
> resolution : 0.0005555558, 0.0005555558  (x, y)
> extent     : 34.34335, 37.36224, -1.442097, 1.150127  (xmin, xmax, ymin, ymax)
> crs        : +proj=longlat +datum=WGS84 +no_defs 
> source     : r_tmp_2022-04-19_212648_10928_21780.grd 
> names      : Kenya_Sentinel2_LULC2016 
> values     : 1, 10  (min, max)

Here is the code I used when drawing the raster with tmap这是我用tmap绘制栅格时使用的代码

tm_shape(countieslandcover) + tm_raster(palette = terrain.colors(10, 0.7, rev = F), n= 10, legend.show = T, legend.is.portrait = T, colorNA = NULL) + 
  tm_layout(title = 'Landcover types of top 5 counties by population', legend.position = c('left', 'bottom'))

How can I make the values appear distinctively (like separated) on both the map and the legend?我怎样才能使values在 map 和图例上都显得与众不同(如分开)? My values stands for landcover classes and will make sense if each appears standing on its own on the legend (ie. '1', '2' and so on) two landcovers can't exist on the same place.我的values代表土地覆盖类别,如果每个类别在图例中独立出现(即“1”、“2”等),两个土地覆盖不能存在于同一个地方,这将是有意义的。 I tried reproject the raster to a local datum, +init=epsg:32737 and also played with the tm_raster arguments to no avail.我尝试将光栅重新投影到本地基准+init=epsg:32737并且还尝试使用tm_raster arguments 无济于事。

Example categorical raster示例分类栅格

library(terra)
set.seed(0)
r <- rast(nrows=10, ncols=10)
values(r) <- sample(3, ncell(r), replace=TRUE) - 1
cls <- c("forest", "water", "urban")
levels(x) <- cls
names(x) <- "land cover"
plot(x)

The standard way to aggregate this would be with the "modal" function聚合这个的标准方法是使用“模态”function

a <- aggregate(x, 2, "modal")
plot(a)

But you use the min function, which probably makes no sense anyway, and then you loose the levels但是你使用了min function,这可能毫无意义,然后你放松了水平

b <- aggregate(x, 2, "min")
plot(b)

But you could restore the categories with但你可以恢复类别

levels(b) <- levels(x)
plot(b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM