library(httr)
library(plyr)
library(ggplot2)
library(ggthemes)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(magrittr)
library(tidytext)
library(wordcloud)
## Loading required package: RColorBrewer
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:magrittr':
## 
##     extract
library(magrittr)
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
#library(widyr)
library(stringr)
library(twitteR)
## 
## Attaching package: 'twitteR'
## The following objects are masked from 'package:dplyr':
## 
##     id, location
## The following object is masked from 'package:plyr':
## 
##     id
library(ROAuth)
options(httr_oauth_cache = T)

api_key <- "kdOswHOnGOZncbfdIaCiFaU0k"
api_secret <- "vwYaDD2L5mtGbOW13RVVjnL6hcfLza9gMV3U2AZUQGQdcBpANp"
access_token <- "976616536625397760-EnZZ9J2zIvAPlLPreYRm0PXGzHV4r6z"
access_token_secret <- "3tf7ihrb7G53xpiEkVC4zfA0wclvBiNHypr3bvTTH9lCE"



setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
## [1] "Using direct authentication"
tmobile.tweets = searchTwitter(searchString = 'TMobile', lang="en", n = 5000)



tweets_DF <- twListToDF(tmobile.tweets)
table(tweets_DF$isRetweet)
## 
## FALSE  TRUE 
##  3324  1676
tmobiletext <- tweets_DF %>%
                     unnest_tokens(word, text)
data("stop_words")
data(stop_words)
custom_stop_words <- bind_rows(
                    data_frame(word = c("https","rt","t.co", "http"), 
                              lexicon = c("custom")), 
                              stop_words)

tmobiletext <- tmobiletext %>%
             anti_join(custom_stop_words, by="word")
#summary(tweets_DF)
tmobiletext %>%
          filter(isRetweet == FALSE)%>%
          count(word, sort = TRUE) %>%
          filter(n > 50) %>%
          mutate(word = reorder(word, n)) %>%
          ggplot(aes(word, n)) +
                  geom_col(fill="yellow") +
                        coord_flip()+
                  theme_light()+
                  theme(text = element_text(size=10))+
                  geom_text(aes(x=word, y=n, label = n),
                      check_overlap = FALSE, size=3, hjust=1)

tmobiletext %>%
  inner_join(get_sentiments("nrc")) %>%
  count(sentiment, sort = TRUE) %>%
  ungroup()%>%
  group_by(sentiment) %>%
  ungroup() %>%
  mutate(sentiment = reorder(sentiment, n))%>%
  ggplot(aes(sentiment, n)) +
      geom_col(fill = "yellow", show.legend = T) +
          labs(y = "Number of Occurences",x = "Emotions") +
  coord_flip()+
      geom_text(aes(x=sentiment, y=n, label = n),
                    check_overlap = FALSE, 
                    size=3, 
                    angle=0, 
                    hjust=1.5)+
      theme_light()+    
      theme(text = element_text(size=14))
## Joining, by = "word"

.libPaths("C:/Users/dev/Documents/R/win-library3.5")
tmobiletext %>%
  inner_join(get_sentiments("nrc")) %>%
  count(word, sentiment, sort = TRUE) %>%
  ungroup()%>%
  group_by(sentiment) %>%
  top_n(10) %>%
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = sentiment)) +
      geom_col(show.legend = FALSE) +
          facet_wrap(~sentiment, scales = "free_y", nrow=4) +
          labs(y = "Number of Occurences",
          x = NULL) +
      coord_flip()
## Joining, by = "word"
## Selecting by n

 tmobiletext %>%
  inner_join(get_sentiments("bing")) %>%
  count(word, sentiment, sort = TRUE) %>%
  ungroup()%>%
  group_by(sentiment) %>%
  top_n(10) %>%
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = sentiment)) +
      geom_col(show.legend = FALSE) +
          facet_wrap(~sentiment, scales = "free_y") +
          labs(y = "Number of Occurences",
          x = NULL) +
      coord_flip()+
      theme_light()+
      geom_text(aes(x=word, y=n, label = n),
                    check_overlap = FALSE, size=3, angle=0, hjust=1)+
                    theme(text = element_text(size=16))
## Joining, by = "word"
## Selecting by n

library(reshape2)
tmobiletext %>%
  inner_join(get_sentiments("bing")) %>%
  count(word, sentiment, sort = TRUE) %>%
  ungroup()%>%
  group_by(sentiment) %>%
  top_n(100) %>%
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  acast(word ~ sentiment, value.var = "n", fill = 0)%>%
  comparison.cloud(colors = c("#F8766D", "#00BFC4"),
                   max.words = 100)
## Joining, by = "word"
## Selecting by n