I am processing some data that creates a lot of images (maps).
I've made it so that I can automatically generate the maps, however I have a number of problems that I can't figure out.
Each map created relates to an April.day
in the df
Here is the first 50 rows of the dataframe
dput(df_NS)
structure(list(Nestbox = c("O242", "O51", "O59", "O71", "C110",
"C116", "C120", "C13", "C132", "C134", "C139", "C140", "C29",
"C30", "C33", "C48", "C56", "C9A", "MP25", "MP67", "B30", "MP2",
"B101", "B11", "B112", "B15", "B197", "B2", "B217", "B22", "B30",
"B95", "MP21", "MP25", "MP33", "MP51", "MP56", "MP6", "MP60",
"MP61", "MP67", "MP77", "EX84", "EX92", "SW130", "O31", "O38",
"O38B", "O48", "O58"), April.day = c(-25, -22, -22, -22, -14,
-14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14,
-23, -23, -20, -20, -11, -11, -11, -11, -11, -11, -11, -11, -11,
-11, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
-10, -10, -10, -10, -10, -10), State.code = c(1L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 3L, 2L, 1L), lon = c(-1.341280663,
-1.343562025, -1.343620358, -1.340629756, -1.332551665, -1.329108814,
-1.328655294, -1.330835311, -1.330715028, -1.33052464, -1.328144549,
-1.328287425, -1.329353862, -1.329343236, -1.33041446, -1.325353001,
-1.327279282, -1.332909331, -1.300122834, -1.299148682, -1.310197641,
-1.305886812, -1.308725397, -1.309505208, -1.309235075, -1.308580716,
-1.30959055, -1.308685087, -1.309426224, -1.306562029, -1.310197641,
-1.307564253, -1.301598673, -1.300122834, -1.299510666, -1.299846899,
-1.297823339, -1.305388627, -1.297220016, -1.297398331, -1.299148682,
-1.300378324, -1.333554619, -1.338688389, -1.332015649, -1.344951753,
-1.344769267, -1.345214102, -1.342514477, -1.343145083), lat = c(51.76635545,
51.76553293, 51.76450781, 51.76428383, 51.75689245, 51.75615401,
51.75742817, 51.75637019, 51.75666667, 51.75740286, 51.7596281,
51.75976378, 51.75721637, 51.75695556, 51.75701561, 51.75871255,
51.75875955, 51.75720018, 51.76339382, 51.75986347, 51.76597134,
51.76737513, 51.76464054, 51.76481595, 51.76542577, 51.76557477,
51.76682149, 51.7644335, 51.76714421, 51.76681267, 51.76597134,
51.76571265, 51.76447255, 51.76339382, 51.76268887, 51.76062289,
51.76030512, 51.76678776, 51.75996884, 51.75968219, 51.75986347,
51.75998767, 51.76749876, 51.76822905, 51.76474771, 51.76863319,
51.76622254, 51.7655237, 51.76482531, 51.76430735)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -50L), groups = structure(list(
April.day = c(-25, -23, -22, -20, -20, -14, -14, -11, -11,
-11, -10, -10, -10), State.code = c(1L, 1L, 1L, 1L, 3L, 1L,
2L, 1L, 2L, 3L, 1L, 2L, 3L), .rows = structure(list(1L, 19:20,
2:4, 22L, 21L, c(7L, 8L, 11L, 12L, 13L, 14L, 15L, 16L,
17L), c(5L, 6L, 9L, 10L, 18L), 23:30, 32L, 31L, c(33L,
35L, 41L, 42L, 43L, 45L, 47L, 50L), c(34L, 36L, 37L,
38L, 39L, 40L, 44L, 46L, 49L), 48L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -13L), .drop = TRUE))
and this is the code that I've been working with
df_split <- df_NS %>%
ungroup() %>%
split(.$April.day)
statecol<- colorFactor(palette = "viridis", df_NS$State.code) #create the colour palette
plot_fun <- function(ix) {
x<- do.call(rbind, df_split[seq(ix)])
leaflet() %>%
setView(lng = -1.324640, lat = 51.770462, zoom = 13.25) |>
addTiles() %>%
addCircleMarkers(data = x, label = ~as.character(x$Nestbox), radius = 5, color = ~statecol(State.code), stroke = FALSE, fillOpacity = 5) %>%
addLegend('bottomright', pal = statecol, values = x$State.code,
title = 'Nest State',
opacity = 2)
}
plots <- lapply(seq_along(Nestates_NS_split), plot_fun)
plotnumbers<- length(plots)
for( i in 1:plotnumbers) {
print(plots[[i]])
}
I would like to automatically export all the images created to a folder inside the working directory, with the corresponding April.day
in the filename and then annotate the image with the April.day
in the dataframe.
Previously I would have exported each file using the export image feature in R studio but that is tedious.
Then I would annotate the images using something like this from the magick
package
install.packages("magick")
library(magick)
#first read in the image
ns0104<- image_read("C:.../Images for gifs/NS0104.png")
#annotate the image
ns0104<-image_annotate(ns0104, "4th April", size = 30, color = "black", gravity = "north", boxcolor = "white" )
# Save the annotated file
image_write(ns0104,"ns0104.png" )
Then combine the images to a GIF
list.files(path = "C:.../Images for gifs", pattern = "*.png", full.names = T) %>%
map(image_read) %>% # reads each path file
image_join() %>% # joins image
image_animate(fps=2, delay = 150) %>% # animates, can opt for number of loops
image_write("map.gif")
So as you can see to do this manually for more than 25 images would be really tedious
Any help appreciated, also open to other more efficient methods