1

I'm trying to build a single plot that is a series of maps displaying a number of variables.

I've played with facet_grid() which gets me close-ish to where I'm getting, but it does not allow for each plot to have its own color ramp. I've also been working with ggarrange() and plot_grid(), which are working OK, but I can't get facet-styled labels.

Quick reprex to make data that resembles what I'm working with, apologies for downloading packages to get maps...

data("USArrests")
install.packages("devtools")
library(devtools)
devtools::install_github("UrbanInstitute/urbnmapr")
library(urbnmapr)

states_sf <- get_urbn_map("states", sf = T)
USArrests <- USArrests %>% as_tibble(rownames = "state")

Double checking that this dataset works...

ggplot(states_sf %>% left_join(USArrests, by = c("state_name" = "state"))) + geom_sf(aes(fill = Murder))
ggplot(states_sf %>% left_join(USArrests, by = c("state_name" = "state"))) + geom_sf(aes(fill = Assault))

Great. OK.

Step 2 of playing with dataset to make a reprex.

USArrests.long1 <- USArrests %>% pivot_longer(cols = Murder:Rape, names_to = "crime", values_to = "value") %>% mutate(scen = "baseline")
USArrests.long2 <- USArrests.long1 %>% mutate(value = value * 0.5) %>% mutate(scen = "lower")

USArrests.long <- bind_rows(list(USArrests.long1, USArrests.long2) %>% set_names("baseline", "cut"), .id = "scen") %>% filter(!is.na(value))

Here's what facet_grid() gives me

ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state"))) + geom_sf(aes(fill = value)) + facet_grid(scen~crime)

maps with facet_grid()

I like the labels but I want each map to have it's own color ramp. My real version doesn't have random NA facets.

I sort of like ggarrange(), but I don't like the ggarrange labels. I'd like them to be arranged in a grid similar to how facet_grid does it (and ideally aesthetically too).


p1 <- ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state")) %>% 
               filter(crime == "Assault" & scen == "baseline")) + geom_sf(aes(fill = value))
p2 <- ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state")) %>% 
               filter(crime == "Murder" & scen == "baseline")) + geom_sf(aes(fill = value))
p3 <- ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state")) %>% 
               filter(crime == "Rape" & scen == "baseline")) + geom_sf(aes(fill = value))
p4 <- ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state")) %>% 
               filter(crime == "UrbanPop" & scen == "baseline")) + geom_sf(aes(fill = value))
p5 <- ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state")) %>% 
               filter(crime == "Assault" & scen == "cut")) + geom_sf(aes(fill = value))
p6 <- ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state")) %>% 
               filter(crime == "Murder" & scen == "cut")) + geom_sf(aes(fill = value))
p7 <- ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state")) %>% 
               filter(crime == "Rape" & scen == "cut")) + geom_sf(aes(fill = value))
p8 <- ggplot(states_sf %>% left_join(USArrests.long, by = c("state_name" = "state")) %>% 
               filter(crime == "UrbanPop" & scen == "cut")) + geom_sf(aes(fill = value))

ggarrange(p1, p2, p3, p4, p5, p6, p7, p8, ncol = 2, nrow = 4, labels = c("assault baseline", "murder baseline", "rape baseline", "urbanpop baseline", "assault cut", "murder cut", "rape cut", "urbanpop cut"))

maps with ggarrange()

My question is: is there a way to get the label aesthetics and the 2 variable setup in an xy grid with panels like facet_grid(), but get separate color ramps and legends for each panel like ggarrange()?

Thanks!

LightonGlass
  • 176
  • 1
  • 13

0 Answers0