0

I am new to cartogram, geospatial & gganimate and was recreating animated plot by using code from website: https://www.r-graph-gallery.com/a-smooth-transition-between-chloropleth-and-cartogram.html

But at the last step of animating I am now getting this error:

Error: It appears that you are trying to use the old API, which has been deprecated. Please update your code to the new API or install the old version of gganimate from https://github.com/thomasp85/gganimate/releases/tag/v0.1.1

My Code (with different object names from website):

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"))

Here it uses tweenr which I have never seen before:

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
  )

Animation Code: (this step/code chunk gives an error)

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)

I have tried using transition_manual(F) instead of gganimate(africa_plt, "Animated_Africa.gif", title_frame = F) but that didn't work either.

ViSa
  • 1,563
  • 8
  • 30
  • In the web page you linked to, just above the animation code, there's a note that says: "Note: This code uses the old version of gganimate. It needs to be updated. Please drop me a message if you can help me with that!". – eipi10 May 17 '21 at 16:07
  • @eipi10 yes you are right. I guess this wouldn't be an easy thing to do with the latest version of `gganimate`. Will wait for sometime to see if someone could answer otherwise will send a request to close this question. thanks for letting me know about the **Note**. – ViSa May 17 '21 at 16:12

0 Answers0