-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentimentAnalysisandVis.R
306 lines (251 loc) · 8.57 KB
/
SentimentAnalysisandVis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#Libraries
library(tidyverse)
library(plotly)
library(maps)
library(rbokeh)
library(widgetframe)
library(htmlwidgets)
#Reading Dataset
library(readxl)
Dataset <- read_excel("D:/sem4/A-SIN/PROJECT/Dataset.xlsx")
View(Dataset)
df<-Dataset
dim(df)
#Counting NAs
apply(df, 2, function(x) sum(is.na(x)))
#Tweets +ve, -ve, neutral
df %>%
ggplot(aes(airline_sentiment))+
geom_bar(fill = "tomato", color = "black")+
labs(x = "Airline Sentiment", y = "Count")
#Tweets with Airlines
df %>%
ggplot(aes(airline_sentiment))+
geom_bar(aes(fill = airline))+
labs(x = "Airline Sentiment", y = "Count", fill = "Airlines")
#No. of tweets per airline
df %>%
count(airline, airline_sentiment) %>%
ggplot(aes(airline, n))+
geom_bar(stat = "identity", colour = "grey19", fill = "skyblue")+
facet_wrap(~airline_sentiment, ncol = 2)+
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9))+
labs(x = "Airlines", y = "Count of Tweet Sentiments")
#Count of Sentiments
df %>%
count(airline, airline_sentiment) %>%
ggplot(aes(airline_sentiment, n))+
geom_bar(stat = "identity", colour = "grey19", fill = "skyblue")+
facet_wrap(~airline, ncol = 2)+
labs(x = "Airlines", y = "Count of Tweet Sentiments")
#Mean Airline Sentiment
df %>%
group_by(airline, airline_sentiment) %>%
summarise(
mean_airline_sentiment = mean(airline_sentiment_confidence, na.rm = TRUE)
) %>%
ggplot(aes(airline_sentiment, mean_airline_sentiment))+
geom_bar(stat = "identity", fill = "tomato", color = "black")+
facet_wrap(~airline, ncol = 2)+
labs(x = "Airline Sentiment", y = "Mean Airline Sentiment Confidence")
#Reasons of negative without NA's
df %>%
filter(!is.na(negativereason)) %>%
ggplot(aes(negativereason))+
geom_bar(fill = "violetred3", color = "black")+
theme(axis.text.x = element_text(angle = 30, hjust = 1, size = 9))+
labs(x = "Negative Reasons", y = "Count of Negative Reasons")
#WRT Airlines
df %>%
filter(!is.na(negativereason)) %>%
count(airline, negativereason) %>%
ggplot(aes(negativereason, n))+
geom_bar(stat = "identity", colour = "grey19", fill = "springgreen4")+
facet_wrap(~airline, ncol = 3)+
theme(axis.text.x = element_text(angle = 30, hjust = 1, size = 9))+
labs(x = "Negative Reasons", y = "Count of Negative Reasons")
df %>%
filter(!is.na(negativereason)) %>%
count(airline, negativereason) %>%
ggplot(aes(airline, n))+
geom_bar(stat = "identity", colour = "grey19", fill = "springgreen4")+
facet_wrap(~negativereason, ncol = 3)+
theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 9))+
labs(x = "Airlines", y = "Count of Negative Reasons")
#No. of Retweets
df %>%
group_by(airline_sentiment) %>%
summarise(
number_of_retweets = sum(retweet_count, na.rm = TRUE)
)
#Reason retweets
df %>%
filter(airline == "United") %>%
group_by(negativereason) %>%
summarise(
n_retweets = sum(retweet_count, na.rm = TRUE)
) %>%
ggplot(aes(negativereason, n_retweets))+
geom_bar(stat = "identity", fill = "skyblue", color = "black")+
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10))+
labs(x = "Negative Reasons", y = "Number of Retweets")
#More Libraries
library(dplyr)
library(tidytext)
library(RColorBrewer)
library(ggplot2)
library(wordcloud)
library(tm)
options(warn=-1)
dataset<-df
str(dataset)
dataset$text <- as.character(dataset$text)
tidy_dataset <- dataset %>%
unnest_tokens(word, text)
summary(dataset$airline_sentiment)
#Visualization of whether the sentiment of the tweets was positive, neutral, or negative for each airlines
ggplot(dataset, aes(x = airline_sentiment, fill = airline_sentiment)) +
geom_bar() +
facet_grid(. ~ airline) +
theme(axis.text.x = element_text(angle=65, vjust=0.6),
plot.margin = unit(c(3,0,3,0), "cm"))
#Frequent Words in positive sentiment
positive <- tidy_dataset %>%
filter(airline_sentiment == "positive")
list <- c("to", "the","i", "a", "you", "for", "on", "and", "is", "are", "am",
"my", "in", "it", "me", "of", "was", "your", "so","with", "at", "just", "this",
"http", "t.co", "have", "that", "be", "from", "will", "we", "an", "can")
positive <- positive %>%
filter(!(word %in% list))
dtm<-as.matrix(positive)
wordcloud(dtm[,15],
max.words = 100,
random.order=FALSE,
rot.per=0.30,
use.r.layout=FALSE,
colors=brewer.pal(10, "Blues"))
positive <- positive %>%
count(word, sort = TRUE) %>%
rename(freq = n)
head(positive, 21)
positive <- positive %>%
top_n(21)
colourCount = length(unique(positive$word))
getPalette = colorRampPalette(brewer.pal(9, "Set1"))
# The Most 21 Frequent Words in Positive Tweets
positive %>%
mutate(word = reorder(word, freq)) %>%
ggplot(aes(x = word, y = freq)) +
geom_col(fill = getPalette(colourCount)) +
coord_flip()
#Negative Word Cloud
negative <- tidy_dataset %>%
filter(airline_sentiment == "negative")
negative <- negative %>%
filter(!(word %in% list))
dtm_n<-as.matrix(negative)
wordcloud(dtm_n[,15],
max.words = 100,
random.order=FALSE,
rot.per=0.30,
use.r.layout=FALSE,
colors=brewer.pal(10, "Reds"))
negative <- negative %>%
count(word, sort = TRUE) %>%
rename(freq = n)
negative <- negative %>%
top_n(21)
colourCount = length(unique(negative$word))
getPalette = colorRampPalette(brewer.pal(8, "Dark2"))
# The Most 21 Frequent Words in Negative Tweets
negative %>%
mutate(word = reorder(word, freq)) %>%
ggplot(aes(x = word, y = freq)) +
geom_col(fill = getPalette(colourCount)) +
coord_flip()
#Intersection of positive and negative
intersect(negative$word, positive$word)
dataset %>%
filter(negativereason != "") %>%
ggplot(aes(x = negativereason)) +
geom_bar(fill = "tomato") +
theme(axis.text.x = element_text(angle=65, vjust=0.6))
#Most Frequent in Neutral
neutral <- tidy_dataset %>%
filter(airline_sentiment == "neutral")
neutral <- neutral %>%
count(word, sort = TRUE) %>%
rename(freq = n)
neutral <- neutral %>%
filter(!(word %in% list))
head(neutral, 21)
#Words for each Sentiment
totals <- tidy_dataset %>%
# Count by tweet id to find the word totals for tweet
count(tweet_id) %>%
# Rename the new column
rename(total_words = n)
totals <- dataset %>%
inner_join(totals, by = "tweet_id") %>%
select(tweet_id, total_words, airline_sentiment) %>%
arrange(desc(total_words))
totals <- head(totals, 20)
ggplot(totals, aes(x = airline_sentiment , y = total_words, fill = airline_sentiment)) +
geom_col() +
scale_fill_brewer(palette="Paired")
#Individual flights
text_df <- data_frame(line = 1:nrow(dataset), airline=dataset$airline, text = dataset$text )
grouped_text <- unnest_tokens(text_df,word, text)
##American wordcloud
grouped_text %>% filter(airline == 'American')%>%
anti_join(stop_words) %>%
count(word) %>%
with(wordcloud(word, n, max.words = 50))
##Delta Wordcloud
grouped_text %>% filter(airline == 'Delta')%>%
anti_join(stop_words) %>%
count(word) %>%
with(wordcloud(word, n, max.words = 50))
##Southwest WordCloud
grouped_text %>% filter(airline == 'Southwest')%>%
anti_join(stop_words) %>%
count(word) %>%
with(wordcloud(word, n, max.words = 50))
##United WordCloud
grouped_text %>% filter(airline == 'United')%>%
anti_join(stop_words) %>%
count(word) %>%
with(wordcloud(word, n, max.words = 50))
##US Airways WordCloud
grouped_text %>% filter(airline == 'US Airways')%>%
anti_join(stop_words) %>%
count(word) %>%
with(wordcloud(word, n, max.words = 50))
##Virgin America Word Cloud
grouped_text %>% filter(airline == 'Virgin America')%>%
anti_join(stop_words) %>%
count(word) %>%
with(wordcloud(word, n, max.words = 20))
#Confidence in negative reason
df %>%
filter(!is.na(negativereason)) %>%
group_by(negativereason) %>%
summarise(
mean_confidence = mean(negativereason_confidence, na.rm = TRUE)
) %>%
ggplot(aes(negativereason, mean_confidence))+
geom_bar(stat = "identity", color = "black", fill = "skyblue")+
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9))+
labs(x = "Negative Reasons", y = "Mean Negative Confidence")
#Negative reason Confidence for airlines
df %>%
filter(!is.na(negativereason)) %>%
group_by(airline, negativereason) %>%
summarise(
mean_confidence = mean(negativereason_confidence, na.rm = TRUE)
) %>%
ggplot(aes(negativereason, mean_confidence))+
geom_bar(stat = "identity", colour = "grey19", fill = "springgreen4")+
facet_wrap(~airline, ncol = 3)+
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9))+
labs(x = "Negative Reasons", y = "Mean Confidence in Negative Reasons")