#
console
Mapping, R, Twitter

Tweet location and created time on Google Earth

In this post we see how to pull tweets and plot their location as well as the time at which the tweets were posted on Google Earth (spatio-temporal data) which is surprisingly easy to do.

Featured packages: the much underrated plotKML package, the twitteR package, the spacetime package and finally the sp package.

EDIT: This is now neatly wrapped into a package; tweets2earth. Github and manual.

I am far from having a background in GIS but I love maps, everyone loves maps right? ggmap/ggplot2 allow producing visually appealing maps which meet my needs most of the time. However I recently had to explore the geographical spread of tweets over time and so looked into interactive maps as it was very tedious and unprecise to produce PNG maps for different date range and flick through them (StopMotion package anyone?). That is when I stumbled upon the plotKML package – incredible functionalities and very easy to use, I encourage anyone to look into it. The code below is very simple and straightforward, it could be greatly improved.

Google Earth runs on Mac, PC and Linux; go and download it now, it is publicly available though a pro version is now also offered. All displays in Google Earth are controlled by KML files, which we will be writing in R. KML is, in essence, an XML-based language for three-dimensional geospatial data.

First let’s load our libraries.

#Initialise
libs <- c("twitteR", "plotKML", "spacetime", "sp")
lapply(libs, library, character.only=TRUE)

Set up credentials to pull tweets. Note the function below uses the dev version of the twitteR package – if you do not know where to get the keys and tokens mentioned let me know.

setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

Now we can pull the tweets and prepare the data as required for the projection.

#Search Terms
search_terms <- c("use", "many", "keywords", "as", "few tweets",
    "are", "geo-tagged")

#Download tweets
df <- data.frame()
for (i in 1:length(search_terms)){
  print(search_terms[i])
  tweets = searchTwitter(search_terms[i], lang="en")
  df=rbind(merge(search_terms[i], twListToDF(tw),all=TRUE), tweets)
}

#Remove unknown locations
df <- df[!is.na(df$longitude),]

#Change long & lat class
for (i in 15:16) {
  df[,i] <- as.numeric(df[,i])
}

#Prepare plot
sp <- SpatialPoints(df[,c("longitude","latitude")])
proj4string(sp) <- CRS("+proj=longlat +datum=WGS84")
df_st <- STIDF(sp, time = df$created, data = df[,c("screenName","retweetCount", "favorited")])

#get icon
shape = "http://maps.google.com/mapfiles/kml/pal2/icon18.png"

The plot – the last line opens the kml file on Google Earth.

  • Size = number of retweets
  • label = screenName of user
  • color = favorited (boolean) – whether the tweet has been favortied or not.
  • slider in top left to see how the tweets propagated over time.
kml(df_st, dtime = 24*3600, size = retweetCount, colour = favorited,
    shape = shape, labels = screenName, colour_scale = SAGA_pal[["SG_COLORS_GREEN_BLACK"]])
system("open df_st.kml") #Opens KML file on Google Earth

You can mess with the colour, sizes etc. My understanding is that the SAGA_pal and R_pal colour palettes work well with the plotKML package. Tip: KML files being similar to XML can easily get large, you can specify the kmz parameter in the kml() function to compress it (?plotKML | ?kml).

You can download an example in the sidebar – tweets from LA, Austin and Miami over a 20-day period.

google_earth2

Advertisements

Discussion

Trackbacks/Pingbacks

  1. Pingback: Stream(ish) Tweets location to Google Earth | SocialFunction() - October 13, 2014

  2. Pingback: Twitter crawler with R & [DBM] | SocialFunction() - December 17, 2014

  3. Pingback: Stream(ish) Tweets location to Google Earth - Jabber Cruncher - May 24, 2015

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 )

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: