r - 难以渲染 gganimate 地图
问题描述
我是新手gganimate
。很棒的包裹。
我能够重现包示例,但我正在努力呈现我的实际用例。我想知道是否有任何gganimate
用户愿意确定是否有更有效的方法来运行此代码。我已经在本地机器和 RStudio Cloud 上尝试过。我还尝试绘制每周数据而不是每日数据(因此将整体数据减少了 6/7。
# load packages
library(tidyverse)
library(sf)
library(viridis)
library("rio")
# get county geometry
url <- "https://gist.githubusercontent.com/ericpgreen/717596c37478ef894c14b250477fae92/raw/c2cf4b273a2c7f0677f22a37b5e9f7e893204e3b/cali.R"
cali <- rio::import(url)
# get covid data
covid <- read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv", stringsAsFactors = FALSE)
# prep covid data
covidPrepped <-
covid %>%
filter(state=="California") %>%
select(date, fips, cases, deaths) %>%
mutate(date = lubridate::ymd(date)) %>%
mutate(fips = stringr::str_pad(fips, width=5, pad="0"))
# make sure every county has a row for every day
complete <-
cali %>%
left_join(covidPrepped, by = c("GEOID" = "fips")) %>%
complete(GEOID, date, fill = list(cases = 0)) %>%
select(date, GEOID, cases)
# join back to geometry and construct casesPop
pData <-
complete %>%
left_join(select(cali, GEOID, NAME, estimate, geometry),
by = "GEOID") %>%
st_as_sf() %>%
mutate(casesPop = (cases/estimate)*100000) %>%
mutate(casesPop = ifelse(is.na(casesPop), 0, casesPop)) %>%
mutate(group = cut(casesPop,
breaks = c(0, 1, 3, 10, 30, 100,
300, 1000, 3000, 10000,
Inf),
labels = c(0, 1, 3, 10, 30, 100,
300, 1000, 3000, 10000),
include.lowest = TRUE)
) %>%
mutate(month = lubridate::month(date,
label=TRUE,
abbr=TRUE),
day = lubridate::day(date),
monthDay = paste(month, day, sep=" ")) %>%
select(GEOID, geometry, group, monthDay)
# animate
ggplot(pData) +
geom_sf(aes(fill = group), color = "white", size=.1) +
scale_fill_viridis_d(option = "magma", drop=FALSE) +
coord_sf(crs = 102003) +
# on RStudio Cloud I got a CRS error with the line above
# switching to the line below works
#coord_sf(crs = "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80", datum=NA) +
theme_minimal() +
theme(legend.position = "top",
legend.box = "horizontal",
legend.title = element_blank(),
legend.justification='left') +
guides(fill = guide_legend(nrow = 1)) +
# gganimate portion
transition_states(monthDay,
transition_length = 4,
state_length = 1) +
ease_aes('cubic-in-out')
解决方案
推荐阅读
- google-apps-script - 如何在自动电子邮件中显示百分比值?
- silverstripe - SS4 上的垃圾邮件防护/Recaptcha
- database - 使用聚合管道对新计算的数组字段进行排序和连接
- excel - 根据标题从列中删除单元格范围
- javascript - 如何使按钮自动单击它自己
- c# - 登录时.NET Core 数据库操作失败
- java - java.lang.NoSuchMethodError 在运行 junit 测试用例时使用 org.json.JSONTokener 进行模式验证失败
- python - 如何为 pandas.tseries.holiday.USFederalHolidayCalendar 重新创建日历规则?
- zoho - 从 Zoho APIv1 升级到 APIV2 -> 请求不保存数据
- reactjs - Gatsby:为什么本地服务器与生产构建的站点呈现不同?