Location, Location, Location

Adnan Fiaz

Introduction

Step 1: Where is everyone?

Question: Are there any useRs in Birmingham?
Data souce: Twitter (#rstats)

library(rtweet) # http://rtweet.info for more information
library(tidyverse)

# get all tweets that mention #rstats
usrs <- search_tweets("#rstats", n = 18000) %>% 
  # extract user data from the tweets
  users_data() %>% 
  select(user_id, location) %>% 
  # drop users with no location
  filter(location != "") %>%
  # join together with the following (not everyone tweets)
  union(
    # get user data from users that mention #rstats in their profile
    search_users("#rstats", n=1000) %>% 
    select(user_id, location) %>% 
    filter(location != "")
  ) %>% 
  # drop duplicates
  distinct()

Step 1: Where is everyone?

user_id location
636864273 München, Bayern
107600670 Philadelphia
2181077592 New York, NY
507582860 St. Louis, MO
2176483988 New York, USA
40699859 Saint John, New Brunswick

Step 1: Where is everyone?

To enable visualisation we need more than the location, we need coordinates.

library(ggmap) # https://github.com/dkahle/ggmap for more info
# register a google api key to overcome quota limit
register_google(key=google_api_key)
# get the longitude/latitude 
usrs <- usrs %>%
  # limiting to the first 1500 users to not use up the quota
  slice(1:1500) %>%  
  mutate_geocode(location) %>% 
  # drop failed requests
  filter(!is.na(lat))

Step 1: Where is everyone?

qmplot(x=lon, y=lat, data=usrs, maptype="toner-lite", color = I("blue"), extent="device")

Step 1: Where is everyone?

Step 2: What are my options?

Question: where can I hold a meetup?
Data source: Google Maps / Places

library(googleway) # https://github.com/SymbolixAU/googleway for more info
# search for places within a radius of a location
center_of_birmingham <- c(52.483056,-1.893611)
function_rooms <- google_places(location = center_of_birmingham,
                    keyword = "function room", radius = 5000,
                    key = google_api_key)

Step 2: What are my options?

name lat lng
Rai Function Hall 52.50090 -1.932395
Vauxhall Sports & Social Club/ function room hire 52.48688 -1.877048
Sapphire Conference & Banqueting Suite 52.47202 -1.881626
Elegance Suite (Birmingham) 52.49164 -1.871361
Kashmiri Dera Family Hall & Function Room 52.51670 -1.856179
Diamonds Function Centre 52.48968 -1.906345
Diamonds Function Centre 52.48126 -1.893614
Yenton Assembly Rooms Ltd 52.51927 -1.847201
Emerald Club 52.47333 -1.856447
Gti Function Room 52.50206 -1.926524
Saint Mary and St John’s Bar and Function Room 52.51634 -1.848177

Step 2: What are my options?

Step 2: What are my options?

Step 2: What are my options?

Step 3: How do I get there?

Question: Are there any good transport links near the venues?
Data Source: Google Maps

train_stations <- google_places(location = center_of_birmingham,
                    place_type = "train_station", radius = 5000,
                    key = google_api_key) %>% 
  parse_result() %>%  
  mutate(type="train_station")

Step 3: How do I get there?

Step 3: How do I get there?

Step 4: Putting it all together

Step 4: Putting it all together

Step 4: Putting it all together (the easy way)

Calculate distances

get_distances <- function(origins, destinations){
  # query the Maps Distance API
  dis <- google_distance(origins, destinations, key = google_api_key, mode="walking")
  
  # check if the query returns any results
  if("distance" %in% names(dis$rows$elements[[1]])){
    return(dis$rows$elements[[1]]$distance$value)
  } 
  return(NA)
}

Step 4: Putting it all together (the easy way)

Filter and sort results

# for each option, calculate the distance to all transport links
result <- map2(options$lat, options$lng, 
               ~ get_distances(c(.x, .y), transport[, c("lat", "lng")])) %>% 
  # rbind all the vectors
  do.call(rbind, .) %>% 
  # convert to data.frame
  as.data.frame() %>% 
  # add the name of each option
  cbind("name"=options$name) %>%
  # transpose the data.frame to get one [option, transport, distance] combo
  gather(key="transport", value="distance", -name) %>% 
  filter(!is.na(distance), distance < 500) %>% 
  count(name) %>%
  top_n(3, wt=n)

Step 4: Putting it all together (the easy way)

name n
Birmingham School of Media 1
Diamonds Function Centre 1
The Button Factory 1
The Shakespeare 2
Total Pub Solutions Ltd 1
Unique Within The University of Birmingham 1

Summary

Questions?