knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "figures/" ) devtools::load_all()
The gbfs
package supplies a set of functions to interface with General
Bikeshare Feed Specification .json feeds in R, allowing users to save
and accumulate tidy .rds datasets for specified cities/bikeshare programs.
The North American Bikeshare Association's gbfs
is a standardized data release format for live information on the status
of bikeshare programs, as well as metadata, including counts of bikes at
stations, ridership costs, and geographic locations of stations and
parked bikes.
Features
We're on CRAN! Install the latest release with:
install.packages("gbfs") library(gbfs)
You can install the developmental version of gbfs
from GitHub with:
# install.packages("devtools") devtools::install_github("simonpcouch/gbfs")
The gbfs
is a standardized data feed describing the current status of a
bikeshare program.
Although all of the data is live, only a few of the datasets change often:
station_status
: Supplies the number of available bikes and
docks at each station as well as station availabilityfree_bike_status
: Gives the coordinates and metadata on available
bikes that are parked, but not at a station.In this package, these two datasets are considered "dynamic", and can be
specified as desired datasets by setting feeds = "dynamic"
in the
main wrapper function in the package, get_gbfs
.
Much of the data supplied in this specification can be considered static. If you
want to grab all of these for a given city, set feeds = "static"
when calling
get_gbfs
. Static feeds include:
system_information
: Basic metadata about the bikeshare programstation_information
: Information on the capacity and coordinates of stationssystem_hours
, system_calendar
, system_regions
,
system_pricing_plans
, and system_alerts
Each of the above feeds can be queried with the get_suffix
function, where
suffix
is replaced with the name of the relevant feed.
For more details on the official gbfs
spec, see
this document.
In this example, we'll grab data from Portland, Oregon's Biketown bikeshare program and visualize some of the different datasets.
# load necessary packages library(tidyverse)
First, we'll grab some information on the stations.
# grab portland station information and return it as a dataframe pdx_station_info <- get_station_information("https://gbfs.lyft.com/gbfs/1.1/pdx/gbfs.json") # check it out! glimpse(pdx_station_info)
...as well as the number of bikes at each station.
# grab current capacity at each station and return it as a dataframe pdx_station_status <- get_station_status("https://gbfs.lyft.com/gbfs/1.1/pdx/gbfs.json") # check it out! glimpse(pdx_station_status)
Just like that, we have two tidy datasets containing information about Portland's bikeshare program.
Joining these datasets, we can get the capacity at each station, along with each station's metadata.
# full join these two datasets on station_id and select a few columns pdx_stations <- full_join(pdx_station_info, pdx_station_status, by = "station_id") %>% # just select columns we're interested in visualizing select(id = station_id, lon, lat, num_bikes_available, num_docks_available) %>% mutate(type = "docked")
Finally, before we plot, lets grab the locations of the bikes parked in Portland that are not docked at a station,
# grab data on free bike status and save it as a dataframe pdx_free_bikes <- get_free_bike_status("https://gbfs.lyft.com/gbfs/1.1/pdx/gbfs.json", output = "return") %>% # just select columns we're interested in visualizing select(id = bike_id, lon, lat) %>% # make columns analogous to station_status for row binding mutate(num_bikes_available = 1, num_docks_available = NA, type = "free")
...and bind these dataframes together!
# row bind stationed and free bike info pdx_full <- bind_rows(pdx_stations, pdx_free_bikes)
Now, plotting,
# filter out stations with 0 available bikes pdx_plot <- pdx_full %>% filter(num_bikes_available > 0) %>% # plot the geospatial distribution of bike counts ggplot() + aes(x = lon, y = lat, size = num_bikes_available, col = type) + geom_point() + # make aesthetics slightly more cozy theme_minimal() + scale_color_brewer(type = "qual")
pdx_plot
Folks who have spent a significant amount of time in Portland might be able to
pick out the Willamette River running Northwest/Southeast through the city.
With a few lines of gbfs
, dplyr
, and ggplot2
, we can put together a
meaningful visualization to help us better understand how bikeshare bikes
are distributed throughout Portland.
Some other features worth playing around with in gbfs
that weren't touched
on in this example:
get_gbfs
, will grab every
dataset for a given city. (We call the functions to grab individual datasets
above for clarity.)directory
and
return
arguments.output
argument is left as default in get_free_bike_status
and
get_station_status
(the functions for the dynamic
dataframes,)
and a dataframe already exists at the given path, gbfs
will row bind the
dataframes, allowing for the capability to accumulate large datasets over time.gbfs
feeds, you might find the
get_gbfs_cities
and get_which_gbfs_feeds
functions useful.Please note that the gbfs
R package is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.