R语言 Tmap:增加图和图例之间的间距

vom3gejh  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(127)

我想增加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  )

sqxo8psd

sqxo8psd1#

您似乎正在寻找扩展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"))

91zkwejq

91zkwejq2#

How do you position the title and legend in tmap?使用tmap中的一些bbox设置来回答您的查询。它帮助我解决了类似的问题。

相关问题