Putting twitter data on the map

Analyzing Social Media Data in R

Sowmya Vivek

Data Science Coach

Lesson overview

  • Types of geolocation data available in tweets
  • Sources of geolocation information
  • Extract location details from tweets
  • Plot the tweet location data on maps
Analyzing Social Media Data in R

Why put twitter data on the map

  • Mapping locations help understand where tweets are concentrated
  • Influence people in those locations with targeted marketing
  • Understand reactions to planned or unplanned events
Analyzing Social Media Data in R

Include geographic metadata

  • Twitter users can geo-tag a tweet when it is posted
  • Two types of geolocation metadata
    • Place
    • Precise location
Analyzing Social Media Data in R

Place

  • "Place" location is selected from a predefined list
  • Includes a bounding box with latitude and longitude coordinates
  • Not necessarily issued from the location of the tweet

Tweet with place location

Analyzing Social Media Data in R

Precise location

  • Specific longitude and latitude "Point" coordinate from GPS-enabled devices
  • Represents the exact GPS location
  • Only 1-2% of tweets are geo-tagged
Analyzing Social Media Data in R

Sources of geolocation information

  • The tweet text
  • User account profile
  • Twitter Place added by the user
  • Precise location point coordinates
Analyzing Social Media Data in R

Extract tweets

library(rtweet)
# Extract 18000 tweets on "#politics"
pol <- search_tweets("#politics", n = 18000)
Analyzing Social Media Data in R

Extract geolocation data

# Extract geolocation data and append new columns
pol_coord <- lat_lng(pol)
  • The coordinates are extracted from the columns, coords_coords or bbox_coords
Analyzing Social Media Data in R

View lat and lng columns

View(pol_coord)

lat and lng columns

Analyzing Social Media Data in R

Omit rows with missing lat and lng values

# Omit rows with missing lat and lng values
pol_geo <- na.omit(pol_coord[, c("lat", "lng")])
Analyzing Social Media Data in R

View geocoordinates

head(pol_geo)
lat            lng
<dbl>         <dbl>
19.17414    72.874244            
53.35490    -6.247621            
53.27350    -6.399521            
53.67989     9.372680            
12.92311    77.558448            
54.59940    -5.836670
Analyzing Social Media Data in R

Plot geo-coordinates on the US state map

# Plot longitude and latitude values of tweets on US state map
map(database = "state", fill = TRUE, col = "light yellow")

with(pol_geo, points(lng, lat, pch = 20, cex = 1, col = 'blue'))
Analyzing Social Media Data in R

View the locations on the US state map

Plot of geo-coordinates on the US state map

Analyzing Social Media Data in R

Plot geocoordinates on the world map

# Plot longitude and latitude values of tweets on the world map
map(database = "world", fill = TRUE, col = "light yellow")

with(pol_geo, points(lng, lat, pch = 20, cex = 1, col = 'blue'))
Analyzing Social Media Data in R

View the locations on the world map

Plot of geocoordinates on the world map

Analyzing Social Media Data in R

Let's practice!

Analyzing Social Media Data in R

Preparing Video For Download...