In this blog we will learn how to visualize tweets on Google Maps using R!

Twitter App required to get started with this. If you don’t have Twitter Application, go to Twitter Developer link and sign in with your credentials, after that go to twitter Apps & click on “Create New App”button for creating new application.


Once you’ve done this, make a note of your Keys & Access Token.

  • Consumer Key (API Key)
  • Consumer Secret (API Secret)
  • Access Token
  • Access Token Secret

R Packages required: 

install.packages(“ggmap”): It allows us to access maps from the Google Maps API.

ggmap package has a function get_map that can download maps from Google Maps API.

install.packages(“twitteR“): It provides an interface to the Twitter web API.

twitteR package has searchTwitter function that can search tweets based on a supplied search string.

Extracting Maps: 

Extraction of India map using get_map using below coordinates. Coordinates extracted using this Blog .


R Commands: 


I have selected the maptype = “terrain“, other maptype eg. satellite & roadmap.

Here is the output of ggmap(india_map).


Extracting Tweets: 

Twitter OAuth Setup:


Extracting 500 tweets having tag @narendramodi within a 1000 km radius of the given latitude/longitude.

20.593684° N, 78.96288° E is of India Location: Extracted using this Blog.


Here is the output of View(my_tweets_df). 500 Observation & 16 Variable.



Excluding rows from data frame where longitude/latitude=NA & taking only last two columns (unique data).


Here is the final output of View(my_tweets_df). 206 Observation & 2 Variable.


Mapping of Tweets & Google Map: 

Converting both columns of my_tweets_df as numeric.


Mapping Map with my_tweets_df .


Here is the output of plot.

Seems like more tweets (having tag @narendramodi) are coming from Mumbai & Delhi.



Happy Learning!

7 thoughts on “Visualization of Tweets on Google Maps

  1. Hey man am getting this error- :
    In doRppAPICall(“search/tweets”, n, params = params, retryOnRateLimit = retryOnRateLimit, :
    100 tweets were requested but the API can only return 0
    any thoughts on that?


  2. HI ,

    setup_twitter_oauth(consumer_key,consumer_secret,access_token, access_secret)

    #tweets = searchTwitter(“@narendra modi”, n = 10000, lang = “en”)
    tweets= searchTwitter(‘@narendra modi’,geocode=’20.593684,78.96288,1000km’,n=500),

    tweets.df = twListToDF(tweets)

    i am not able to display the longitude/latitude


Leave a Reply

Fill in your details below or click an icon to log in: Logo

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