DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • From Data to Decisions: Visualizing SAP Insights With Python
  • How To Create a Network Graph Using JavaScript
  • Data Analytics Using Python

Trending

  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • How to Format Articles for DZone
  1. DZone
  2. Data Engineering
  3. Data
  4. GTFS Transit Data Visualization in R

GTFS Transit Data Visualization in R

Learn how to use R with ggplot2 and ggmap to visualize GTFS (General Transit Feed Specification) route and schedule information on a map.

By 
Gonçalo Trincao Cunha user avatar
Gonçalo Trincao Cunha
·
Sep. 07, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
11.5K Views

Join the DZone community and get the full member experience.

Join For Free

GTFS (General Transit Feed Specification) is a specification that defines a data format for public transportation routes, stop, schedules, and associated geographic information.

In this post, we’ll use R with ggplot2 and ggmap to visualize GTFS route and schedule information on a map.

This post uses a GTFS feed from CARRIS, which is a bus public transport operator from the city of Lisbon.

Plot the Transport Network

Plot the whole network on a map:

Image title

The code looks like this:

library(ggmap)
library(ggplot2)
library(ggthemes)
library(dplyr)

#read GTFS data
shapes <- read.csv("shapes.txt")
# fetch the map
lx_map <- get_map(location = c(-9.157513,38.73466), maptype = "roadmap", zoom = 12)
# plot the map with a line for each group of shapes (route)
ggmap(lx_map, extent = "device") +
  geom_path(data = shapes, aes(shape_pt_lon, shape_pt_lat, group = shape_id), size = .1, alpha = .5, color='blue') +
  coord_equal() + theme_map()

Heatmap of Stops With Most Trips

Plot a heatmap of the regions with least and most number of trips. You can see in dark blue the areas with the greater number of trips.

Image titleThe code looks like this:

# read GTFS data
stops <- read.csv("stops.txt")
stop_times <- read.csv("stop_times.txt") %>% sample_n(10000) # use a data sample of 10.000 instead of the whole dataset
trips <- read.csv("trips.txt")
calendar <- read.csv("calendar.txt") %>% filterCalendar("2017-09-11") # filter trips of a given day

#join all stop times with stop info and trips
stops_freq = 
  inner_join(stop_times,stops,by=c("stop_id")) %>% 
  inner_join(trips,by=c("trip_id")) %>%
  inner_join(calendar,by=c("service_id")) %>%
  select(stop_id,stop_name,stop_lat,stop_lon) #%>%

# plot the map with a density/heatmap trips/stops
ggmap(lx_map, extent = "device") +
  stat_density2d(data = stops_freq, aes(x = stop_lon, y = stop_lat, alpha=..level..), # variable transparency according to number of trips
                 size = .5, color='black', bins=5, geom = "polygon", fill='blue') # use 5 bins(transparency levels) to reprisent different densities

#################################################################
# function to filter services valid on the date filter_date_str
filterCalendar=function (calendar, filter_date_str){
  calendar=calendar %>%
    mutate(start_date_dt=as.Date(as.character(start_date), format="%Y%m%d")) %>%
    mutate(end_date_dt  =as.Date(as.character(end_date), format="%Y%m%d"))

  filter_date=as.Date(filter_date_str, format="%Y-%m-%d")
  week_day=c("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday")[as.POSIXlt(filter_date)$wday + 1]
  calendar[filter_date>=calendar$start_date_dt # filter start/end dates
           & filter_date<=calendar$end_date_dt  
           & calendar[[week_day]] == 1 # filter the weekday
           ,]
}

Plot Stops With Size Based on Trip Frequency

Plot a circle for each stop. The circle size and color are based on the trip frequency.

Image title

The code looks like this:

# read GTFS stop_times
stop_times <- read.csv("stop_times.txt")

#join all data and count number of services grouped by stop
  stops_freq = 
    inner_join(stop_times,stops,by=c("stop_id")) %>%
    inner_join(trips,by=c("trip_id")) %>%
    inner_join(calendar,by=c("service_id")) %>%
    select(stop_id,stop_name,stop_lat,stop_lon) %>%
    group_by(stop_id,stop_name,stop_lat,stop_lon) %>%
    summarize(count=n()) %>%
    filter(count>=150) # filter out least used stops

  # plot the map with stop data  
  ggmap(lx_map, extent = "device") +
     geom_point(data = stops_freq,aes(x=stop_lon, y=stop_lat, size=count, fill=count), shape=21, alpha=0.8, colour = "blue")+ #plot stops with blue color
     scale_size_continuous(range = c(0, 9), guide = FALSE) + # size proportional to number of trips
     scale_fill_distiller()  # circle fill proportional to number of trips

GTFS Data Sources

Here's a list of sites where you can get GTFS feeds from multiple operators

  • Transit feeds

  • Transit feeds registry

  • Transporlis data

And that's it. Enjoy!

R (programming language) Data visualization

Opinions expressed by DZone contributors are their own.

Related

  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • From Data to Decisions: Visualizing SAP Insights With Python
  • How To Create a Network Graph Using JavaScript
  • Data Analytics Using Python

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!