ggplot: :geom_sf(). Separate fill palette for each facet

Hi,

I am interested to know whether it is possible to have a separate fill palette for each facet when using geom_sf(). I thought about referencing colour palettes within a column, but can't seem to get that to work.

I have been following this very helpful example.

library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
library(cowplot)
library(RColorBrewer)

nc <- st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/4.0/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> geographic CRS: NAD27

nc2 <- nc %>% 
  select(id=NAME, BIR74, SID74, BIR79, SID79) %>%
  pivot_longer(cols = BIR74:SID79) %>% 
  st_as_sf()
#> Warning in val_cols[col_id] <- unname(as.list(data[cols])): number of items to
#> replace is not a multiple of replacement length

#Fill with one overall legend - would be better with a separate legend per facet.
nc2 %>%
  ggplot() +
  geom_sf(aes(fill=value)) +
  facet_wrap(~name) +
  scale_fill_viridis_c() +
  theme_linedraw()

#Now with a separate legend for each facet
nc2 %>%
    split(.$name) %>%
    map(~ ggplot(., aes(fill = value)) +
            geom_sf() +
            facet_wrap(~name) +
            scale_fill_viridis_c() +
            theme_linedraw()) %>%
    cowplot::plot_grid(plotlist = .)

#But could we get a different colour palette for each facet?
nc2 %>%
  mutate(fill_pal = case_when(
    name=="BIR74" ~ "BuGn",
    name=="SID74" ~ "GnBu",
    name=="BIR79" ~ "BuPu",
    name=="SID79" ~ "OrRd"
  )) %>%
    split(.$name) %>%
    map(~ ggplot(., aes(fill = value)) +
            geom_sf() +
            facet_wrap(~name) +
            scale_fill_brewer(palette = {{fill_pal}}) + #not working
            theme_linedraw()) %>%
    cowplot::plot_grid(plotlist = .)
#> Error in pal_name(palette, type): object 'fill_pal' not found

Very grateful for any suggestions to achieve this.

Thanks,

Peter

Created on 2021-06-07 by the reprex package (v0.3.0)

I have done this in two ways. 1) made four different layers and plotted together with ggdraw. 2) made one "super palette" and adjusted the ID column. Basically concatenate your facet ID with the color ID into a new column, and then concatenate the different palettes together. The only custom thing you would need after this is a manual fill legend for each facet using the original palettes.