Last active
November 17, 2019 01:35
-
-
Save layik/17cb5e10caeb61189529606498a1d78c to your computer and use it in GitHub Desktop.
This file contains 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
packages <- c("rtweet", "stringr", "ggplot2", "tmap", | |
"dplyr", "textdata", "tm", "tidytext", | |
"sentimentr", "wordcloud") | |
if (length(setdiff(packages, rownames(installed.packages()))) > 0) { | |
install.packages(setdiff(packages, rownames(installed.packages())),repos='http://cran.us.r-project.org') | |
} | |
lapply(packages, library, character.only = TRUE) | |
# ?? | |
name <- "layik" | |
sa <- get_timeline(name, n = 3200) # default 3200 | |
#cleanup | |
sa$text = lapply(sa$text,tolower) | |
sa$text = gsub("[\r\n]","",sa$text) | |
sa$text = str_replace_all(sa$text,"[^[:graph:]]", " ") | |
sa$text = str_replace_all(sa$text, "http[[:alnum:][:punct:]]*", " ") | |
# afinn = as.data.frame(get_sentiments("afinn")) | |
s = sentiment(sa$text) | |
ggplot(s, aes(element_id, sentiment), fill = 'blue') + | |
geom_line(color = "blue") | |
p = unlist(lapply(s$sentiment, function(x){ | |
if(x > 0.5) return('Positive') | |
if(x > 0) return("Good") | |
if(x == 0) return('Neutral') | |
if(x < -0.5) return('Negative') | |
'Bad' | |
})) | |
bp<- ggplot(data.frame(table(p)), | |
aes(x="", y=Freq, fill=p))+ | |
geom_bar(width = 1, stat = "identity") + | |
xlab("") + ylab(mp) | |
pie <- bp + coord_polar("y", start=0) | |
pie |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment