A couple things seem unusual in your ggplot call:
argentina<- ggplot(total) +
geom_sf(col = NA, aes(fill=(gini),geometry=geometry)) +
scale_fill_discrete() +
coord_sf(datum = NA) +
theme_minimal() +
argentina
- the specification of geometry = geometry in aes is superfluous; you can rely on defaults unless you are dealing with an object with more geometry columns (rather unusual, and not the case of GADM data)
- the gini coefficient from your data frame does not look discrete (so scale_fill_continuous seems a better idea)
- the non specified datum in coord_sf call seems strange; GADM is in WGS84, so I suggest coord_sf(st_crs(4326))
- I don't quite understand your adding of object argentina on the last line of ggplot (after theme_minimal)
The full size GADM file is about 5 MB, which does not seem like much, but animating it via {gganimate} took a while on my mid range laptop, and used up about 8 gigs of RAM. The simplification mentioned by @nirgrahamuk is definitely a good idea.
I suggest the following approach, loosely based on your gini data frame - as it has only Buenos Aires data, and as {dplyr} does not support cross join it is somewhat convoluted at the beginning, but the plotting and animation parts are hopefully straightforward.
library(sf)
library(dplyr)
library(ggplot2)
library(gganimate)
# gadm file
borders <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_ARG_1_sf.rds"))
# inner join to fake data - because dplyr can't cross join,
# and gganimate needs the rows with NAs
fake_data <- data.frame(NAME_0 = "Argentina",
year = 2003:2008)
borders <- borders %>%
inner_join(fake_data, by = "NAME_0") %>%
select(NAME_1, year)
# real data - a sample...
data <- tibble::tribble(~NAME_1,~year, ~gini,
"Buenos Aires", 2003, 0.481,
"Buenos Aires", 2004, 0.460,
"Buenos Aires", 2005, 0.441,
"Buenos Aires", 2006, 0.457,
"Buenos Aires", 2007, 0.442,
"Buenos Aires", 2008, 0.425)
chrt_src <- borders %>%
left_join(data, by = c("NAME_1", "year"))
# plot the result
plot <- ggplot(data = chrt_src, aes(fill = gini)) +
scale_fill_continuous() +
geom_sf() +
coord_sf(crs = st_crs(4326)) +
theme_void() +
transition_time(year)
# animate the plot as a gif
animate(plot = plot,
fps = 15,
renderer = gifski_renderer(loop = T))
anim_save('argentina.gif')
