Home > database >  Set legend width to be 100% plot width for any geom
Set legend width to be 100% plot width for any geom

Time:06-09

Similar to enter image description here

I've tried the solutions from the links, which are to use units() and patchwork respectively, but those doesn't work with geom_col and return the next distorted legend enter image description here

CodePudding user response:

This one is from one of the mentioned so pages enter image description here

CodePudding user response:

The legend.margin argument allows you to edit the size of the legend using the margin function

library(gapminder)

ggplot(gapminder |> 
         filter(continent %in% c("Americas", "Europe"), country %in% c("Chile", "Spain")))  
  geom_col(aes(x = country, y = lifeExp, fill = continent), position = "dodge")  
  theme(legend.position = "bottom",
        legend.background = element_rect(fill = "gray"),
        legend.margin = margin(0,6,0,6, "cm"))

enter image description here

  • Related