Creating Smooth Animations in R with tweenr (Traffic Speed Data)

library("utils")  
library("tweenr")
library("gganimate")
library("ggplot2")
Filename1 <- "../Data/d04_text_station_5min_2018_02_05.txt.gz"
Filename2 <- "../Data/d04_text_station_5min_2018_02_07.txt.gz"
Filename3 <- "../Data/d04_text_station_5min_2018_02_09.txt.gz"

PeMS1 <- read.delim(gzfile(Filename1), sep="," ,header=FALSE)
PeMS2 <- read.delim(gzfile(Filename2), sep="," ,header=FALSE)
PeMS3 <- read.delim(gzfile(Filename3), sep="," ,header=FALSE)
# Selection a loop detector station with a given ID, for instance SelectedID=400340 (lanes=4)
SelectedID <- 400340
StationData1 <- subset(PeMS1,PeMS1 [,2]==SelectedID)
StationData2 <- subset(PeMS2,PeMS2 [,2]==SelectedID)
StationData3 <- subset(PeMS3,PeMS3 [,2]==SelectedID)
StationData1$V1 <- as.POSIXlt(StationData1$V1, format="%m/%d/%Y %H:%M:%S")
StationData2$V1 <- as.POSIXlt(StationData2$V1, format="%m/%d/%Y %H:%M:%S")
StationData3$V1 <- as.POSIXlt(StationData3$V1, format="%m/%d/%Y %H:%M:%S")
StationData1$st <- "Monday"
StationData2$st <- "Wednesday"
StationData3$st <- "Friday"
StationData1$colour <- "red"
StationData2$colour <- "blue"
StationData3$colour <- "green"
StationData1$tt <- StationData1$V1$hour + StationData1$V1$min/60
StationData2$tt <- StationData2$V1$hour + StationData2$V1$min/60
StationData3$tt <- StationData3$V1$hour + StationData3$V1$min/60
data <- tween_states(list(StationData1, StationData2, StationData3), 3, 1, 'linear', 300)
ggplot(data, aes(tt,V12,  size = V10, colour=V10, frame = .frame)) +
geom_point(alpha=0.8) + xlab("Time (24 Hours)")+ylab("Speed (mph)")+
xlim(0,24)+
ylim(0,100)+
scale_colour_gradient(low="blue", high="red",guide="legend")
ggplot
tweenr+gganimate

--

--

--

Lead Traffic Modeler @Ulteig

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abdullah Kurkcu

Abdullah Kurkcu

Lead Traffic Modeler @Ulteig

More from Medium

Things You Must Know Before Using Linear Regression | Part 2

Behavioral Data Analysis with R and Python

Manipulating Data with R

Data Analysis and Text Mining on HIMYM DataSet