gganimate 地图未加载

gganimate map not loading

随着时间的推移,我正在为美国制作县级动画。我可以很容易地制作 ggplot(所以我知道我的数据在形状文件的正确条件下),但每次我包含 transition_time() 或 transition_state() 来动画它时,“地图”在第一帧之后,我得到的要么是完全空的,要么是完全空的。具体来说,这个有效:

# Plot graph of January sales prices--works just fine ...
playdata_one = subset(playdata, playdata$date == 202001)
graph1 = ggplot() + 
  coord_fixed(1.3) +
  labs(title = "Median sales prices", subtitle = "January 2020") +
  theme(panel.background = element_blank(),
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +  
  geom_polygon(data = playdata_one, aes(x = long, y = lat, fill = saleprice, group = group)) + 
  geom_polygon(data = states, aes(x = long, y = lat, group = group), color="black", fill = NA) +
  scale_fill_viridis(na.value="transparent")

但事实并非如此:

# Plot animated graph of January to March--fails ...
graph2 = ggplot() + 
  coord_fixed(1.3) +
  labs(title = "Median sales prices", subtitle = "{unique(msa_map_final$date[msa_map_final$date == {frame_time}])}") +
  theme(panel.background = element_blank(),
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +  
  geom_polygon(data = playdata, aes(x = long, y = lat, fill = saleprice, group = group)) + 
  geom_polygon(data = states, aes(x = long, y = lat, group = group), color="black", fill = NA) +
  scale_fill_viridis(na.value="transparent") + 
  transition_states(date)

anim = animate(graph2)
anim

有什么想法吗?我已经上传了我的完整(示例)代码和数据 here

您只能看到亚利桑那州,因为您的数据主要来自亚利桑那州。我只选择了 playdata 的一个子集作为 df 来展示这一点。你原代码的问题是 subtitle 需要和 transition_states.

保持一致
df <- playdata %>% filter(date >=201912, date <202005)

graph2 = ggplot() + 
  coord_fixed(1.3) +
  labs(title = "Median sales prices", 
       subtitle = "{unique(df$date[df$date == {closest_state}])}"
       ) +
  theme(panel.background = element_blank(), 
        axis.title = element_blank(), 
        axis.text = element_blank(),
        axis.ticks = element_blank()) +  
  geom_polygon(data = states, aes(x = long, y = lat, group = group), color="black", fill = NA) +
  geom_polygon(data = df, aes(x = long, y = lat, fill = saleprice, group = group)) + 
    scale_fill_viridis(na.value="transparent") + 
  transition_states(date)
  
frames<-  df$date %>% unique %>% length
anim = animate(graph2, nframes=frames)

anim