简体   繁体   中英

Raster classes not appearing in legend

I created this raster .tiff file. It is available from here .

I had created the countieslandcover.tiff through this code:

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. When I tried drawing this initial raster landcover_countiesmap with 'tmap` I got an error:

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 . 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.

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

Here is the raster metadata for countieslandcover.tiff , the result of making it coarser using the 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

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? 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. I tried reproject the raster to a local datum, +init=epsg:32737 and also played with the tm_raster arguments to no avail.

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

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

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

But you could restore the categories with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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