First, I am aware of this answer : Mapping different states in R using facet wrap
But I work with object of library sf
.
It seems that facet_wrap(scales = "free")
is not available for objects plotted with geom_sf
in ggplot2. I get this message:
Erreur : Free scales are only supported with
coord_cartesian()
andcoord_flip()
Is there any option I have missed ?
Anyone has solve the problem without being forced to use cowplot
(or any other gridarrange)?
Indeed, here is an example. I would like to show the different French regions separately in facets but with their own x/y limits.
The result without scales = "free"
Scales are calculated with the extent of 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)
The result using cowplot
I need to use a list of ggplots and can then combine them. This is the targeted output. It is cleaner. But I also want a clean way to add a legend. (I know may have a common legend like in this other SO question : facet wrap distorts state maps 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)