Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Combining ggmap with ggplot to create animation in r

$
0
0

I have some multi-line string data originally in shapefile that I have converted to a data.frame and shown below (data).

I am trying to animate the lines over a static satellite map. I can separately plot the lines on top of a satellite map using ggmap and I can also animate them using ggplot. However I can't get the animation to work on top of the satellite map.

The code I have that works is:

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')

data (dput(df) at end of post as requested.)

##  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")

alto

## static plus lines


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

map2

## 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")

Produces an animation that looks like this(not animated image but it does work)

animation

everything above works but can't get the animation on top of the map thought i could just do

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")

Or even the above just without the ggplot(). I've tried lots of different variations but can't get the animation to work over the top of the static map.

Update: I've managed to animate over a shape file outline using the code below and where stater is an imported shapefile. Not what I want yet.

animation over 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())

I have used dput(df[1:20],) to get the first 20 rows of the data for re-producability. If it helps I can put in the total data set but it's pretty big.

structure(list(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), piece = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), group = 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")


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>