首页 > 解决方案 > 结合ggmap和ggplot在r中创建动画

问题描述

我最初在 shapefile 中有一些多行字符串数据,我已将其转换为 data.frame 并显示如下(数据)。

我正在尝试为静态卫星地图上的线条制作动画。我可以使用 ggmap 在卫星地图上单独绘制线条,也可以使用 ggplot 为它们设置动画。但是我无法让动画在卫星地图上运行。

我拥有的代码是:

setwd("~/R Projects/pipes")
library("ggmap")
library("ggplot2")
library("tidyverse")
library("readxl")
library("rgdal")
library("sf")
library("lubridate")
library("gganimate")
library("magick")
library("animation")

api<-"my api"
register_google(key=api)
getOption("ggmap")

df <- read.csv('combined.csv')

数据(根据要求在帖子末尾的 dput(df)。)

##  static map
alto <- get_googlemap(center = c(lon = 144.970561, lat = -37.833619),
                  zoom = 11, scale = 2,
                  maptype ='satellite',
                  color = 'color')
ggmap(alto)
ggsave("map.png")

##  static map
alto <- get_googlemap(center = c(lon = 144.970561, lat = -37.833619),
                  zoom = 11, scale = 2,
                  maptype ='satellite',
                  color = 'color')
ggmap(alto)
ggsave("map.png")

中音

## static plus lines


 ggmap(alto) +
   geom_path(data = df, aes(x = lon, y = lat,  group = group)) +
   theme(legend.position="none")
 ggsave("map2.png")

地图2

## animation

pgg <- ggplot() +
  geom_path(data = df, aes(x = lon, y = lat, group = group)) +
  transition_states(df$DATE_OF_CO, transition_length = 2, state_length = 1 ) +
  labs(title = 'Year: {closest_state}') +
  shadow_mark() +
  theme(legend.position="none")

产生一个看起来像这样的动画(不是动画图像,但它确实有效)

动画

上面的一切都有效,但无法在地图顶部显示动画,我认为我可以做到

pgg <- ggmap(alto) + ggplot()
  geom_path(data = df, aes(x = lon, y = lat,  group = group)) +
  transition_states(df$DATE_OF_CO, transition_length = 2, state_length = 1 ) +
  labs(title = 'Year: {closest_state}') +
  shadow_mark() +
  theme(legend.position="none")

甚至上面没有ggplot()。我尝试了很多不同的变化,但无法让动画在静态地图的顶部工作。

更新:我已经设法使用下面的代码对形状文件轮廓进行动画处理,其中 stater 是导入的形状文件。还不是我想要的。

shapefile 上的动画

pgg <- ggplot() +
coord_map(xlim = c(144.3, 147), ylim = c(-38.5, -37.0)) +
geom_sf(data = cropped, mapping = aes(col=stater$DATE_OF_CO)) +
geom_sf(data = lessthan1880, mapping = aes(col=lessthan1880$DATE_OF_CO)) +
transition_states(lessthan1880$DATE_OF_CO, transition_length = 2, state_length = 1 ) +
labs(title = 'Year: {closest_state}') +
shadow_mark() +
theme(legend.position="none",
      axis.text.x = element_blank(), axis.text.y = element_blank())

animate(pgg, renderer = ffmpeg_renderer())

我已经习惯于dput(df[1:20],)获取前 20 行数据以实现可重复性。如果有帮助,我可以放入总数据集,但它非常大。

结构(列表(X = 1:20,id = c(1L,1L,2L,2L,2L,2L,2L,2L,2L,3L,3L,3L,4L,4L,5L,5L,5L,5L, 5L, 5L), x = c(319493.19, 319492.97, 319616.43, 319623.89, 319623.99, 319630.08, 319630.09, 319642.72, 319650.08, 319861.8, 319876.33, 319884.35, 319555.23, 319546.93, 320390.58, 320388, 320384.37, 320383.26, 320371.15, 320362.7), y = c(5814162.42, 5814160.43, 5814444.85, 5814437.72, 5814437.62, 5814431.81, 5814431.8, 5814419.74, 5814412.71, 5814327.08, 5814342.32, 5814350.85, 5813269.08, 5813283.58, 5812354.27, 5812360.93, 5812370.31, 5812373.2, 5812404.73, 5812426.83), order = c(1L , 2L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 1L, 2L, 1L, 2L, 3L, 4L, 5L, 6L), 件 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),组 = c(0.1, 0.1, 1.1, 1.1, 1.1, 1.1 , 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 3.1, 3.1, 4.1,4.1, 4.1, 4.1, 4.1, 4.1), lon = c(144.949659155492, 144.949656162307, 144.951128493667, 144.951211403054, 144.95121251334, 144.95128019949, 144.951280310519, 144.951420681911, 144.951502481523, 144.95388459965, 144.954053331259, 144.954146493697, 144.950140937721, 144.950050317255, 144.959398111446, 144.959370468462, 144.959331577865, 144.959319691108 , 144.959190008275, 144.95909954463), lat = c(-37.8020955460794, -37.802113427358, -37.7995759286726, -37.799641624985, -37.7996425454775, -37.7996960813685, -37.7996961734177, -37.7998072972236, -37.7998720728529, -37.8006851635106, -37.8005507558349, -37.8004755040199, -37.8101544910241, - 37.8100222431272, -37.8185593206355, -37.8184988233163, -37.8184136191003, -37.8183873691346, -37.818100981058, -37.8179002534268), DATE_OF_CO = c(1859L, 1859L, 1862L, 1862L, 1862L, 1862L, 1862L, 1862L,1862L, 1860L, 1860L, 1860L, 1859L, 1859L, 1857L, 1857L, 1857L, 1857L, 1857L, 1857L)), row.names = c(NA, 20L), class = "data.frame")

标签: ranimationggplot2ggmap

解决方案


推荐阅读