【问题标题】:Tmap: increase space between plot and legendTmap:增加情节和图例之间的空间
【发布时间】:2021-02-14 09:43:34
【问题描述】:

我想增加 tmap 中绘图和图例之间的空间。使用 'legend.outside.size' 有效,但也改变了整个情节的比例。有没有办法把传奇往下移?理想情况下,下部面板和图例之间的间距应大于垂直面板之间的间距。

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  ) 

【问题讨论】:

    标签: r tmap


    【解决方案1】:

    您似乎正在寻找扩展tmap 自定义的能力。我建议您可以使用不同的包以更少的代码获得更好、更容易定制的图。例如,使用rasterVis 允许对ggplot 进行所有自定义,例如,使用各种theme 选项更改图例栏的大小、宽度、间距、中断和颜色很简单:

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

    【讨论】:

    • 谢谢。我一直在寻找一种在 tmap 中执行此操作的方法,因为 tmap 几乎具有我想要的所有其他功能并且非常易于使用。我在 shapefile (wrld_simpl) 上绘制栅格数据,在 tmap 中很容易做到这一点,但我还没有完全弄清楚如何使用 ggplot 来做到这一点。
    • @webbe 在 ggplot 中也应该很容易做到