Visualising #foursquare api data, #R and Google Visualisations.

Okay so perhaps I got a little overexcited about the moving visualisation demo. Let’s work on something more social.

The Foursquare API let’s you pull back your user data. So we’re going to take that data and generate a map of checkins from it.

1. Get thy data.

Easy enough from the Foursquare API explorer you can copy/paste the full url with authorisation key.

Use curl to pull the data down:

curl https://api.foursquare.com/v2/users/self/checkins?oauth_token=[authtoken]&v=[yyyymmdd] > data.json

2. Getting the data frames ready in R.

The Google Map method (gvisMap()) requires the location (either lat:lon or postcode) in an array and an another for the labels (or tips) when the pin location is clicked. The snippet below loads the Google Visualisation API for R and the we load our saved JSON data into R as a data.frame. Lastly we create two vectors, Postcodes and Tip. For this example I’ll be using Lat/Lon locations.

library(googleVis)checkins = fromJSON('/work/rails/foursquare/data.json')Postcode <- c()Tip <- c()

3. Iterating the locations

Now we need to cycle through the checkins and place the data in the respective vectors. The locations are colon separated so the paste function is used to concat them together. 

for(n in 1:length(checkins$response$checkins$items)) {??    locationname = checkins$response$checkins$items[[n]]$venue$name    location = paste(checkins$response$checkins$items[[n]]$venue$location$lat,    checkins$response$checkins$items[[n]]$venue$location$lng, sep=":")    Postcode <- c(Postcode, location);    Tip <- c(Tip, locationname)}

4. Generating the map

Create a data.frame and add our two vectors

df <- data.frame(Postcode, Tip)

Next use the googleVis library to pull everything together and generate our map.

M2 <- gvisMap(df, "Postcode", "Tip", options=list(showTip=TRUE, mapType="normal"))

Lastly plot the map on screen

plot(M2)

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: