Hi there! I have an Argentina map with each state, that i got from GADM and converted it to sf. So i have the list of states "NAME_1" with its shape in "geometry".
library(tidyverse)
library(raster)
library(sf)
arg <- getData("GADM",country="ARG",level=1)
arg_sf<-st_as_sf(arg) %>% select(c(NAME_1,geometry))
In the other hand I have some values in a data frame that shows how fair income is distributed in each state (there are 24 states) over 12 years.
head(gini)
NAME_1 year gini
1 Buenos Aires 2003 0,481
2 Buenos Aires 2004 0,460
3 Buenos Aires 2005 0,441
4 Buenos Aires 2006 0,457
5 Buenos Aires 2007 0,442
6 Buenos Aires 2008 0,425
...
...
...
I would like to animate the map, so each state changes its colour everytime the year changes.
So I merge the values with the geometry of the state :
total <- as.tibble(merge(gini,arg_sf, by="NAME_1"))
argentina<- ggplot(total) +
geom_sf(col = NA, aes(fill=(gini),geometry=geometry)) +
scale_fill_discrete() +
coord_sf(datum = NA) +
theme_minimal() +
argentina
So then i can animate it with gganimate
anim<-argentina + transition_states(year,
transition_length = 2,
state_length = 1)
But I am pretty sure there is something wrong! It is taking it too long to animate it and the file goes so heavy that I have an error as a result that says I am out of memory.
I think that it is related to the fact that when I merge, I am copying the "geometry" of each state once for every observation, and that might make it quite heavy.
Is this the correct way to plot or animate a map?
Thank you!