我应该如何解决光栅包中未对齐的颜色条刻度?

问题描述 投票:0回答:1

我正在尝试使用

raster
包来可视化 GeoTIFF 文件。我不得不说这是一个不错的选择,因为我比
terra
包更喜欢它的 colorbar 风格。但同时我也发现了一个问题:在它的输出结果中,colorbar的tick和break(或者说颜色块)没有对齐。我真诚地希望这个问题能够得到解答,我也尝试着看了在帮助文档和搜索引擎中寻求帮助,但收效甚微。

这是我的输出:Rater plot

当我们仔细观察颜色条时:misaligned colorbar ticks

这是我使用的代码:

library(raster)

set.seed(123)

# Define a color palette
mycolorbar <- colorRampPalette(c(
  "#fb6203",
  "#ef9c35",
  "#f4d095",
  "#ffffff",
  "#78c6ec",
  "#349dfb",
  "#035ba8"
))

dummy_raster <- raster(matrix(nrow = 10,ncol = 10))
values(dummy_raster) <- runif(100, min = -1, max = 1)


# Plot and save as PDF
pdf(file = "mytest01082.pdf", width = 5, height = 6)

plot(dummy_raster,
     breaks = seq(-1, 1, 0.1),
     col = mycolorbar(20),
     horizontal = TRUE,
     legend.width = 1,
     legend.shrink = 1,
     axis.args = list(
       at = seq(-1, 1, 0.5),
       tcl = 0.3
     ))

dev.off()

我想知道是否可以通过一些简单的参数设置来解决这个问题。这些未对齐的刻度可能会导致一些误解。另外,我也知道

terra
包将来很快就会取代
raster
包,我也非常希望在使用
terra
包实现我想要的 colorbar 样式时得到一些帮助。

衷心感谢您的帮助!

r geospatial raster terra
1个回答
0
投票

“光栅”显示连续的图例,即使您有课程,因为您使用了中断。我相信“terra”通过使用“间隔”图例表现得更好。但您可以覆盖该默认值。

您的示例数据

library(terra)   
set.seed(123)
x <- rast(matrix(runif(100, min=-1, max=1), nrow=10, ncol=10))
mycolorbar <- colorRampPalette(c("#fb6203", "#ef9c35", "#f4d095", "#ffffff", "#78c6ec", "#349dfb", "#035ba8"))
brks <- seq(-1, 1, 0.5)  
cols <- mycolorbar(length(brks)-1)

默认的“间隔”图例

plot(x, breaks=brks, col=cols, mar=c(2,2,2,6), plg=list(cex=1.1))

地图下方

plot(x, breaks=brks, col=cols, mar=c(6,2,2,2),
    plg=list(x=0, y=-0.1, horiz = TRUE, cex=1.2))

在地图下方使用连续图例,并设置图例抽控。

plot(x, type="continuous", breaks=brks, col=cols, mar=c(6,2,2,2),
    plg=list(at=brks, labels=brks, horiz=TRUE, x="bottom"))

© www.soinside.com 2019 - 2024. All rights reserved.