简体   繁体   中英

Tmap: increase space between plot and legend

I would like to increase the space between the plot and the legend in tmap. Using 'legend.outside.size' works, but also changes the ratio of the entire plot. Is there a way to just move down the legend? Ideally, the spacing between the lower panels and the legend would be greater than the spacing between vertical panels.

library(tmap)
library(raster)

r <- raster::raster(matrix(runif(100), 10, 10))
s <- raster::raster(matrix(runif(100), 10, 10))
t <- raster::raster(matrix(runif(100), 10, 10))
u <- raster::raster(matrix(runif(100), 10, 10))

allrasters<-stack(r,s,t,u)
tm_shape(allrasters,is.master = TRUE) +
  tm_raster (title = '',
            legend.is.portrait = FALSE, 
            legend.format = list(text.align='center'),
             style="cont",
             palette =  "RdBu")+ 
  
  tm_layout(main.title= '', 
            inner.margins= c(0.0,0.0,0.0,0.0), 
            outer.margins = c(0,0.04,0,0), 
            legend.text.size = 1.5,
            legend.outside = TRUE,
            legend.outside.position = 'bottom',
            legend.outside.size = 0.15,
            legend.frame=TRUE,
            legend.just = c('center','bottom'),
            legend.position = c('center','BOTTOM'),
            panel.labels = as.character(c(2013:2018)), 
            panel.label.height=1.5, panel.label.size=1.5, 
            frame = FALSE, frame.lwd = NA, panel.label.bg.color = NA  ) 

在此处输入图片说明

You seem to be looking for the ability to stretch the customisation of tmap . I would suggest that you could get better and more easily customizable plots with less code using a different package. For example, using rasterVis allows all the customization of a ggplot , where for example it is trivial to change the size, width, spacing, breaks and colors of the legend bar using the various theme options:

library(rasterVis)

gplot(allrasters) + 
  geom_tile(aes(fill = value)) + 
  facet_wrap(~variable, 
             labeller = labeller(variable = function(x) 2013:2016)) + 
  scale_fill_distiller(palette = "RdBu", direction = 1,
                       breaks = c(0.2, 0.5, 0.8), name = "") +
  coord_equal() +
  theme_void() +
  theme(text = element_text(size = 14, face = "bold"),
        legend.position = "bottom",
        legend.key.size = unit(20, "points"),
        legend.box.spacing = unit(25, "points"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM