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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Tableau + R: Back Your Data Visualizations With Statistical Testing
  • Drag and Drop Visualization in R
  • How To Create Interactive Reports in Power BI: A Step-By-Step Tutorial
  • Maximising Data Analytics: Microsoft Fabric vs. Power BI

Trending

  • How to Submit a Post to DZone
  • Automated Testing Lifecycle
  • TDD With FastAPI Is Easy
  • Revolutionizing Software Testing
  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.

Gonçalo Trincao Cunha user avatar by
Gonçalo Trincao Cunha
·
Sep. 07, 17 · Tutorial
Like (6)
Save
Tweet
Share
10.40K 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

  • Tableau + R: Back Your Data Visualizations With Statistical Testing
  • Drag and Drop Visualization in R
  • How To Create Interactive Reports in Power BI: A Step-By-Step Tutorial
  • Maximising Data Analytics: Microsoft Fabric vs. Power BI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: