Display different states with geom_sf using face wrap and scaling

First, I know about this answer: Mapping different states in R using face wrappers
But I am working with an sf library object.
It seems that facet_wrap(scales = "free") not available for objects built with geom_sf in ggplot2. I get this message:

Erreur: free scales are only supported with coord_cartesian() and coord_flip()

Is there any option I missed?
cowplot anyone solve the problem without being forced to use a cowplot (or any other mesh set)?

Indeed, here is an example. I would like to show different French regions separately in faces, but with their own x / y limits.

Result without scales = "free"

Scales are calculated taking into account the entire map.

 FRA <- raster::getData(name = "GADM", country = "FRA", level = 1) FRA_sf <- st_as_sf(FRA) g <- ggplot(FRA_sf) + geom_sf() + facet_wrap(~NAME_1) 

face areas with geom_sf

Result Using Cowplot

I need to use the ggplots list and then combine them. This is the target result. It is cleaner. But I also need a clean way to add legend. (I know that there may be a common legend, as in this other SO question: facet wrapper distorts state mappings in R )

 g <- purrr::map(FRA_sf$NAME_1, function(x) { ggplot() + geom_sf(data = filter(FRA_sf, NAME_1 == x)) + guides(fill = FALSE) + ggtitle(x) }) g2 <- cowplot::plot_grid(plotlist = g) 

face areas with geom_sf and cowplot

+7
r ggplot2 sf
source share
1 answer

I know that you are looking for a solution using ggplot2 , but I found that the tmap package may be a choice, depending on your needs. The tmap syntax tmap similar to ggplot2 , it can also accept an sf object. Take FRA_sf as an example, we can do something like this.

 library(tmap) tm_shape(FRA_sf) + tm_borders() + tm_facets(by = "NAME_1") 

enter image description here

Or we can use geom_spatial from the geom_spatial package, but geom_spatial only accepts a Spatial* object.

 library(ggplot2) library(ggspatial) ggplot() + geom_spatial(FRA) + # FRA is a SpatialPolygonsDataFrame object facet_wrap(~NAME_1, scales = "free") 

enter image description here

+3
source share

All Articles