In my previous post I highlighted Tableau’s text mining capabilities, resulting in fancy visuals such as word clouds:
Today I’d like to follow up on this and show how to implement sentiment analysis in Tableau using Tableau’s R integration. Some of the many uses of social media analytics is sentiment analysis where we evaluate whether posts on a specific issue are positive, neutral, or negative (polarity), and which emotion in predominant.
What do customers like or dislike about your products? How do people perceive your brand compared to last year?
In order to answer such questions in Tableau, we need to install an R package that is capable of performing the sentiment analysis. In the following example we use an extended version of the sentiment package, which was initiated by Timothy P. Jurka.
The sentiment package requires the tm and Rstem packages, so make sure that they are installed properly. Execute these commands in your R console to install sentiment from GitHub (see alternative way to install at the end of this blog post):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("devtools") | |
library(devtools) | |
install_github("aloth/sentiment/sentiment") |
The sentiment package offers two functions, which can be easily called from calculated fields in Tableau:
The function get_polarity
returns „positive“, „neutral“, or „negative“:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SCRIPT_STR(' | |
library(sentiment) | |
get_polarity(.arg1, algorithm = "bayes") | |
' | |
, ATTR([Tweet Text])) |
The function get_emotion
returns „anger“, „disgust“, „fear“, „joy“, „sadness“, „surprise“, or „NA“:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SCRIPT_STR(' | |
library(sentiment) | |
get_emotion(.arg1, algorithm = "bayes") | |
' | |
, ATTR([Tweet Text])) |
The sentiment package follows a lexicon based approach and comes with two files emotions_english.csv.gz
(source and structure) and subjectivity_english.csv.gz
(source and structure). Both files contain word lists in English and are stored in the R package library under /sentiment/data
directory.
If text is incorrectly classified, you could easily fix this issue by extending these two files. If your aim is to analyze text other than English, you need to create word lists for the target language. Kindly share them in the comments!
Feel free to download the Packaged Workbook (twbx) here.
Update 11 Aug 2016: If you are having trouble with install_github
, try to install directly form this website:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("Rcpp") | |
install.packages("https://alexloth.com/utils/sentiment/current/sentiment.zip",repos=NULL) |