Skip to content

Instantly share code, notes, and snippets.

@johnschrom
Last active January 5, 2021 21:20
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save johnschrom/8638763 to your computer and use it in GitHub Desktop.
Save johnschrom/8638763 to your computer and use it in GitHub Desktop.
Making a map of foursquare checkins
library(ggplot2)
library(maps)
library(mapproj)
###############################################################################
# Step 1: Get data from Foursquare
# If you already have it, then great :) Otherwise, you can use RPI. The source
# is listed below, and there are instructions for getting keys in the readme.
# RPI: https://github.com/johnschrom/RPI
source('rpi.R');
checkins <- getFoursquare('YOUR ACCESS TOKEN');
checkins <- as.data.frame(checkins)
###############################################################################
# Step 2: Process data
# note: if you already have your data, you can start by loading it in here
checkins.year <- checkins
checkins.year$createdat <- as.Date(as.POSIXct(as.numeric(as.character(
checkins.year$createdat)), origin="1970-01-01"));
# Only choose one of the two options below (either only 2013 data or all data):
# Uncomment for 2013 data only:
checkins.year <- checkins.year[which(
checkins.year$createdat > as.Date('2012-12-31') &
checkins.year$createdat < as.Date('2014-01-01')),
c('venue_lat', 'venue_lng')]
# Uncomment for all data:
#checkins.year <- checkins.year[,c('venue_lat', 'venue_lng')]
checkins.year$venue_lng <- as.numeric(as.character(checkins.year$venue_lng))
checkins.year$venue_lat <- as.numeric(as.character(checkins.year$venue_lat))
checkins.year.project <- mapproject(list(y=checkins.year$venue_lat,
x=checkins.year$venue_lng))
checkins.final <- data.frame(x=checkins.year.project$x,
y=checkins.year.project$y)
# Note: this is only set up for US checkins, but you should be able to include
# international checkins by modifying the map_data below:
states <- map_data("usa",project="albers",par=c(39,45))
###############################################################################
# Step 3: Plot data
p <- ggplot()
p <- p + geom_polygon(data=states, aes(x=long, y=lat, group=group),
colour="#333333", fill="#111111")
p <- p + theme( panel.grid.major = element_line(colour=NA),
panel.grid.minor = element_line(colour=NA),
panel.background = element_rect(fill="black",
colour=NA),
axis.ticks = element_line(colour=NA),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank());
p <- p + geom_point(data=checkins.final, aes_string(x='x', y='y'),
colour='#008C9E', size=1)
plot(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment