How to add space between legend and map using geom_sf

38 Views Asked by At

I'm trying to do a map with ggplot + geom_sf and the legend is over the map. How can I increase on the bottom (or padding) of the map? I've tried plot.margin = margin(b = 2) but it is the margin then entire plot increasing under the caption, then didn't work.

My code:

ggplot(shp_dados_ce) +
  geom_sf(aes(fill = populacao_2022), ) +
  theme_void() +
  scale_fill_viridis(
    trans = "log10"
    , name="Habitantes"
    , guide = guide_legend( 
      keyheight = 0.25
      , keywidth= 1
      , label.position = "bottom"
      , title.position = 'top'
      , nrow=1
    ) 
  ) +
  labs(
    title = "title",
    subtitle = "subtitle",
    caption = "caption"
  ) +
  theme(
    text = element_text(color = "#22211d"),
    panel.background = element_rect(fill = "#f5f5f2", color = NA),
    panel.border = element_rect(fill = NA, color = "green"),
    
    plot.background = element_rect(fill = "#f5f5f2", color = NA),
    plot.margin = margin(b = 2),
    plot.title = element_text(
      size= 16
      , hjust=0.05
      , vjust=0.05
      , color = "#4e4d47"
    ),
    plot.subtitle = element_text(
      size= 11
      , hjust=0.05
      , color = "#4e4d47"
      , margin = margin(
        b = -0.25
        , t = 5
      )
    ),
    
    legend.background = element_rect(fill = "green", color = NA),
    legend.box.background = element_rect(color = "black"),
    legend.position = c(0.5, 0.04),
    legend.text = element_text(size = 6),
    legend.title = element_text(size = 6),
    legend.margin = margin(l = 0.5 ,t = 2),
    legend.box.margin = margin(3,2,2,2)
  )

And my map

enter image description here

Thanks.

1

There are 1 best solutions below

0
walves On

Solved with legend.position = 'bottom'.

Thanks Allan Cameron for his comment.