You need to enable JavaScript to run this app. 使用gganimate绘制动态地图时报错'from'必须为有限数如何解决
优惠活动
大模型
产品
解决方案
定价
更多

使用gganimate绘制动态地图时报错'from'必须为有限数如何解决

报错原因

该报错来自gganimate包的transition_time函数,你的场景下触发原因有两个:

  1. 你提供的样本数据中所有station_install_date取值完全一致,transition_time需要时间变量存在至少两个不同的取值才能生成时间序列帧,当所有时间值相同时,计算得到的时间范围上下限无有效差值,调用seq生成帧序列时就会触发该报错。
  2. 你的数据集是按station_install_date和lat分组的分组tibble,gganimate读取分组数据的时间变量范围时可能出现计算异常,返回非有限值。
解决方案

第一步:预处理数据集

先解除数据集分组,同时确认完整数据集的station_install_date列至少包含2个不同的日期取值:

library(dplyr)
install_time_df <- ungroup(install_time_df)

如果是测试用单日期数据,补充多组不同安装日期的站点数据即可。

第二步:调整动画代码适配累计展示需求

你需要实现累计展示效果,要额外加上shadow_mark参数保留历史帧的点位,否则每帧只会显示对应日期的新增站点,调整后代码如下:

library(gganimate)
library(ggmap)

seoul <- get_googlemap("Seoul, South Korea", zoom=11, maptype = "roadmap")
anim_plot <- ggmap(seoul) + 
  geom_point(data = install_time_df, aes(x = long, y = lat, size = capa_sum), color = "red", alpha = 0.3) +
  transition_time(station_install_date) +
  # 保留所有历史点位,实现累计效果
  shadow_mark(past = TRUE, future = FALSE) +
  ease_aes("linear") +
  # 可选:添加当前帧日期标题
  labs(title = "站点累计安装情况 日期:{frame_time}")

# 渲染输出动画
animate(anim_plot, nframes = 80, fps = 8)

内容的提问来源于stack exchange,提问作者user9532692

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 12:45:05