library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(wordcloud)
## Warning: package 'wordcloud' was built under R version 3.6.2
## Loading required package: RColorBrewer
library(SentimentAnalysis)
## Warning: package 'SentimentAnalysis' was built under R version 3.6.2
##
## Attaching package: 'SentimentAnalysis'
## The following object is masked from 'package:base':
##
## write
library(ggplot2)
library(tm)
## Warning: package 'tm' was built under R version 3.6.2
## Loading required package: NLP
##
## Attaching package: 'NLP'
## The following object is masked from 'package:ggplot2':
##
## annotate
#Load Data (2 columns, airline and tweet text)
tweets = read.csv("E:/TA Work Docs/Twitter Analysis/Tweets.csv")[ ,c('airline', 'text')]
str(tweets)
## 'data.frame': 500 obs. of 2 variables:
## $ airline: Factor w/ 3 levels "Delta","Southwest",..: 2 2 2 2 2 2 2 1 1 1 ...
## $ text : Factor w/ 500 levels ". @SouthwestAir condescension must be a quality your co rewards. Your tone reeks of it",..: 410 261 379 333 340 320 366 483 484 80 ...
View(tweets)
#convert the Factor to character using as.character
documents= as.character(tweets[,c("text")])
#Analyze sentiments
sentiment <- analyzeSentiment(documents)
tweetSentiments <- convertToDirection(sentiment$SentimentQDAP)
#Simple stacked bar chart of positive and negative sentiments by airline
ggplot(tweets, aes(tweets$airline, fill=tweetSentiments)) + geom_bar()

#Word cloud of positive tweets and negative tweets for each airline (wordcloud package)
docs <- Corpus(VectorSource(tweets$text))
docs <- tm_map(docs, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(docs, content_transformer(tolower)):
## transformation drops documents
docs <- tm_map(docs, removeNumbers)
## Warning in tm_map.SimpleCorpus(docs, removeNumbers): transformation drops
## documents
docs <- tm_map(docs, removeWords, stopwords("english"))
## Warning in tm_map.SimpleCorpus(docs, removeWords, stopwords("english")):
## transformation drops documents
docs <- tm_map(docs, removeWords, c("virginamerica" , "delta" , "southwest" ,"jetblue" ,"flights","southwestair"))
## Warning in tm_map.SimpleCorpus(docs, removeWords, c("virginamerica",
## "delta", : transformation drops documents
docs <- tm_map(docs, removePunctuation)
## Warning in tm_map.SimpleCorpus(docs, removePunctuation): transformation
## drops documents
docs <- tm_map(docs, stripWhitespace)
## Warning in tm_map.SimpleCorpus(docs, stripWhitespace): transformation drops
## documents
dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d, 10)
wordcloud(words = d$word, freq = d$freq, min.freq = 1,
max.words=200, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Dark2"))
