I am trying to plot maps for three different dependent variables (each plotted separately), over multiple metro areas. To do this, I am trying to do 2 nested maps. I start by doing it just over the 3 different variables foe one metro area - defining the function and running it through map and it works. The code for this part is below:
plot_fun <- function(x){
base_plot_2015$`14460` +
data_2015 %>% subset(met2013 == 14460) %>% subset(onetsoccode == "151021") %>%
geom_sf(mapping = aes_string(fill = x)) +
scale_fill_viridis_c(option = "plasma")
}
expl <- names(data_2015)[17:19]
expl <- set_names(expl)
plot_Boston <- map(expl, ~plot_fun(.x))
This gives me a list of 3 maps and so far so good. (Please note that the base_plot is a list containing maps of each of the metro areas saved as the metro area id, which is stored as double).
Next, I want to do the same, but also iterate it over different metro codes. I try doing this by defining the following function:
plot_fun <- function(x,y){
base_plot_2015$`y` +
data_2015 %>% subset(met2013 == y) %>% subset(onetsoccode == "151021") %>%
geom_sf(mapping = aes_string(fill = x)) +
scale_fill_viridis_c(option = "plasma")
}
The idea is to map it over a vector of metro areas (y) for each dependent variable to be plotted(x). However, before I do that, I can't get this function to work. When I run this function using plot_fun("work_home", 14460), I get the following error:
Error in base_plot_2015$y + data_2015 %>% subset(met2013 == y) %>% subset(onetsoccode == : non-numeric argument to binary operator
Would someone be kind enough to point out what I am doing wrong here?
Also, to do the nested map, does the code map(met, ~map(expl, ~plot_fun, y=.x)) (where met is a vector of metro area codes) make sense?