How to show the date in the title in a gganimate gif?

212 Views Asked by At

I want to show the date in a map with " transition_manual " from gganimate but I don´t know how to do it. I have upload the gif map here: map with gganimate

I have uploaded the .xlm here: xml data

and my code is here:

    # para manipular dataframes
library(tidyverse)
# para importar archivos shapefiles
library(rgdal)
# Para transformar los archivos shapefiles 
library(broom)
library(ggplot2)
library(readxl)
library(gganimate)

library(sf)



setwd("C:/Users/blackmamba/Desktop/mapas/Provincias_ETRS89_30N")

carto_base <- readOGR("Provincias_ETRS89_30N.shp")
#plot(carto_base) para verificar el directorio y demas

# Para convertir el archivo shapefile en un dataframe utilizamos la función tidy()
data_provincias <- tidy(carto_base)



dfprovincias <- read_excel("incidencia.xls")
#view(dfprovincias)

#leemos los números de la columna ID como carácteres, para poder juntarlos con lef_joint al archivo que contine las id cartograficas 
dfprovincias$id <- as.character(dfprovincias$id)
dfprovincias$incidencia = as.numeric(gsub(",","\\.",dfprovincias$incidencia))


dfprovincias$fecha<-as.Date(dfprovincias$fecha, "%Y-%m-%d")
#str(dfprovincias$fecha)




dfprovincias_grafico1 <- data_provincias%>%
  left_join(dfprovincias, by= "id")





head(dfprovincias_grafico1)





colores <- RColorBrewer::brewer.pal(9,'Reds')[c(2,3,4,5,6,7,8,9)]

# En función de los resultados obtenidos establecemos nuestros cortes en los siguientes valores:
corte <- c( 0,15,30,50,80,100,150,250,860)


# Los valores mínimo y máximo son:

val_min <- min(dfprovincias_grafico1$incidencia)
val_max <- max(dfprovincias_grafico1$incidencia)


# Y por tanto, los rangos serán los siguientes:

breaks <- c(val_min, corte, val_max)

dfprovincias_grafico1$breaks <- cut(dfprovincias_grafico1$incidencia,
                                   breaks = breaks,
                                   include.lowest = T)

breaks_scale <- levels(dfprovincias_grafico1$breaks)
labels_scale <- rev(breaks_scale)





dfprovincias_grafico1 %>%
  #filter (fecha=="2020-09-25")%>%
  ggplot(aes(x=long, y= lat, group = group)) +
  geom_polygon(aes(fill=breaks), color= "white", size = 0.2) +
  labs( #title = "Tasa de INCIDENCIA/100.000 hab por provincias",
        #title ="Fecha: {as.Date.numeric(frame_along, origin = '2020-10-01')}"
        subtitle = "desde 15 de Junio al 5 de Ocutbre 2020) ",
        caption = "Fuente: ISCIII",
        fill = "incidencia por 100.000 hab") +
  theme_minimal() +
  theme(
    axis.line = element_blank(),
    axis.text = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.background = element_rect(fill = "snow", color = NA),
    panel.background = element_rect(fill= "snow", color = NA),
    plot.title = element_text(size = 16, hjust = 0),
    plot.subtitle = element_text(size = 12, hjust = 0),
    plot.caption = element_text(size = 8, hjust = 1),
    legend.title = element_text(color = "grey40", size = 13),
    legend.text = element_text(color = "grey40", size = 12, hjust = 0),
    legend.position = c(0.93, 0.3),
    plot.margin = unit(c(0.5,2,0.5,1), "cm")) +
  scale_fill_manual(
    values = rev(colores),
    breaks = rev(breaks_scale)) +
#transition_states(fecha) 
transition_manual(fecha)+
  labs(title = "Day = {frame}") 

The date var its called "fecha"

I want to slow the .gif too. It´s possible? I don´t know how to do it neither. Thank`s you all in advance fellas!

0

There are 0 best solutions below