Create an animation of your tracks.
How to create a map an animation using gganimate
Load and/or install the following packages:
library(sula)
Example<-GPS_01
Mark a day zero to begging the animation, and the sequence to use
first(Example$DateGMT)
[1] "02/11/2017"
Example$date_diff <- as.Date(as.character(Example$tStamp),
format="%Y-%m-%d")-
as.Date(as.character('2017-11-02'),
format="%Y-%m-%d")
Create number sequence for animation
Example<-Example %>%
group_by(IDs) %>%
mutate(Secuencia = row_number())
For creating maps, the package sf is to be called.
Now load your data, here I am giving my directory, you should use the one on your computer.
To dowload the shapefile from Chile you can use this this link, this shapefile comes from the DIVA-GIS
Reading layer `CHL_adm1' from data source
`C:\Users\lerma\OneDrive\Documents\03Academico\02Proyectos-Postdoc\2024\1Programming\1Distill\Distill\_posts\2023-06-01-gganimate\CHL_adm1.shp'
using driver `ESRI Shapefile'
Simple feature collection with 16 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -109.455 ymin: -55.98403 xmax: -66.41472 ymax: -17.50755
Geodetic CRS: WGS 84
Plot the area of interest
My_map<-My_map+
theme_bw()+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = '#edf2f4'),
axis.text.x = element_text(size=12,color = "black"),
axis.text.y = element_text(size=12,color = "black"),
axis.ticks.x = element_line(color = "black"),
axis.ticks.y = element_line(color = "black"),
title = element_text(colour = "black"),
legend.position = "none")+
scale_x_continuous(labels = function(x) paste0(x, '\u00B0', "W")) +
scale_y_continuous(labels = function(x) paste0(x, '\u00B0', "N"))
My_map
To animate this plot, we will use the package gganimate
Because animations might take some time, I like to use the package beepr to let me know when the animation is ready
library(beepr)
transition_reveal leaves the track on the back ground so you can keep record where it was
My_animation<-ggplot(data=Example) +
geom_sf(data = Country,colour = "#edf2f4", fill = "#2b2d42",)+
geom_point(data=Example,aes(x = Longitude, y=Latitude, colour = tStamp))+
geom_path(data=Example,aes(x = Longitude, y=Latitude))+
coord_sf(xlim = c(-110,-109), ylim = c(-27.5,-26.5))+
theme_bw()+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = '#edf2f4'),
axis.text.x = element_text(size=12,color = "black"),
axis.text.y = element_text(size=12,color = "black"),
axis.ticks.x = element_line(color = "black"),
axis.ticks.y = element_line(color = "black"),
title = element_text(colour = "black"),
legend.position = "none")+
scale_x_continuous(labels = function(x) paste0(x, '\u00B0', "W")) +
scale_y_continuous(labels = function(x) paste0(x, '\u00B0', "N")) +
transition_reveal(along=tStamp) +
shadow_trail(distance = 0.01,
alpha = 0.5,
shape = 1)
My_animation
Other arguments are:
transition_time use the information for the transition
is labs shows the title with time so you can use as a
reference
shadow_wake is the wake behind the points read
more here
The package animation allows to save the animation as a file
To export, you can save the animation as gif
Or transform from gif to mp4 using the package av
av::av_encode_video("animation.gif",
framerate = 5 ,
output = 'animation.mp4')
beep(sound=1,expr=NULL)