首页 > 解决方案 > 使用 animate 包创建地图动画

问题描述

我正在尝试复制一个地图动画,显示从 1990 年到 2012 年每个州的总统投票结果。我的目标是在动画标题中添加年份,以便读者知道哪一年对应于每一帧。但是,我不断收到我无法理解的错误消息。

非常感谢任何有关如何解决此问题的帮助!

library(choroplethr)
library(dplyr)
library(tidyr)
library(magrittr)
library(gganimate)
library(maps)

#download president data
data("df_president_ts")

us <- map_data("state")

elections <- df_president_ts %>%
  #gather into long data
  gather(year, winner, `1789`:`2012`) %>%
  #filter only elections after 1900
  filter(year >= 1900) %>%
  #join with state polygons
  right_join(us, by = "region") %>%
  mutate(party = case_when(
    winner %in% c("SR", "I", "AI", "PR") ~ "Third Party",
    winner == "D" ~ "Democrat",
    winner == "R" ~ "Republican"
  ))
elections$year=as.integer(elections$year)  # convert year from character to integer

## the original code on the website:
p1 <- ggplot(data = elections, aes(frame = year)) +
  geom_polygon(aes(x = long, y = lat, group = group, fill = party),
               color = "#f5f5f2") +
  theme_map() +
  coord_map("albers", lat0=30, lat1=40) + 
  scale_fill_manual(values = c("#05204A", "#A24936", "#3E5641"), 
                    na.value = "gray70",
                    name = "Winning Party") +
  ggtitle("US Presidential Election Results: ")

gganimate(p1, interval = 1.5)

# it shows that 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


## THUS, I changed the code to following:
p1 <- ggplot(data = elections, aes(frame = year)) +
  geom_polygon(aes(x = long, y = lat, group = group, fill = party),
               color = "#f5f5f2") +
  coord_map("albers", lat0=30, lat1=40) + 
  scale_fill_manual(values = c("#05204A", "#A24936", "#3E5641"), 
                    na.value = "gray70",
                    name = "Winning Party") +
  ggtitle("US Presidential Election Results: ")+
  transition_manual(year)

animate(p1)


## The code below creates an animation, but I want to add year to the title so that each frame 
## corresponds to its own year.
p<-ggplot(data = elections,aes(frame=year)) +
  geom_polygon(aes(x = long, y = lat, group = group, fill = party)) +
  coord_map("albers", lat0=30, lat1=40) + 
  scale_fill_manual(values = c("#05204A", "#A24936", "#3E5641"), 
                    na.value = "gray70",
                    name = "Winning Party") 

g<-p+ transition_manual(year)+
  labs(title="US Presidential Election Results")
animate(g)

#I tried: ggtitle("US Presidential Election Results: {frame_time}"), but it does not work.
g<-p+ transition_manual(year)+
  labs(title="US Presidential Election Results: {frame_time}")
h<-animate(g,  renderer = gifski_renderer())


# I also tried the following, it gives an error msg: "Error in seq.default(range[1], range[2], length.out = nframes) : 
# 'from' must be a finite number. 
g<-p + transition_time(year) +
  labs(title = "US Presidential Election Results:{frame_time}")
h<-animate(g)

# lastly, I tried this, but it creates the same err msg: "rror in seq.default(range[1], range[2], length.out = nframes):
# 'from' must be a finite number. 
p<-ggplot(data = elections) +
  geom_polygon(aes(x = long, y = lat, group = group, fill = party)) +
  coord_map("albers", lat0=30, lat1=40) + 
  scale_fill_manual(values = c("#05204A", "#A24936", "#3E5641"), 
                    na.value = "gray70",
                    name = "Winning Party") 
g<-p + transition_time(year) +
  labs(title = "US Presidential Election Results:{frame_time}")
h<-animate(g)

标签: rdictionaryanimationggplot2

解决方案


aes(frame = year)这是旧的代码gganimate

如果您查看?transition_manual有一个名为“标签变量”的部分,它说明了哪些变量可用于标题。

p1 <- ggplot(elections, aes(x = long, y = lat, group = group, fill = party)) +
  geom_polygon(color = "#f5f5f2") +
  #theme_map() +  #not sure where this is from
  coord_map("albers", lat0=30, lat1=40) + 
  scale_fill_manual(values = c("#05204A", "#A24936", "#3E5641"), 
                    na.value = "gray70",
                    name = "Winning Party") +
  transition_manual(year) +
  ggtitle("US Presidential Election Results: {current_frame}")

animate(p1, fps = 3)

我同意错误消息在解决这个问题时并不总是特别有用,但文档本身非常详尽。

在此处输入图像描述


推荐阅读