问题描述
您好,我正在尝试制作西班牙的covid地图的动画,并且gif运行非常快,所以我想放慢一点,使其更具可读性。
这是我的代码。 我想知道如何降低gif的速度。我尝试使用“动画”选项,但不适用于tbl_df对象
# 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",plot.title = element_text(size = 16,hjust = 0),plot.subtitle = element_text(size = 12,plot.caption = element_text(size = 8,hjust = 1),legend.title = element_text(color = "grey40",size = 13),legend.text = element_text(color = "grey40",size = 12,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}')
非常感谢您!
解决方法
这有效!
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",plot.title = element_text(size = 16,hjust = 0),plot.subtitle = element_text(size = 12,plot.caption = element_text(size = 8,hjust = 1),legend.title = element_text(color = "grey40",size = 13),legend.text = element_text(color = "grey40",size = 12,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)