簡體   English   中英

柵格類未出現在圖例中

[英]Raster classes not appearing in legend

我創建了這個光柵.tiff文件。 它可以從這里獲得。

我通過這段代碼創建了countieslandcover.tiff

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

其中landcover_countiesmap是在下采樣之前具有 13988 行和 16304 列的初始柵格。 當我嘗試使用“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

這是我決定使用aggregate function 將柵格下采樣到countieslandcover.tiff的時候。這樣做后,可以使用tmap繪制柵格。 然而,問題是我的價值觀並沒有清楚地出現在圖例上。 它們不是“1”、“2”、“3”等,而是顯示為“1 到 2”、“2 到 3”,如下圖所示。

圖例值應該清楚地出現,而不是成對出現

這是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)

這是我用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'))

我怎樣才能使values在 map 和圖例上都顯得與眾不同(如分開)? 我的values代表土地覆蓋類別,如果每個類別在圖例中獨立出現(即“1”、“2”等),兩個土地覆蓋不能存在於同一個地方,這將是有意義的。 我嘗試將光柵重新投影到本地基准+init=epsg:32737並且還嘗試使用tm_raster arguments 無濟於事。

示例分類柵格

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)

聚合這個的標准方法是使用“模態”function

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

但是你使用了min function,這可能毫無意義,然后你放松了水平

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

但你可以恢復類別

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM