Created
May 7, 2018 18:18
-
-
Save imjakedaniels/a3e5543c679e06c2121d9a72c9482c0f to your computer and use it in GitHub Desktop.
Scraped 5000 Tweets from the #MentalHealthWeek to find the highest frequency of words used.
This file contains hidden or 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
```{r} | |
#https://cran.r-project.org/web/packages/rtweet/rtweet.pdf | |
# install from CRAN | |
install.packages("rtweet") | |
install.packages("httpuv") | |
install.packages("tidyverse") | |
install.packages("dplyr") | |
install.packages("tidytext") | |
# load rtweet | |
library(rtweet) | |
library(httpuv) | |
library(tidyverse) | |
library(lubridate) | |
library(ggplot2) | |
library(dplyr) | |
library(readr) | |
library(tidytext) | |
data("stop_words") | |
``` | |
```{r} | |
## setting my permissions, use your own keys here | |
appname <- "xxxxxxxxxxxx" | |
## api key (example below is not a real key) | |
key <- "xxxxxxxxxxxx" | |
## api secret (example below is not a real key) | |
secret <- "xxxxxxxxxxxxxx" | |
## create token named "twitter_token" | |
twitter_token <- create_token( | |
app = appname, | |
consumer_key = key, | |
consumer_secret = secret) | |
``` | |
```{r} | |
tw <- search_tweets("#MentalHealthWeek", n=5000, type = "mixed", include_rts = F) | |
custom_stop_words <- bind_rows(data_frame(word = c("t.co", "https", "it's", "7", "13", "mentalhealthweek", "amp", "cmha_ntl", "1"), | |
lexicon = c("custom")), stop_words) | |
tidy_mental <- tw %>% | |
unnest_tokens(word, text) %>% | |
anti_join(custom_stop_words) %>% | |
select(word, favorite_count, retweet_count) %>% | |
group_by(word) %>% | |
count(word, sort = T) %>% | |
head(25) %>% | |
ggplot(aes(x = reorder(word, n), y = n, fill = "blue")) + | |
geom_bar(show.legend = F, stat = "identity", aes(fill = "blue")) + | |
labs(y = "Number of Mentions", x = NULL) + | |
ggtitle("Based on 5,000 Tweets from #MentalHealthWeek") + | |
coord_flip() | |
tidy_mental | |
``` |
Author
imjakedaniels
commented
May 7, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment