geom_sf in blogdown: remove white frame

When plotting a ggplot graph with geom_sf the resulting plot has a white 'frame'. Is there an easy way in blogdown to set the color of this frame, i.e. to the background color of the blogdown theme.

The issue has been raised before, here , here, here, and here but I was wondering whether there is a more 'direct' solution in blogdown.

Many thanks.

library(tidyverse)
library(sf)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc) +
  geom_sf(aes(fill = AREA))+
  theme(plot.background=element_rect(fill="grey80"))

1 Like

Themes provide almost unlimited control over the display of ggplot objects. Often this is the simplest to work from.

library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.1.3, PROJ 7.1.0

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc) +
  geom_sf(aes(fill = AREA)) +
  theme_minimal()

Created on 2020-11-13 by the reprex package (v0.3.0.9001)

Many thanks. I am aware of the theme properties and I actually wasn't referring to the grey area under the map, but the white space above and below the plot (if you hover over the image it becomes clearer).

In the meantime I found this post which seems to do the trick.

But many thanks in any case.

cowplot::ggdraw(p) + theme(panel.background = element_rect(fill = "#2C3E4F", colour = "#2C3E4F"))
1 Like

Just as an addition - The solution with cowplot seems not to work when your plot includes an inset created with patchwork. The alignment gets messed up.

1 Like