# get datastates <- tigris::states(progress_bar=FALSE) %>%filter(., STUSPS %in%c("WA", "OR", "ID")) %>%st_transform(crs =st_crs(fire.haz))# method 1# first dataset in ggplot() and use inherit.aes=FALSEp2 <-ggplot(data = fire.haz_df, aes(x=x, y=y, fill = WHP_ID)) +geom_raster() +geom_sf(data = states, fill=NA, color="red", inherit.aes =FALSE) +coord_sf(default_crs =crs(fire.haz))# method 2# first dataset in geom so inherit.aes isn't necessaryp3 <-ggplot() +geom_raster(data = fire.haz_df, aes(x=x, y=y, fill = WHP_ID)) +geom_sf(data = states, fill=NA, color="red") +coord_sf(default_crs =crs(fire.haz))
Layering rasters and vectors
Complicated layouts with patchwork
Subplots
p2+p3
Combine legends
p2+p3 +plot_layout(guides="collect")
Change layout
p+p3 +plot_layout(nrow=2)
Map insets
# get data (filter to continuous US)conus <- tigris::states() %>%filter(!(DIVISION ==0| STUSPS %in%c("HI", "AK"))) %>%st_transform(st_crs(fire.haz))# create inset mapinset <-ggplot(data = conus) +geom_sf(fill="white") +# fill study area states by filtering to those states and setting color parametersgeom_sf(data =filter(conus, STUSPS %in%c("ID", "OR", "WA")),fill ="gray70", color="red") +theme_void() +theme(panel.background =element_rect(fill="white", color="black")) +coord_sf()
Map insets
layout <-c( patchwork::area(t =1, l =1, b =5, r =4), patchwork::area(t =1, l =4, b =2, r =5), patchwork::area(t =3, l =4, b =5, r =5))p2 + inset +guide_area() +plot_layout(design=layout, guides='collect')