-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNLPandClustering.R
143 lines (103 loc) · 5.64 KB
/
NLPandClustering.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
library(ggplot2)
library(readr)
library(tm)
library(SnowballC)
library(RColorBrewer)
library(wordcloud)
library(biclust)
library(cluster)
library(igraph)
library(fpc)
library(gridExtra)
library(cowplot)
library(reshape2)
library(scales)
library(NLP)
library(readxl)
Dataset <- read_excel("D:/sem4/A-SIN/PROJECT/Dataset.xlsx")
airline<-Dataset
str(airline)
#Histogram plot showing the number of tweets and sentiment each airline
airlineSentiment = as.data.frame(table(airline$airline,airline$airline_sentiment))
colnames(airlineSentiment) = c("Airline","Sentiment","Freq")
colours = c("firebrick1","deepskyblue","chartreuse3")
histPlot2 = ggplot(airlineSentiment) + aes(x=Airline,y=Freq,fill=Sentiment) + scale_fill_manual(values=c("indianred1","deepskyblue","chartreuse3"))
histPlot2 = histPlot2 + geom_bar(stat="identity")
histPlot2
# 2.1. Individual pie charts for each Airline
sentAmerican = subset(airlineSentiment, Airline == "American")
American = ggplot(sentAmerican) + aes(x="American", y=Freq, fill=Sentiment) +
geom_bar(stat="identity") +
coord_polar("y") +
theme(axis.text.x=element_text(color="black")) +
theme(axis.ticks=element_blank(), axis.title=element_blank(), axis.text.y=element_blank())
y.breaks = cumsum(sentAmerican$Freq) - sentAmerican$Freq/2
American = American + scale_y_continuous(breaks=y.breaks, labels=sentAmerican$Sentiment) +
ggtitle("American") + theme(plot.title = element_text(face="bold")) + scale_fill_manual(values=c("indianred1","deepskyblue","chartreuse3"))
# DELTA AIRLINES
sentDelta = subset(airlineSentiment, Airline == "Delta")
Delta = ggplot(sentDelta) + aes(x="Delta", y=Freq, fill=Sentiment) +
geom_bar(stat="identity") +
coord_polar("y") +
theme(axis.text.x=element_text(color="black")) +
theme(axis.ticks=element_blank(), axis.title=element_blank(), axis.text.y=element_blank())
y.breaks = cumsum(sentDelta$Freq) - sentDelta$Freq/2
Delta = Delta + scale_y_continuous(breaks=y.breaks, labels=sentDelta$Sentiment) +
ggtitle("Delta") + theme(plot.title = element_text(face="bold")) + scale_fill_manual(values=c("indianred1","deepskyblue","chartreuse3"))
# SOUTHWEST AIRLINES
sentSouthwest = subset(airlineSentiment, Airline == "Southwest")
Southwest = ggplot(sentSouthwest) + aes(x="Southwest", y=Freq, fill=Sentiment) +
geom_bar(stat="identity") +
coord_polar("y") +
theme(axis.text.x=element_text(color="black")) +
theme(axis.ticks=element_blank(), axis.title=element_blank(), axis.text.y=element_blank())
y.breaks = cumsum(sentSouthwest$Freq) - sentSouthwest$Freq/2
Southwest = Southwest + scale_y_continuous(breaks=y.breaks, labels=sentSouthwest$Sentiment) +
ggtitle("Southwest") + theme(plot.title = element_text(face="bold")) + scale_fill_manual(values=c("indianred1","deepskyblue","chartreuse3"))
# UNITED AIRLINES
sentUnited = subset(airlineSentiment, Airline == "United")
United = ggplot(sentUnited) + aes(x="United", y=Freq, fill=Sentiment) +
geom_bar(stat="identity") +
coord_polar("y") +
theme(axis.text.x=element_text(color="black")) +
theme(axis.ticks=element_blank(), axis.title=element_blank(), axis.text.y=element_blank())
y.breaks = cumsum(sentUnited$Freq) - sentUnited$Freq/2
United = United + scale_y_continuous(breaks=y.breaks, labels=sentUnited$Sentiment) +
ggtitle("United") + theme(plot.title = element_text(face="bold")) + scale_fill_manual(values=c("indianred1","deepskyblue","chartreuse3"))
# VIRGIN AMERICA
sentVAirways = subset(airlineSentiment, Airline == "Virgin America")
VAirways = ggplot(sentVAirways) + aes(x="Virgin America", y=Freq, fill=Sentiment) +
geom_bar(stat="identity") +
coord_polar("y") +
theme(axis.text.x=element_text(color="black")) +
theme(axis.ticks=element_blank(), axis.title=element_blank(), axis.text.y=element_blank())
y.breaks = cumsum(sentVAirways$Freq) - sentVAirways$Freq/2
VAirways = VAirways + scale_y_continuous(breaks=y.breaks, labels=sentVAirways$Sentiment) +
ggtitle("Virgin America") + theme(plot.title = element_text(face="bold")) + scale_fill_manual(values=c("indianred1","deepskyblue","chartreuse3"))
# US AIRWAYS
sentUSAirways = subset(airlineSentiment, Airline == "US Airways")
USAirways = ggplot(sentUSAirways) + aes(x="US Airways", y=Freq, fill=Sentiment) +
geom_bar(stat="identity") +
coord_polar("y") +
theme(axis.text.x=element_text(color="black")) +
theme(axis.ticks=element_blank(), axis.title=element_blank(), axis.text.y=element_blank())
y.breaks = cumsum(sentUSAirways$Freq) - sentUSAirways$Freq/2
USAirways = USAirways + scale_y_continuous(breaks=y.breaks, labels=sentUSAirways$Sentiment) +
ggtitle("US Airways") + theme(plot.title = element_text(face="bold")) + scale_fill_manual(values=c("indianred1","deepskyblue","chartreuse3"))
plot_grid(American,Delta,Southwest,United,VAirways,USAirways,ncol=2,nrow=3)
# 3. Initial Global analysis
table(airline$negativereason, airline$airline)
globalSentReasons = as.data.frame(table(airline$negativereason, airline$airline))
colnames(globalSentReasons) = c("Reason","Airline", "Freq")
ggplot(globalSentReasons) + aes(y = Freq, x = Reason, group = Airline, colour = Airline) + coord_polar() + geom_point() + geom_path() + labs(x = NULL)
#CALCULATE PERCENTAGES
aggregate(Freq ~ Airline, globalSentReasons, sum)
globalSentReasons$TotalTwAirline = 0
globalSentReasons[1:11,4] = 2759
globalSentReasons[12:22,4] = 2222
globalSentReasons[23:33,4] = 2420
globalSentReasons[34:44,4] = 3822
globalSentReasons[45:55,4] = 2913
globalSentReasons[56:66,4] = 503
globalSentReasons$PercentOfTotal = (globalSentReasons[,3]/globalSentReasons[,4])*100
ggplot(globalSentReasons) + aes(y = PercentOfTotal, x = Reason, group = Airline, colour = Airline) + coord_polar() + geom_point() + geom_path() + labs(x = NULL)