Reputation: 77
I'm trying to make an COVID animation using the COVID data from my country. But i keep getting it wrong, and most of the issues i have no idea of how can i solve the problem.
libraries:
library(ggplot2)
library(tidyverse)
library(dplyr)
library(hrbrthemes)
library(rgdal)
library(raster)
library(ggmap)
library(tmap)
require(sp)
library(geobr)
library(readr)
library(gganimate)
library(gifski)
First of all, you can get the dataframe from here:
caso <- readr::read_csv("https://data.brasil.io/dataset/covid19/caso.csv.gz")
caso$date <- as.Date(caso$date)
caso$state <- as.factor(caso$state)
tibble [399,497 x 12] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ date : Date[1:399497], format: "2020-07-22" "2020-07-21" "2020-07-20" ...
$ state : Factor w/ 27 levels "AC","AL","AM",..: 4 4 4 4 4 4 4 4 4 4 ...
$ city : chr [1:399497] NA NA NA NA ...
$ place_type : chr [1:399497] "state" "state" "state" "state" ...
$ confirmed : num [1:399497] 34660 34405 34145 33705 33585 ...
$ deaths : num [1:399497] 544 533 515 507 505 499 493 488 483 478 ...
$ order_for_place : num [1:399497] 124 123 122 121 120 119 118 117 116 115 ...
$ is_last : logi [1:399497] TRUE FALSE FALSE FALSE FALSE FALSE ...
$ estimated_population_2019 : num [1:399497] 845731 845731 845731 845731 845731 ...
$ city_ibge_code : num [1:399497] 16 16 16 16 16 16 16 16 16 16 ...
$ confirmed_per_100k_inhabitants: num [1:399497] 4098 4068 4037 3985 3971 ...
$ death_rate : num [1:399497] 0.0157 0.0155 0.0151 0.015 0.015 0.0149 0.0149 0.01
> head(caso)
# A tibble: 6 x 12
date state city place_type confirmed deaths order_for_place is_last estimated_popul~
<date> <fct> <chr> <chr> <dbl> <dbl> <dbl> <lgl> <dbl>
1 2020-07-22 AP NA state 34660 544 124 TRUE 845731
2 2020-07-21 AP NA state 34405 533 123 FALSE 845731
3 2020-07-20 AP NA state 34145 515 122 FALSE 845731
4 2020-07-19 AP NA state 33705 507 121 FALSE 845731
5 2020-07-18 AP NA state 33585 505 120 FALSE 845731
6 2020-07-17 AP NA state 33436 499 119 FALSE 845731
# ... with 3 more variables: city_ibge_code <dbl>, confirmed_per_100k_inhabitants <dbl>
The brazil map is also available:
Estados <- read_state(year=2018)
So far, i've been doing plots by summarizing the data,like this:
ggplot() +
geom_sf(data=ontem, aes(fill=deaths), color="#FEBF57", size=.15, show.legend = TRUE) +
labs(title = "Mortes por COVID",size=8) +
scale_fill_distiller(palette = "BrBG",
name= "Mortes Confirmadas", limits=c(min(ontem$deaths),max(ontem$deaths)))+
theme_void() + theme(plot.title = element_text(hjust = 0.5))
options(scipen=10000)
Where "ontem" df is a dataframe of the last day status of the covid (subset of caso):
ontem <- caso %>% filter(date == Sys.Date()-1,place_type == 'state')
But i would like to make an animation of how the deaths (for example) increase each day, i tried to use something like the same code plus transition_time(date) but i keep getting warning/error messages.
Can someone help me with this? I'm stuck for days!
Upvotes: 0
Views: 228
Reputation: 21
Ben is right, but the beauty of transition_time() is to use {frame_time} to see the specific time, usually I do something like this that I learned from someone else:
scale_x_time(name = "Time", labels = label_time(format = "%H"),
breaks = as_hms(c("00:00:00","01:00:00","02:00:00","03:00:00",
"04:00:00","05:00:00","06:00:00","07:00:00","08:00:00","09:00:00",
"10:00:00","11:00:00","12:00:00","13:00:00","14:00:00","15:00:00",
"16:00:00","17:00:00","18:00:00","19:00:00","20:00:00",
"21:00:00","22:00:00","23:00:00","24:00:00")))
Upvotes: 1
Reputation: 1154
The transition_time()
function requires a vector to be in a date or time format. So, you must either ensure that your time variable is in a format that gganimate
likes (it is pretty finicky with date formats) OR you could compute an integer that tracks sequence of time (1, 2, 3, 4...) after sorting by date/time, and using transition_states()
with the sequence vector. The latter approach, I've found, is a lot easier.
Upvotes: 1