【问题标题】:gganimate map not loadinggganimate地图未加载
【发布时间】:2021-09-08 11:12:42
【问题描述】:

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

# 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

【问题讨论】:

  • 播放数据从何而来?能否请您使这个问题可重现,以便我们可以运行代码并重现问题?
  • 嗨,乔恩!我认为 GitHub 上的 playdata + playcode 应该足以重现该错误,并希望将其原因揭示给比我更聪明的人。数据来自 zillow.com/research/data --"Median Sale Price (Smooth, All Homes, Monthly View)" ;在播放数据之前,我只是将其与 MSA/县人行横道合并,然后将数据限制在 2020 年的两个州和三个月(大小)。
  • 我认为问题出在subtitle。去掉字幕,你的代码对我有用。
  • 志强--有趣。删除字幕肯定会有所帮助,但在我看来,它现在缺少所有加利福尼亚 MSA(显然只显示了一个亚利桑那 MSA)。你看到类似的东西吗?
  • 我已经删除了我之前的答案并添加了一个新答案。

标签: r ggplot2 gganimate


【解决方案1】:

您只能看到亚利桑那州,因为您的数据主要来自亚利桑那州。我只选择了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

【讨论】:

    猜你喜欢
    • 2015-04-22
    • 2020-03-18
    • 2021-04-05
    • 2018-02-05
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多