0

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  ) 

enter image description here

webbe
  • 130
  • 8

2 Answers2

0

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"))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thanks. I was looking for a way to do it in tmap because tmap has almost all the other features I want and is super easy to use. I am plotting raster data over a shapefile (wrld_simpl) and it's easy to do that in tmap but I haven't quite figured out how to do it using ggplot yet. – webbe Nov 01 '20 at 18:39
  • @webbe it should be easy to do in ggplot too – Allan Cameron Nov 02 '20 at 07:34
-1

How do you position the title and legend in tmap? has an answer to your query using some bbox settings in tmap. It helped me in a similar problem.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34515509) – user12256545 Jun 10 '23 at 13:07