Skip to content

Instantly share code, notes, and snippets.

@fredbenenson
Last active December 17, 2015 20:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredbenenson/5665047 to your computer and use it in GitHub Desktop.
Save fredbenenson/5665047 to your computer and use it in GitHub Desktop.
Code for Generating a Scatter Plot of Citi Bike Share Station Names
library(ggplot2)
library(rjson)
# Strip out enclosing object so its just an array of stations before importing into R.
# e.g. data should be of the form:
# [
# {"id":72,"stationName":"W 52 St & 11 Av","availableDocks":14,"totalDocks":39,"latitude":40.76727216,"longitude":-73.99392888,"statusValue":"In Service","statusKey":1,"availableBikes":21,"stAddress1":"W 52 St & 11 Av","stAddress2":"","city":"","postalCode":"","location":"","altitude":"","testStation":false,"lastCommunicationTime":null,"landMark":""},
# ...
# ]
#
bike <- fromJSON(paste(readLines("bike_data.json"), collapse = ""))
bike_colnames <- names(unlist(bike)[0:17])
bike <- data.frame(matrix(unlist(bike), ncol=17, byrow=T))
colnames(bike) <- bike_colnames
qplot(data = bike, x = as.numeric(as.character(totalDocks)), y = as.numeric(as.character(availableBikes)),
color = as.numeric(as.character(latitude)),
label = stationName, geom = "text", angle = 10) +
scale_x_continuous("Total Docks") +
scale_y_continuous("# of Available Bikes") +
scale_color_gradient("Latitude (darker is downtown)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment