R how to control spacing of colour bar/legend

和自甴很熟 提交于 2019-12-06 04:43:19

It's ugly, but you can use plot for the map and image.plot for your legend. You will need to add the labels of your custom breaks to the position of the equally spaced breaks.

Plot map without legend:

library(raster)
r <- raster(ncol=5, nrow=4)
r[] <- 1:20
my_breaks = c(0,1,2,3,5,10,20)
n = 6
my_col    = rainbow(n)
plot(r, breaks = my_breaks, col = my_col, legend = FALSE, zlim=c(0,20))

The default breaks will be equally spaced from 0 to 20:

def_breaks = seq(0,20,length.out=(n+1))

Add legend using image.plot from the fields package, placing custom break labels at default break positions:

library(fields)
image.plot(r, zlim = c(0,20), 
           legend.only = TRUE, 
           col = my_col,
           axis.args = list(at = def_breaks, labels = my_breaks))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!