【发布时间】:2020-07-23 23:47:55
【问题描述】:
我正在尝试使用我所在国家/地区的 COVID 数据制作 COVID 动画。 但是我一直弄错了,而且大多数问题我都不知道如何解决问题。
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)
首先,您可以从这里获取数据框:
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>
巴西地图也有:
Estados <- read_state(year=2018)
到目前为止,我一直在通过汇总数据来绘制图表,如下所示:
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)
其中“ontem”df 是 covid 的最后一天状态的数据框(caso 的子集):
ontem <- caso %>% filter(date == Sys.Date()-1,place_type == 'state')
但我想制作死亡人数(例如)每天增加的动画,我尝试使用相同的代码加上 transition_time(date) 之类的东西,但我不断收到警告/错误消息。
有人可以帮我解决这个问题吗?我被困了好几天!
【问题讨论】:
-
我无权访问数据,您可以使用
dput()粘贴其中的一部分。此外,在您的问题中提及所有包和自己的功能。 -
您好,马上查看。我没有使用我自己的任何功能,只是 ggplot
-
很抱歉,但遗漏了很多。
ontem在哪里?请在发布之前检查所有提供的代码的可重复性。你的 gganimate 代码在哪里? -
当然。感谢您的建议。
-
我很确定我们不必安装所有提供的软件包来完成这项工作。请修改。我不会安装所有这些。