Search code examples
rggplot2marginbounding-box

Remove inner padding in ggplot


In the code below, how do I minimize/remove the inner padding to make the green polygon span the entire gray bounding box?

suppressMessages(library(GISTools))
suppressMessages(library(ggplot2))
data(newhaven)

blocks_df <- fortify(blocks)
ggplot(data = blocks_df) +
  geom_polygon(aes(x=long, y=lat, group = group), fill = "darkolivegreen4") +
  coord_equal() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

enter image description here Thanks.


Solution

  • The coord_equal() layer can take an expand argument -- set that to FALSE and the plot window will fit to the plotted data's size:

    ... 
    ggplot(data = blocks_df) +
      geom_polygon(aes(x=long, y=lat, group = group), fill = "darkolivegreen4") +
      # set `expand=FALSE` 
      coord_equal(expand=FALSE) +
      theme(axis.title = element_blank(),
            axis.text = element_blank(),
            axis.ticks = element_blank()))
    

    And a quick follow-up: note that the expand parameter on most other coordinate layers (e.g. scale_x_continuous()) needs to be a vector of length two (for "multiplicative and additive expansion constants"). So you'd say e.g. scale_x_continuous(expand=c(0,0)) in that case. :p