使用levelplot或spplot调整colorkey / colorbar / legends和图之间的间隙

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

我使用levelplot绘制了栅格数据

> library(raster)
> ras > raster("DEM.tif")
> spplot(S_dub, colorkey = list(space ="right",height=1, width=1, hjust = .3), 
         col.regions = coul,scales=list(alternating=TRUE), 
         xlab = list(label = "Longitude", vjust = -.2),ylab = "Latitude")

enter image description here

问题是,颜色栏距离绘图有点远,我想使其更靠近绘图。实际上,我注意到只有在添加代码scales=list(alternating=TRUE)时,它才会发生。我尝试了hjust,但是它不起作用。

如何减少绘图和颜色栏之间的间隙/空间?

r legend raster colorbar levelplot
1个回答
0
投票

您可以通过修改以下key$framevp$x <- unit(0.74, units = 'npc')行中的值进行调整:

library(grid)
data(meuse.grid)
gridded(meuse.grid)=~x+y

s <- spplot(meuse.grid[,'dist'],
            colorkey = list(space = 'right', height = 0.735, width = 1), 
            scales=list(alternating = TRUE), 
            xlab = list(label = 'Longitude', vjust = -.2), 
            ylab = 'Latitude')

key <- draw.colorkey(s$legend[[1]]$args$key)
s$legend <- NULL

key$framevp$x <- unit(0.74, units = 'npc')
key$framevp$y <- unit(0.525, units = 'npc')

s
grid.draw(key)
© www.soinside.com 2019 - 2024. All rights reserved.