我是新手cartogram
,并且正在使用网站上的代码重新创建动画情节geospatial
:https ://www.r-graph-gallery.com/a-smooth-transition-between-chloropleth-and-cartogram.htmlgganimate
但在我现在收到此错误的最后一步:animating
错误:您似乎正在尝试使用已弃用的旧 API。请将您的代码更新到新的 API 或从https://github.com/thomasp85/gganimate/releases/tag/v0.1.1安装旧版本的 gganimate
我的代码(与网站不同的对象名称):
library(tidyverse)
library(maptools)
library(cartogram)
library(viridis)
library(sf)
library(mapproj)
library(gganimate)
library(tweenr)
data("wrld_simpl")
cartogram_data = wrld_simpl[wrld_simpl$REGION==2,]
cartogram_data_sf <- st_as_sf(cartogram_data)
cartogram_sf_proj = st_transform(cartogram_data_sf,3857)
cartogram_plot <- cartogram::cartogram(cartogram_sf_proj, "POP2005", itermax =7)
cartogram_data_df <- broom::tidy(cartogram_data) %>%
dplyr::left_join(cartogram_data@data, by=c("id"="ISO3"))
cartogram_df <- broom::tidy(cartogram_data) %>%
dplyr::left_join(cartogram_data@data, by=c("id"="ISO3"))
这里它使用了我以前从未见过的补间:
cartogram_data_df$id <- seq(1,nrow(cartogram_data_df))
cartogram_df$id <- seq(1,nrow(cartogram_df))
data <- rbind(cartogram_df, cartogram_data_df, cartogram_df)
# Set transformation type + time
data$ease <- "cubic-in-out"
data$time <- rep(c(1:3), each=nrow(cartogram_df))
# Calculate the transition between these 2 objects?
dt <- tween_elements(data, time='time', group='id', ease='ease', nframes = 30)
# check a few frame
ggplot() +
geom_polygon(data = dt %>% filter(.frame==0) %>% arrange(order),
aes(fill = POP2005, x = long, y = lat, group = group), size=0, alpha=0.9
)
ggplot() +
geom_polygon(data = dt %>% filter(.frame==5) %>% arrange(order),
aes(fill = POP2005, x = long, y = lat, group = group) , size=0, alpha=0.9
)
ggplot() +
geom_polygon(data = dt %>% filter(.frame==10) %>% arrange(order),
aes(fill = POP2005, x = long, y = lat, group = group) , size=0, alpha=0.9
)
动画代码:(此步骤/代码块给出错误)
africa_plt <- ggplot() +
geom_polygon(data = dt %>% arrange(order) , aes(fill = POP2005/1000000, x = long, y = lat, group = group, frame=.frame) , size=0, alpha=0.9) +
theme_void() +
scale_fill_viridis(
name="Population (M)", breaks=c(1,50,100, 140),
guide = guide_legend(
keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"),
label.position = "bottom", title.position = 'top', nrow=1)
) +
labs( title = "Africa", subtitle="Population per country in 2005" ) +
ylim(-35,35) +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f4", color = NA),
panel.background = element_rect(fill = "#f5f5f4", color = NA),
legend.background = element_rect(fill = "#f5f5f4", color = NA),
plot.title = element_text(size= 22, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
plot.subtitle = element_text(size= 13, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
legend.position = c(0.2, 0.26)
) +
coord_map() +
# transition_manual(F)
# Make the animation
#animation::ani.options(interval = 1/9)
gganimate(africa_plt, "Animated_Africa.gif", title_frame = F)
我尝试过使用transition_manual(F)
而不是,gganimate(africa_plt, "Animated_Africa.gif", title_frame = F)
但这也不起作用。