I'm trying to plot two maps side by side using sf::plot and I can't manage to get it to work. There is two problems, the first one is that the plots are made on top of each other instead of side by side and the second is that I lose the legend.
Here is a example and more explanations.
library(sf)
library(dplyr)
# preparing the shapefile
nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>%
select(AREA, PERIMETER) %>%
mutate(AREA = as.factor(AREA<median(AREA)))
If I plot every field independently:
plot(nc[,1])
plot(nc[,2])
Both images are nice, with a legend and all, but I want both on the same panel. sf::plot offers this feature built in as explained in https://r-spatial.github.io/sf/articles/sf5.html#geometry-with-attributes-sf:
plot(nc)
I lose the legend and they are on top of each other instead of side by side. In ?plot you can read:
For more control over individual maps, set parameter mfrow with par prior to plotting, and plot single maps one by one.
But when I do, it doesn't work:
par(mfrow=c(1,2))
plot(nc[,1])
plot(nc[,2])
par(mfrow=c(1,1))
Any idea how to plot 2 maps side by side with sf?



