Creating a 3D Time-Space Diagram using R— MTA BusTime Data
Let’s first load the library we are going to use in R.
This is a Markdown document. If you are interested about how to do similar HTML pages, please visit: http://rmarkdown.rstudio.com
We stored MTA Bustime Data using their API for a day for all the buses and converted it into pings.csv
# Read the input into a dataframe df <- read.csv("../Data/pings.csv", head=TRUE, skip=1) # Check the class of the RecordedAtTime column class(df$RecordedAtTime)##  "factor"# convert the class of the RecordedAtTime column
df$RecordedAtTime <- as.POSIXlt(df$RecordedAtTime) # Extract hour from the column head(df$RecordedAtTime$hour)##  7 8 7 8 8 7
Let’s create a subset of the data only containing the bus line B54 and direction 0 so that we can create a 3D Time-space diagram.
# Create another dataframe which will contain one bus line and one direction
# Bus line = B54
df2 <- subset(df, df$PublishedLineName == "B54")
# Direction = 0
df2 <- subset(df2, df2$DirectionRef == 0)
# Let's take a look at the data
# Before creating a 3D Plot, we need to order the dataframe by RecordedAtTime df2 <- df2[with(df2, order(df2$RecordedAtTime)),] # This function will create an interactive 3D plot plot3d(df2$Longitude,df2$Latitude, df2$RecordedAtTime, type="l", col=as.numeric(df2$DatedVehicleJourneyRef))