I'm a student trying to learn some of r for my biological analysis, but I don't really know coding. I'm currently working on a function that recieves as input an bird species name and gives as output a map of distribution for the species. I'm trying to modify my function so it could also work with a list or column of species name and save all the different maps but still can't figure out how to do it. Thanks for the help. Here´s my function
species.distrib.map <- function(species_name, distribution_color = "#f16d3d", file_type = ".pdf") {
spec_pol<- subset(birds_maps, Species == species_name)
spec_sf <- st_as_sf(spec_pol)
spec_map<- ggplot() +
geom_sf(data = Depart_sf %>%
filter(!DEPTO == "SAN ANDRES,PROVIDENCIA Y SANTA CATALINA"),
fill = "transparent", color = "darkgray", linewidth = 0.1) +
geom_sf(data = spec_sf, fill = distribution_color, alpha = 0.5, color = NA) +
geom_sf(data = Colb_sf, fill = "transparent", color = "darkgray", linewidth = 0.3) +
geom_point(data= CIAT, aes(x = Longitud, y = Latitud), color = "#265f66", size = 1) +
geom_rect(data = rect_df_colb, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), color = "black", fill = NA, linewidth= 0.57) +
theme_void()
ggsave(filename = str_c("D:/R/birdmaps/Species maps/", species_name, file_type), plot = spec_map, width = 4.91, height = 8, units = "in", dpi = 700)
return(print(spec_map))
}
I tried to use "for" like this, without results:
pecies.distrib.map <- function(species_names, distribution_color = "#f16d3d", file_type = ".pdf") {
for (species_name in species_names) {
spec_pol <- subset(birds_maps, Species == species_name)
spec_sf <- st_as_sf(spec_pol)
spec_map<- ggplot() +
geom_sf(data = Depart_sf %>%
filter(!DEPTO == "SAN ANDRES,PROVIDENCIA Y SANTA CATALINA"),
fill = "transparent", color = "darkgray", linewidth = 0.1) +
geom_sf(data = spec_sf, fill = distribution_color, alpha = 0.5, color = NA) +
geom_sf(data = Colb_sf, fill = "transparent", color = "darkgray", linewidth = 0.3) +
geom_point(data= CIAT, aes(x = Longitud, y = Latitud), color = "#265f66", size = 1) +
geom_rect(data = rect_df_colb, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), color = "black", fill = NA, linewidth= 0.57) +
theme_void()
filename <- paste0("D:/R/birdmaps/Species maps/", species_name, file_type)
print(paste("Saving map for species:", species_name, "to file:", filename))
ggsave(filename = filename, plot = spec_map, width = 4.91, height = 8, units = "in", dpi = 700)
print(spec_map)
}
}