3

I am trying to trim the white space from a map plot that was created using geom_sf(). I have found multiple resources discussing removing white space when saving a plot using ggsave(). I am hoping to remove the white space without having to save it, though.

Removing white space using ggsave() - R ggplot, remove white margins in ggsave/ggplot

Please find below a reproducible example:

library(spData); library(sf); library(ggplot2)
library(cowplot)
data("us_states", package = "spData")

north_carolina = read_sf(system.file("shape/nc.shp", package = "sf"))

nc_plot <- ggplot() + geom_sf(data = north_carolina)

Now, I want nc_plot to appear at the very bottom left hand corner of a ggdraw() plot. The coordinates of a ggdraw() plot start from the bottom left. So, x = 0, y = 0 should be the bottom left.

ggdraw() + draw_plot(nc_plot, x = 0, y = 0)

As you can see, though, the plot is all the way left but not all the way at the bottom. This is because there is white space above and below the plot. I want to remove the white space from the plot so that I can have the map at the bottom left.

enter image description here

lovalery
  • 4,524
  • 3
  • 14
  • 28
nateroe
  • 487
  • 3
  • 20

2 Answers2

3

To complete @Skaqqs' answer and offer you an alternative solution, you can also continue to use the cowplot package by modifying your last line of code as follows:

  • Code
library(sf)
library(ggplot2)
library(cowplot)


ggdraw() + draw_plot(nc_plot, x = -0.24, y = -0.34, scale = 0.5)
  • Visualization

Created on 2022-01-11 by the reprex package (v2.0.1)

lovalery
  • 4,524
  • 3
  • 14
  • 28
  • 1
    nicely done. If this is the result OP was shooting for, I'll delete my answer and add my code as a comment here. – Skaqqs Jan 11 '22 at 19:31
  • Thanks a lot for your very nice feedback @Skaqqs. I think your solution is worth mine (I upvoted it) and your post would deserve to stay as an answer as well. That said, it is up to you. Cheers. – lovalery Jan 11 '22 at 23:56
2

Can share more about what you're trying to accomplish? You say you want to remove whitespace, but you also want the plot to be at the bottom left of whitespace. What is your ultimate goal here? Let me know if I missed something.

I believe the whitespace is added outside of your ggplot based on your graphic device and ggplot margins. You can control the ggplot margins in theme() (like in this question/answer). You could remove the whitespace by defining your graphic device to the same dimensions as your ggplot object.

If you wanted to place the plot at the bottom left of whitespace, you could define your graphic device space and then plot within that area with one of your favorite plot layout tools. For example:

g <- ggplotGrob(nc_plot)
ggplot() + theme_void() + xlim(0,10) + ylim(0,10) +
  annotation_custom(
    grob = g,
    xmin = -0.5,
    xmax = 3,
    ymin = -2,
    ymax = 5)

enter image description here

edit: example of the role of plot and plot margins (blue area) and the graphic device. Note that the graphic device fills the plotting window with whitespace depending on how the device is sized. The total plot area is shown in blue.

  geom_sf(data = north_carolina) + 
  theme(panel.background=element_blank(),
        panel.spacing = unit(c(0, 0, 0, 0), "cm"),       
        plot.background = element_rect(fill = "lightblue",colour = NA),
        plot.margin = unit(c(0, 0, 0, 0), "null"),  # Edited code
        legend.position = 'none')
plot(p)

enter image description here

I can resize the window to remove whitespace:

enter image description here

Skaqqs
  • 4,010
  • 1
  • 7
  • 21
  • I thought that removing white space would make ```ggdraw() + draw_plot(nc_plot)``` put the plot in the bottom left on its own. The answer that is currently accepted solves the final goal in an easy way, but does not do so by removing white space. ```ggdraw() + draw_plot(nc_plot, x = 0, y = 0)``` means that the plot should be in the bottom left, correct? If the plot was created without any white space surrounding the plot, wouldn't you expect the result to look like the image you and @lovalery provided? – nateroe Jan 12 '22 at 18:02
  • I would change the accepted answer if someone proposed a solution that removes white space surrounding the plot so that ```ggdraw() + draw_plot(nc_plot, x = 0, y = 0)``` makes the plot appear in the bottom left. Is this possible? – nateroe Jan 12 '22 at 18:04