Putting twitter data on the map
1. Putting twitter data on the map
Twitter allows tweets to include geographic metadata indicating the location from where the tweet was authored.2. Lesson overview
In this lesson, we will understand the types of geolocation data available in tweets and the sources of this information. We will extract the location details from tweets and plot them on maps.3. Why put twitter data on the map
Putting tweet location data on the map helps understand where the tweets are concentrated so people in those locations can be influenced by targeted marketing. It also helps understand location-based reactions to planned or unplanned events.4. Include geographic metadata
Twitter users can "geotag" a tweet when it is posted. There are two basic types of geolocation metadata available in tweets: Place and Precise location.5. Place
The "Place" location is selected by the user from a predefined list of places on Twitter. When posted, they include a bounding box with longitude and latitude that define the location area ranging from a venue to an entire region. Tweets associated with "Places" are not necessarily issued from that location.6. Precise location
Precise location is a specific longitude and latitude "Point" coordinate from GPS-enabled devices and represents the exact GPS location. A longitude and latitude array with the geographical coordinates is also included with the exact location. The main drawback here is that only 1-2% of tweets are geo-tagged.7. Sources of geolocation information
There are four sources for geolocation information: A location mentioned in the tweet text. Account-level location in the user profile. Twitter Place with a name such as Bengaluru added to the tweet by the user. Tweets that are geotagged with precise location point coordinates.8. Extract tweets
To extract the available geolocation data in tweets, let's first extract 18000 tweets on "hashtag politics" using search_tweets().9. Extract geolocation data
Next, the lat_lng() function from the rtweet library is invoked to extract the geolocation data from the tweets. The coordinates are picked up from the columns, coords_coords or bbox_coords.10. View lat and lng columns
The extracted data is stored in two new columns lat and lng in the data frame. In the columns lat and lng, only some rows are populated with the geolocation coordinates. Note that the availability of geolocation coordinates depends on users tagging location details in the tweet.11. Omit rows with missing lat and lng values
We omit rows that have NA values in the lat and lng columns using the na.omit() function.12. View geocoordinates
We can see the latitude and longitude values of a few rows in the new data frame.13. Plot geo-coordinates on the US state map
Next, we use the map() function to call the US state map with the following arguments: database set to "state", fill set to TRUE to draw state borders, and col set to "light yellow". We plot the latitude and longitude values using the with() function. The arguments are: the columns lat and lng, pch set to "20", cex set to "1", and col set to "blue" for selecting a shape, size, and color for the points plotted on the map.14. View the locations on the US state map
The blue dots represent the geocoordinates of tweets and we can see higher tweet activity on the subject from the east and northeast coasts of the US.15. Plot geocoordinates on the world map
Let's now plot the geocoordinates of the tweets on the world map. The map() function takes the values "world", TRUE, and "light yellow" for the arguments database, fill, and col respectively. The with() function plots the lng and lat values on the world map.16. View the locations on the world map
The plot shows higher tweet activity on politics in the US, western Europe, and India.17. Let's practice!
We learned to extract geocoordinates from tweets and plot them on the map. Let's practice by plotting tweets on the topic "vegan" on the map.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.