#!/usr/bin/env rscript library(maps) library(mapdata) library(ggmap) stations <- read.csv('../data/stations.csv', header=TRUE, stringsAsFactors=FALSE) for (i in 0:23) { in_filename <- paste('../data/by_hour/activity_', sprintf("%02d", i), '.tsv', sep = '') out_filename <- paste('../data/by_hour/png/activity_', sprintf("%02d", i), '.png', sep = '') activity <- read.table(in_filename, header=TRUE, sep='\t', stringsAsFactors=FALSE) print(in_filename) print(out_filename) # With the activity loaded, we need to go through each unique # station_id to get the total activity. Total activity will be the # sum of the absolute values of change. appended_stations=c() for (i in 1:nrow(stations)) { station <- stations[i,] station_id <- as.character(station$station_id) station_activity <- activity[which(activity$station_id == station_id),] # Now that we have the activity for this particular station, we can # find the net and absolute amounts of activity station$net_activity <- sum(station_activity$num_bikes_available_change) station$abs_activity <- sum(abs(station_activity$num_bikes_available_change)) appended_stations <- rbind(appended_stations, station) } # Make another field on the appended stations that rank activity on # a scale of 0 to 1. appended_stations$abs_activity_percent = (appended_stations$abs_activity - min(appended_stations$abs_activity)) / (max(appended_stations$abs_activity) - min(appended_stations$abs_activity)) appended_stations$net_activity_percent = (appended_stations$net_activity - min(appended_stations$net_activity)) / (max(appended_stations$net_activity) - min(appended_stations$net_activity)) sb_box <- make_bbox(appended_stations$lon, appended_stations$lat, f = 1) sq_map <- get_map(location = sb_box, maptype = "satellite", source = "google") png(out_filename, 1024, 1024) plot(ggmap(sq_map) + geom_point(data = appended_stations, mapping = aes(x = lon, y = lat), color = "yellow", size = ((10 * appended_stations$abs_activity_percent))) + geom_point(data = appended_stations, mapping = aes(x = lon, y = lat), color = "black", size = 1)) dev.off() }