I have a dataset with watersheds across 3 provinces. Each watershed has a corresponding shapefile, and score from 0 to 1 for ecosystem disruption under 3 rcp (climate projection models). Here is the head of my dataframe:
ws rcp ecodisrup geometry
ANNAPOLIS_sp rcp26 0.09090909 MULTIPOLYGON (((-64.86239 4...
BARRINGTON_C rcp26 0.00000000 MULTIPOLYGON (((-65.4722 43...
CHETICAMP_RI rcp26 0.09090909 MULTIPOLYGON (((-60.59559 4...
CLAM_HRB_ST. rcp26 0.27272727 MULTIPOLYGON (((-61.38909 4...
COUNTRY_HARB rcp26 0.09090909 MULTIPOLYGON (((-61.96444 4...
EAST_INDIAN_ rcp26 0.09090909 MULTIPOLYGON (((-63.94016 4...
The watersheds and their shapefiles repeat for each rcp (26, 45, 85), eg:
ws rcp ecodisrup geometry
ANNAPOLIS_sp rcp26 0.09090909 MULTIPOLYGON (((-64.86239 4...
... rcp26 0.00000000 MULTIPOLYGON (((-65.4722 43...
46th ws rcp26 0.09090909 MULTIPOLYGON (((-60.59559 4...
ANNAPOLIS_sp rcp45 0.27272727 MULTIPOLYGON (((-64.86239 4...
... rcp45 0.09090909 MULTIPOLYGON (((-65.4722 43...
46th ws rcp45 0.09090909 MULTIPOLYGON (((-60.59559 4...
ANNAPOLIS_sp rcp85 0.09090909 MULTIPOLYGON (((-64.86239 4...
... rcp85 0.00000000 MULTIPOLYGON (((-65.4722 43...
46th ws rcp85 0.09090909 MULTIPOLYGON (((-60.59559 4...
I'd like to map the watersheds, coloured by ecodisrup, under each of the three rcp scenarios by writing a for loop. The problem I run into is including a ggsave function in the for loop, and also the loop I'm writing is just producing the same map three times.
Later on in my analysis I'll need the same type of for loop for mapping, but I'll add in a species column (produce a map coloured by ecodisrup for each species across all watersheds, under 3 rcp scenarios).
Here is the code I've tried before, expected an output of three maps (one for each rcp). Instead I got three maps, but they all had the exact same colouring (not sure if they just coloured by the last one? rcp85?).
# make empty list to fill in with plots
ecodis_rcp_plots = list()
# define the different rcps
ecodis_rcp = unique(ecodis$rcp)
# begin for loop
for (rcp in ecodis_rcp){
ecodis_rcp_plots[[rcp]] = ggplot(data = ecodis, aes(geometry = geometry)) +
geom_sf(aes(fill = ecodisrup)) +
scale_fill_viridis_c(option = "viridis", limits = c(0, 1))
ggsave(paste("C:/Users/myname/Desktop/EcoDis", rcp, ".png"),ecodis_rcp_plots[[rcp]],width=8,height=8,units="in",dpi=300)
}
Any insight would be greatly appreciated, thank you!