I'm trying to map out all these different "solutions" from the prioritizr model using ggplot. (To preface I'm very new to R so perhaps my code/methods are extremely rudimentary and why I'm also not able to decipher the errors completely). What I was doing before was copying and pasting the code to make a ggplot map for each solution (named s1, s2, s3 etc..). It was very time-consuming - (I think due to the size of the datasets I'm loading). I don't mind if it takes even longer to display all 10, but id rather automate it and let it run all night (like in a for loop or function) as opposed to hanging around my computer all day and ensuring that the plot is done and saved, and then having to click ctrl + enter to run the next plot.
Option 1: Putting the solutions into a list. and I'm running through the list in a for loop to create the map:
solutions <- list("s1" = s1, "s2" = s2, "s3" = s3, "s4" = s4, "s5" = s5, "s10" = s10, "s11" = s11, "s12" = s12)
for (s in 1:length(solutions)) {
ggplot() +
geom_sf(data = range, size = 1, color = "black", fill = "NA") +
geom_sf(data = fire, size = 0.25, color = "darkred",
fill = "darkred", show.legend = 'poly') +
geom_sf(data = poly, size = 0.25, fill = "darkorange", color = "darkorange") +
geom_sf(data = IFL, size = 0.25, fill = 'darkseagreen', alpha = 0.7, color = "darkseagreen") +
geom_sf(data = protected_areas, size = 0.5, alpha = 0.4, fill = "forestgreen", color = "forestgreen") +
geom_sf(data = s[, "solution_1"], aes(fill = solution_1)) + # this is the data from the solution in Prioritizr. It essentially adds a column called solution_1 and if a planning unit is selected for restoration it will be populated with a value of 1.
geom_sf(data = linear, size = 0.5, color = "darkgoldenrod1") +
ggtitle('Test solution') +
scale_fill_gradient(low = NA, high = "blue") +
theme_void() +
sys.sleep(2)
}
Option 2 join the columns with cbind()
and turn the whole thing around with t()
. From a solution I found here.
solutions <- t(cbind("s1" = s1, "s2" = s2, "s3" = s3, "s4" = s4, "s5" = s5, "s10" = s10,
"s11" = s11, "s12" = s12))
for( s in 1:length(solutions)) {
ggplot() +
geom_sf(data = range, size = 1, color = "black", fill = "NA") +
geom_sf(data = fire, size = 0.25, color = "darkred",
fill = "darkred", show.legend = 'poly') +
geom_sf(data = poly, size = 0.25, fill = "darkorange", color = "darkorange") +
geom_sf(data = IFL, size = 0.25, fill = 'darkseagreen', alpha = 0.7, color = "darkseagreen") +
geom_sf(data = protected_areas, size = 0.5, alpha = 0.4, fill = "forestgreen", color = "forestgreen") +
geom_sf(data = s[, "solution_1"], aes(fill = solution_1)) +
geom_sf(data = linear, size = 0.5, color = "darkgoldenrod1") +
ggtitle('Test solution') +
scale_fill_gradient(low = NA, high = "blue") +
theme_void() +
ggsave(s, path = path)
Sys.sleep(2)
}
Option 3 : Using lapply()
because it looks like it's more efficient?
make_map <- function(s){
ggplot() +
geom_sf(data = range, size = 1, color = "black", fill = "NA") +
geom_sf(data = fire, size = 0.25, color = "darkred",
fill = "darkred", show.legend = 'poly') +
geom_sf(data = poly, size = 0.25, fill = "darkorange", color = "darkorange") +
geom_sf(data = IFL, size = 0.25, fill = 'darkseagreen', alpha = 0.7, color = "darkseagreen") +
geom_sf(data = protected_areas, size = 0.5, alpha = 0.4, fill = "forestgreen", color = "forestgreen") +
geom_sf(data = s[, "solution_1"], aes(fill = solution_1)) +
geom_sf(data = linear, size = 0.5, color = "darkgoldenrod1") +
ggtitle('Test solution') +
scale_fill_gradient(low = NA, high = "blue") +
theme_void() +
ggsave(s, path = path)
}
lapply(solutions, make_map)
Essentially, in all the different options I've laid out below, I get the same error:
"Error in s[, "solution_1"] : incorrect number of dimensions"
However, if I plot each solution (i.e.
geom_sf(data = s1[, "solution_1"], aes(fill = solution_1)) + # this is the data from the solution in Prioritizr. It essentially adds a column called solution_1 and if a planning unit is selected for restoration it will be populated with a value of 1.
I don't get the error. Essentially, "solution_1" is a column in each dataframe that contains whether the unit has been selected or not by the model.
Any suggestions as to how I can rectify this?
Adding what one of the dataframes that I'm trying to plot in my loop looks like.
Thank you in advance for your help!