Different Ways Of Visualizing Twitter Sentiments Analysis In R (2024)

Freeman Goja

·

Follow

7 min read

·

Jan 7, 2020

--

Different Ways Of Visualizing Twitter Sentiments Analysis In R (3)

In my previous article on Sentiment Analysis of WhatsApp Chats, I had given an introduction on how to perform sentiment analysis using R. Unlike WhatsApp data, extracting twitter data is a little bit tricky. You must successfully set up Twitter API to get authorization. There are a lot of useful online resources on how to set up twitter API so, this article will be focusing mainly on analysis and visualizations.

Twitter data is perfect for sentiment analysis. With so much flexibility, you can easily restrict your selection of tweets to a particular date range, language, region, number of tweets and then some. In this article, we shall be gauging twitter sentiments towards Trump and visualizing the results in different ways. These tweets were extracted on November 22, 2019 while the congress-led impeachment hearing was going on.

After setting up your API, you will have consumerKey, consumerSecret, accessToken and accessTokenSecret which you need to copy and paste in RStudio like this:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (4)

Then you can set up your authorization like this:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (5)

Once successfully set up, you are ready to start extracting tweets for analysis. The basic R packages you need are:

ROAuth: This R package provides the interface to the OAuth 1.0 for user authentication

twitteR: This is the R package that accesses twitter API. It tends to favour the extraction of analytical data over daily interactions.

You can find all the packages used in the analysis and full analysis code here.

The searchTwitter function in twitteR package provides different options for scrapping tweets. These arguments which are null by default (except n) can be used in isolation or in combination of more than one argument joined by “+”.

n -Specifies the maximum number of tweets you wish to extract.

lang -Tweets about one subject matter may come in different languages. It is NULL by default but when specified, restricts your search to a given language based on the ISO 639–1 code.

since -Used for restricting searches to those tweets since the given date. Please note that date must be in the format YYYY-MM-DD.

until -Specifies the latest date of the tweets you are interested in. Please note that date must be in the format YYYY-MM-DD.

locale -Relatively the most limited option as only ja is currently effective but when specified it sets the locale for the search.

geocode -Lets you define a geographical radius of interest given in latitude/longitude.

sinceID -Used to restrict tweets to only those with IDs newer than the specified ID.

maxID -Restricts search to tweets older than the specified ID.

resultType -Used to filter the returned tweets based on set values.

Different Ways Of Visualizing Twitter Sentiments Analysis In R (6)

In the example above, 10,000 tweets about Trump were retrieved starting from the day of the search and going backwards (default). The scrapped tweets were in a list and needed to be converted to a dataframe as follows:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (7)

To ensure that there is no personal contribution from Trump himself to the collected tweets, all tweets and retweets from @realDonaldTrump and @POTUS handles were removed from our data by the following code:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (8)

There were lots of retweets in the dataset as can be seen below:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (9)

To find the trending tweets about Trump, only unique tweets with the highest retweetCount were required so a new dataframe with only unique tweets was created and pre-processed to view the trending tweets:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (10)

Tweets just like other social media posts are loosely structured and mining them require a great deal of cleaning as we discussed in my earlier article. We also compared the two different approaches to sentiment analysis using R the last time. This article will be considering the bag of words and other related models.

After finding the trending tweets, further analysis were done on the full dataset. The logic here is that, someone who is retweeting another person’s tweet is most likely doing so because the original tweet is representative of his/her thoughts, so both equally count for sentiments. The data was pre-processed and prepared for analysis as shown in this full code.

The top ten positive words as tokenized can be visualized using the kable function in kableExtra package as follows:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (11)

Similarly, we can represent the negative words as shown below:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (12)

In the two figures above, the top 10 words each were chosen but you can choose as many words as possible. You can also order the words based on their frequencies if desired. You can see that the most used positive word was “like” while the most negative used word was “impeach”.

We can also represent the sentimental words in bar graphs as shown below.

Different Ways Of Visualizing Twitter Sentiments Analysis In R (13)
Different Ways Of Visualizing Twitter Sentiments Analysis In R (14)

Again, you can choose to order the plots from the tallest to the shortest bar but that’s not necessary here and so I opted to maintain the order this way to align with the order of the kableExtra html output earlier.

The DT package allows one to create an interactive output that is easy to browse through and find all the sentimental words as shown in this html file. This is how it looks like:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (15)

This could be useful when you are interested in looking for particular key words of interest in your sentiments analysis. The search option enables you to look for the existence or otherwise of a specific word.

The wordcloud function was used to create bags of words with minimum count of 10 for the positive and negative sentiments respectively. The choice to limit the entrance of words to minimum count of 10 was to keep the bag of words simple and readable.

Different Ways Of Visualizing Twitter Sentiments Analysis In R (16)
Different Ways Of Visualizing Twitter Sentiments Analysis In R (17)

You can easily pick out the top 10 words in both bags of words as they appear bolder in each case. Clearly, you can see that “like” and “impeach” are the most popular words in the positive and negative sentiments respectively.

Also, you would have noticed our results visualized in different ways have been consistent, which gives us the confidence that depending on the situation, we could go with any one or more of them and still arrive at a confident conclusion. The Bag of words model is very popular, but sometimes you don’t necessarily need it when a datatable can return a more interactive result, kableExtra can give you a sophisticated but easier to interpret html output or of course, bar plot can visualize it pretty good in a self explanatory style.

Let us look at the distribution of sentiments.

Different Ways Of Visualizing Twitter Sentiments Analysis In R (18)

Here are the proportions:

Different Ways Of Visualizing Twitter Sentiments Analysis In R (19)
Different Ways Of Visualizing Twitter Sentiments Analysis In R (20)
Different Ways Of Visualizing Twitter Sentiments Analysis In R (21)

The graph above shows that there’s far more negative sentiments as there are positive sentiments. However, an overwhelming larger neutral sentiments presents a huge opportunity. Depending on the setting whether it be political or business, both sides (negative and positive) can devise strategies to sway the neutrals. The ultimate winner would be who manages to get more to their side.

The main take away from the proportion analysis is the value of the neutral sentiments. Often, sentiments analysis focus on the negative and positive and that’s okay when you have significant proportions in those areas but when a significant proportion is neutral, it is an opportunity too risky to ignore.

I hope you enjoyed and learnt something from this piece. Stay in touch and see you in my next article titled Predictive Modellers’ Guide To Choosing The Best Fit Regression Model.

Different Ways Of Visualizing Twitter Sentiments Analysis In R (2024)

FAQs

What is the best way to visualize sentiment analysis? ›

Once you have performed sentiment analysis on your text data, you can visualize the results using different types of charts and graphs. For example, you can use a bar chart, a pie chart, or a donut chart to show the distribution of sentiment categories or scores across your data.

How to do sentiment analysis on Twitter data using r? ›

Twitter Sentiment Analysis and Visualization using R
  1. Step 1: Load the required packages (including rtweet) in RStudio.
  2. Step 2: Authenticate using your credentials to Twitter's API by creating an access token.
Jan 19, 2020

What is the best model for Twitter sentiment analysis? ›

There are multiple types of algorithms available that can be applied to the sentiment analysis of Twitter data. Some of the most efficient algorithms are Support Vector Machine (SVM), Recurrent Neural Network (RNN), Convolutional Neural Network (CNN), Random Forest, Naïve Bayes, and Long Short-Term Memory (LSTM).

Which method is best for sentiment analysis? ›

Automated sentiment analysis relies on machine learning (ML) techniques. In this case a ML algorithm is trained to classify sentiment based on both the words and their order. The success of this approach depends on the quality of the training data set and the algorithm.

What is the best visualization method? ›

Top data visualization techniques to use and understand in 2024
  • Scatter plots. ...
  • Treemaps. ...
  • Line chart/line graph. ...
  • Histograms. ...
  • Area chart. ...
  • Box plots/Whisker plot. ...
  • Word clouds. ...
  • Bubble charts. This is a colorful way to show how many values you have in each category.
Apr 19, 2024

Can you use R for sentiment analysis? ›

The foundational steps involve loading the text file into an R Corpus, then cleaning and stemming the data before performing analysis. I will demonstrate these steps and analysis like Word Frequency, Word Cloud, Word Association, Sentiment Scores and Emotion Classification using various plots and charts.

How do you scrape Twitter data for sentiment analysis? ›

Install the Python library (e.g., Tweepy, Snscrape) using pip. Authenticate your requests to the Twitter API using your API keys and access tokens. Use the library's functions to scrape tweets. Export the scraped data into a format of your choice (e.g., CSV, JSON) for analysis.

How to get Twitter data using R? ›

You can use the package httr to make HTTP requests to the Twitter API. If you haven't already installed this, please install the package in your console. You will also need to install jsonlite to work with our JSON object and dplyr for data manipulation. You can now begin writing your R script to connect to the API.

Which algorithm is best for sentiment analysis? ›

Statistical machine learning models like Naive Bayes Classifier, Support Vector Machine (SVM), Logistic Regression, Random Forest, and Gradient Boosting Machines (GBM) are all valuable for sentiment analysis, each with their strengths.

Which Twitter analytics tool is the best? ›

Now that you've established what you're looking for, review our research of the top 10 Twitter analytics tools.
  • Twitter Analytics (Native) Features: The native Twitter Analytics platform offers a wide range of basic information. ...
  • Keyhole. ...
  • Hootsuite. ...
  • Buffer. ...
  • Sprout Social. ...
  • Klear. ...
  • Agorapulse. ...
  • Whatagraph.
May 19, 2024

Which optimizer is best for sentiment analysis? ›

Additionally, a comprehensive review 4 compared sentiment analysis methods based on lexicon or machine learning, with the Naïve Bayes model achieving the best accuracy. Another study 5 used the Shark smell optimizer algorithm with SVM for sentiment analysis, achieving an accuracy of 92.12% after optimization.

What is the methodology of Twitter sentiment analysis? ›

Twitter sentiment analysis examines the overall feeling or emotion expressed in tweets. It employs machine learning and natural language processing techniques to automatically categorize tweets as good, negative, or neutral depending on their content.

How accurate is Twitter sentiment analysis? ›

performed sentiment analysis by conducting experiments on a Twitter dataset with different classifiers. They produced the best results using MLP and SVM with more than 80 % accuracy.

Why is Twitter best for sentiment analysis? ›

Twitter users are often more likely to express their opinions on current events, politics, and cultural topics. The platform's fast-paced nature and the brevity of tweets (280 characters) can make it challenging to accurately analyze sentiment, but it can still provide valuable insights into public opinion.

How do you monitor sentiment analysis? ›

There are various ways to calculate a sentiment score, but the most common method is to use a dictionary of negative, neutral, or positive words. The text is then analyzed to see how many negative and positive words it contains. This can give us a good idea of the overall sentiment of the text.

What is the best model for sentiment analysis? ›

Statistical machine learning models like Naive Bayes Classifier, Support Vector Machine (SVM), Logistic Regression, Random Forest, and Gradient Boosting Machines (GBM) are all valuable for sentiment analysis, each with their strengths.

What is the best sentiment indicator? ›

One of the most frequently used indicators of market sentiment is the CBOE Volatility Index or VIX. The VIX is a forward-looking indicator that measures volatility in the S&P 500 index for the next 30 days.

Which algorithm is best suited for sentiment analysis? ›

Naive Bayes: Most simple and efficient method for sentiment analysis. Support Vector Machines (SVM): Good for high-dimensional sentiment classification.

References

Top Articles
Easy French Onion Soup Recipe | The Gracious Pantry
18 Olive Garden Copycat Recipes to Satisfy Your Italian Food Cravings
'That's Hilarious': Ahsoka's Ezra Bridger Actor Reveals Surprising True-To-Life Detail Behind Sabine Reunion Scene
Moonrise Tonight Near Me
Minus8 Patreon
James Darren, ‘Gidget’ teen idol, singer and director, dies at 88
Endicott Final Exam Schedule Fall 2023
SSD an SATA Anschluss bei Futro S920
Authentication Portal Pvusd
Member Handbook 2021 | Ohio Medicaid Caresource | Member Handbook
Sunshine999
Unlock the Fun: A Beginner's Guide to Playing TBG95 Unblocked Games at School and Beyond
Configuring Fail2ban with Traefik
Precision Garage Door Long Island
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
New & Used Motorcycles for Sale | NL Classifieds
Franklin City School District - Ohio
Busted Newspaper Randolph County Missouri
Who Is Denise Richards' Husband? All About Aaron Phypers
Myth or Fact: Massage Parlors and How They Play a Role in Trafficking | OUR Rescue
Star Rug Aj Worth
Eliud Kipchoge Resting Heart Rate
Can You Put Elvie Stride Parts In Sterilizer
M Life Insider
Tethrd Coupon Code The Hunting Public
CHERIE FM en direct et gratuit | Radio en ligne
Does Gamestop Sell Magic Cards
E41.Ultipro.com
Kvoa Tv Schedule
Ihub Kblb
Busted Paper Haysi Regional Jail
8663081159
Accuweather Radar New York City
Don Wallence Auto Sales Reviews
Directions To 401 East Chestnut Street Louisville Kentucky
20 Fantastic Things To Do In Nacogdoches, The Oldest Town In Texas
Sentara Reference Lab Solutions Bill Pay
Fx Channel On Optimum
How Much Do Internet and Wi-Fi Cost?
7UP artikelen kopen? Alle artikelen online
Charlotte North Carolina Craigslist Pets
Craigslist Hart Mi
Rennlist Com Forums
Enlightenment Egg Calculator
China Rose Plant Care: Water, Light, Nutrients | Greg App 🌱
13 The Musical Common Sense Media
Okeeheelee Park Pavilion Rental Prices
ARK Fjordur: Ultimate Resource Guide | Where to Find All Materials - Games Fuze
Beacon Schneider La Porte
Pinellas Fire Active Calls
The Hollis Co Layoffs
The Starling Girl Showtimes Near Alamo Drafthouse Brooklyn
Latest Posts
Article information

Author: Tyson Zemlak

Last Updated:

Views: 6128

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.