簡體   English   中英

terra 中的水平圖例 R package

[英]horizontal legend in terra R package

對於terra ,我想將圖例水平放置在map下方。從help看來,這可以通過將字符傳遞給legend (例如legend = "bottomleft" )或將列表傳遞給plg來完成,其中plg來自基地-R 圖例 function。但是,對我來說這些都不起作用。 請參閱下面的代碼示例。

謝謝!

rm(list = ls())
library(terra)

f <- system.file("ex/elev.tif", package="terra") 
r <- rast(f)

terra::plot(r)
terra::plot(r, legend = "bottomleft")
terra::plot(r, plg = list(horiz = TRUE))
terra::plot(r, plg = list(x = 6, y = 49.5, horiz = TRUE))

在所有情況下,生成的 map 如下所示:

在此處輸入圖像描述

編輯:通過將范圍傳遞給 plg,覆蓋 plot 軸限制之外的區域,圖例移動到正確的位置,但horiz = TRUE仍然不旋轉它:

e <- c(5.6, 6.6, 49.2, 49.3)
terra::plot(r, plg = list(ext = e, horizontal = TRUE), mar = rep(4, 4))

在此處輸入圖像描述

查看gitHub 中的 terra 代碼,我找到了解決方案。 您需要通過plg列表傳遞locext arguments:

rm(list = ls())
library(terra)
library(RColorBrewer)

f <- system.file("ex/elev.tif", package="terra") 
r <- rast(f)

e <- c(5.5, 7.0, 49.35, 49.38)
terra::plot(r, plg = list(ext = e, loc = "bottom"))

# Change color scale, range, ticks
terra::plot(r,
            col = brewer.pal(7, "BrBG"), range = c(150, 500),
            plg = list(ext = l_ext, loc = "bottom", title = "myvar",
                       at = c(150, 200, 250, 350, 450, 500)))

在此處輸入圖像描述

在此處輸入圖像描述

您可以使用 package rasterspplot ,它可以讓您選擇 plot plot 下面的圖例。您可以使用以下代碼:

library(raster)
library(terra)

f <- system.file("ex/elev.tif", package="terra") 
r <- rast(f)
spplot(r, scales = list(draw = TRUE), colorkey = list(space = "bottom"))

Output:

在此處輸入圖像描述

暫無
暫無

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

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