Raul
Raul

Reputation: 139

How to slow down a gganimate map gif?

hello fellas I am trying to animate a covid map from Spain and the gif run very quickly so I want to slow down a bit for make it more readable.

this is my gif gif from spain covid map

and this is my code. If you need the dataframe I can upload.

and this is my code. I want to know how to slow down the gif. I have tried with "animate" options but don´t work for tbl_df objects

   # 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/..../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 final.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-08-20")%>%
  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 = "20 Agosto 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) #esta es muy lenta su puta madre
transition_manual(fecha) +
# labs(title = "Day = {frame}") 
 labs(title = 'Fecha: {current_frame}')

thank you so much in advance!

Upvotes: 0

Views: 1040

Answers (1)

Raul
Raul

Reputation: 139

with this works!

dfprovincias_grafico1 <- ggplot(data = dfprovincias_grafico1, 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 = "20 Agosto 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) #esta es muy lenta su puta madre
  transition_manual(fecha) +
  
  labs(title = 'Fecha: {current_frame}') 

#dfprovincias_grafico1 <- animate(dfprovincias_grafico1) #works 


animate(dfprovincias_grafico1, nframes = 500, fps=21)

Upvotes: 1

Related Questions