You are on page 1of 3

#!

/usr/bin/env Rscript
# This R script is to read the indego file and create a gif images

# importing libraries:
library(ggplot2)
library(ggmap)
register_google(key = "AIzaSyA46DHNeClnrzlw5y5qgF0aM-3-pUmwc54")
library(gganimate)
library(lubridate)

philadelphia <- get_map(location = "philadelphia", maptype = "roadmap", zoom = 14, source =


"google")
ggmap(philadelphia)
indego<- read.csv("indego.csv")
#indego <- as.Date(indego$start_time, format = "%m/%d/%Y %H:%M")
Start_h <- format(as.POSIXct(strptime(indego$start_time,"%d/%m/%Y %H:%M",tz="")) ,format =
"%H:%M")
Start_Date <- format(as.POSIXct(strptime(indego$start_time,"%d/%m/%Y %H:%M",tz="")) ,format
= "%d/%m/%Y")

indego$Start_h <- Start_h


indego$Start_Date <- Start_Date

# split start_date into day month year:


indego<- separate(indego, col = Start_Date, into = c("Day", "Month","Year"), sep = "/")

## duration using ggplot:


gga <- ggplot(data = indego, aes(x = start_lon, y = start_lat, colour = bike_type)) +
geom_point() +
transition_time(duration)
labs(title = paste("duration", "{round(frame_time, 0)}"))
animate(gga)

# adding passholder type:


gga <- ggplot(data = indego, aes(x = start_lon, y = start_lat, col = bike_id, group =
bike_id, shape = passholder_type)) + geom_point(size = 3, alpha = 0.5) +
transition_time(duration) +labs(title = paste("duration", "{round(frame_time, 0)}"))
+shadow_wake(wake_length = 0.05)
animate(gga)

# add to map:
ggm <- ggmap(philadelphia) + geom_point(data = indego,aes(x = start_lon, y = start_lat,
colour = bike_type, group = bike_type, shape = passholder_type), size = 3, alpha = 0.8) +
transition_time(duration) +
labs(title = paste("duration", "{round(frame_time,0)}")) +
shadow_wake(wake_length = 0.1)
animate(ggm, fps = 24, duration = 16)

anim_save("bike.gif", ggm)

# I used ffmpeg to save gif as video:

ffmpeg -i bike.gif -movflags faststart -pix_fmt yuv420p -vf


"scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4

You might also like