r - gganimate::transition_time 导致飞行多边形
问题描述
尝试创建具有简单特征的动画,本质上是等值线图形式的空间变量的时间序列。
问题是多边形在动画时飞过整个情节。
这是一个可重现的示例,部分摘自https://www.blog.cultureofinsight.com/2017/09/animated-choropleth-maps-in-r/(它使用了旧版本的gganimate
.
加载包:
library(tidyverse) # dev ggplot version required: devtools::install_github("hadley/ggplot2")
library(sf)
library(readxl)
library(httr)
library(gganimate)
获取和组织可重现示例的数据。
# download the natural earth shapefile we need into your working directory
URL <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_map_units.zip"
temp <- tempfile()
download.file(URL, temp)
unzip(temp)
unlink(temp)
# read in shapefile as an sf object and set the projection
# this will be our base world map for plot sans Antarctica
world <- st_read("ne_110m_admin_0_map_units.shp") %>%
st_transform(crs = "+proj=longlat +datum=WGS84") %>%
filter(!NAME %in% c("Fr. S. Antarctic Lands", "Antarctica"))
# download dataset into your working directory
url <- "https://www.blog.cultureofinsight.com/data/wc.xlsx"
GET(url, write_disk("wc.xlsx", overwrite=TRUE))
# read in our the massive 20 rows of data and get the winner/runner-up variable in 1 column #tidyafdata
# setting factor for winner to show first in the legend
winners <- read_excel("wc.xlsx") %>%
gather(w_l, country, winner:runner_up) %>%
mutate(w_l = factor(w_l, levels = c("winner", "runner_up")))
# merge our world shape file with our main dataset
# this will add the polygon for the appropriate country to each row of our winners dataset
# and remove any countries that haven't won or come 2nd in the WC
wc_geo <- left_join(world, winners, by = c("NAME" = "country")) %>%
st_as_sf() %>%
drop_na(Year)
创建 gganimate 对象:
wc_map <- ggplot() +
geom_sf(data = world, colour = "#ffffff20", fill = "#2d2d2d60", size = .5) +
geom_sf(data = wc_geo, aes(fill = w_l)) +
coord_sf(crs = st_crs(world), datum = NA) +
scale_fill_manual(values = c("#D9A441", "#A8A8A8"), name = NULL, labels = c("Winner", "Runner-Up")) +
theme(legend.position = c(0.9, 1.01), legend.direction = "horizontal", axis.text = element_blank(),
panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
transition_time(Year)
动画:
animate(wc_map)
这是结果:
解决方案
这是一个分组问题。你需要对你的小组明确。在这种情况下,创建一个Year
和w_l
您可以使用该软件包rnaturalearth
更方便地获取世界/国家地图。
library(tidyverse)
library(gganimate)
world <- rnaturalearth::ne_countries( returnclass = 'sf') %>% filter(admin!= 'Antarctica')
url <- "https://www.blog.cultureofinsight.com/data/wc.xlsx"
httr::GET(url, httr::write_disk("wc.xlsx", overwrite=TRUE))
winners <- readxl::read_excel("wc.xlsx") %>%
gather(w_l, country, winner:runner_up) %>%
mutate(w_l = factor(w_l, levels = c("winner", "runner_up")))
wc_geo <- left_join(world, winners, by = c("admin" = "country")) %>%
drop_na(Year)
p <-
ggplot() +
geom_sf(data = world, colour = "#ffffff20", fill = "#2d2d2d60", size = .5) +
geom_sf(data = wc_geo, aes(fill = w_l, group = interaction(w_l, Year))) +
scale_fill_manual(values = c("#D9A441", "#A8A8A8"), name = NULL, labels = c("Winner", "Runner-Up")) +
theme(legend.position = c(0.9, 1.01), legend.direction = "horizontal", axis.text = element_blank(),
panel.grid.minor = element_blank(), panel.grid.major = element_blank())
anim <- p + transition_time(Year)
animate(anim)
由reprex 包(v0.3.0)于 2020 年 4 月 24 日创建
PS 总是同一个国家,不是吗...
推荐阅读
- salesforce-marketing-cloud - 营销云中的 Ampscript - 如果值 LIKE
- javascript - 返回数组中的正数组
- python - 从 itertool.combination 列表中获取元组索引
- python - 如何在运行时打印文件(源代码)的行号?
- python - 按 groupby 均值填充 PySpark 数据框列的空值
- android - 使 android TextView 溢出到下一个视图
- sql - 如何在 DB2 中组合来自不同数据库(DBeaver EE)的查询?
- css - SASS 是否插入浏览器前缀以实现兼容性?
- java - Json 有效负载为空,而 xml 对 Spring MVC 项目工作正常
- database - 持久化由 @SecondaryTable 映射的字段