Analyzing Social Media Data in R
Sowmya Vivek
Data Science Coach
library(rtweet)
# Extract 18000 tweets on "#politics"
pol <- search_tweets("#politics", n = 18000)
# Extract geolocation data and append new columns
pol_coord <- lat_lng(pol)
coords_coords
or bbox_coords
View(pol_coord)
# Omit rows with missing lat and lng values
pol_geo <- na.omit(pol_coord[, c("lat", "lng")])
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
# 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'))
# 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