Last active
February 8, 2018 17:43
-
-
Save petehamilton/1f3dbfa7da4adddd61e7 to your computer and use it in GitHub Desktop.
Plot GoCardless Customers by Location
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(maps) | |
| library(mapdata) | |
| library(doParallel) | |
| library(foreach) | |
| setwd("/Users/petehamilton/gocardless-maps") | |
| # Set variables | |
| gocardless_blue = rgb(0.31,0.57,0.85) | |
| gocardless_blue_translucent = rgb(0.31,0.57,0.85,0.1) | |
| dark_red = rgb(0.753, 0.361, 0.361) | |
| par(family="Gotham-Book") | |
| # Read data | |
| customer_data <- read.csv("data/r-customers-export.csv", header = TRUE) | |
| # Use ALL cores | |
| cl <- makeCluster(detectCores() - 1) | |
| registerDoParallel(cl, cores = detectCores() - 1) | |
| num_frames <- 1800 # 30s @ 60fps | |
| batch_size = round(nrow(customerdata) / num_frames) | |
| foreach(i = 1:num_frames, .packages = c("maps","mapdata")) %dopar% { | |
| # Set name for frame file | |
| filename = paste("frame_", sprintf("%06d", i), ".png") | |
| png(filename, height=3000, width=2000, pointsize = 80) | |
| # Split dataset | |
| from = 1 # Or, for non-cumulative: (i-1)*batch_size | |
| new_start = min((i - 1)*batch_size, nrow(customerdata)) | |
| new_end = min(i*batch_size - 1, nrow(customerdata)) | |
| previous_customers = customerdata[from:max(new_start - 1, 0),] | |
| new_customers = customerdata[new_start:new_end,] | |
| # Initialize map | |
| map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight'), xlim=c(-11,3), ylim=c(49,60.9), fill=FALSE, col='white', mar=rep(1,4)) | |
| # Plot older points in blue | |
| points(previous_customers$lon,previous_customers$lat, col=gocardless_blue_translucent, pch=20, cex=0.2) | |
| # Plot new points in red | |
| points(new_customers$lon, new_customers$lat,col=new_point_colour, pch=20, cex=0.3) | |
| # Make title the date of latest customer plotted | |
| title(customerdata[new_end,]$date, col.main=title_colour) | |
| dev.off(dev.cur()) | |
| } | |
| # 2 second fade in at the end | |
| fadein_frames = 60 | |
| foreach(i = 1:fadein_frames, .packages = c("maps","mapdata")) %dopar% { | |
| # Set name for frame file | |
| filename = paste("frame_", sprintf("%06d", num_frames + i), ".png") | |
| png(filename, height=3000, width=2000, pointsize = 80) | |
| opacity_level = i/fadein_frames | |
| # Initialize map | |
| map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight'), xlim=c(-11,3), ylim=c(49,60.9), fill=FALSE, col=rgb(0.298, 0.308, 0.394, opacity_level), mar=rep(1,4)) | |
| # Plot all points | |
| points(customerdata$lon, customerdata$lat, col=old_point_colour, pch=20, cex=0.2) | |
| title("GoCardless", col.main=rgb(0.31,0.57,0.85, opacity_level)) | |
| dev.off(dev.cur()) | |
| } | |
| stopCluster(cl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment