#install.packages(“RedditExtractoR”)
#install.packages(“SentimentAnalysis”)
#install.packages(“SnowballC”)
#install.packages(“ggpmisc”)
#install.packages(“tm”) #install.packages(“wordcloud”) library(dplyr) library(ggplot2) library(ggpmisc) library(RedditExtractoR) library(SentimentAnalysis) library(SnowballC) #getting recent subreddit comments for sports TarheelsThreads<- find_thread_urls(subreddit=“tarheels”,sort_by=“new”,period=“all”) TarheelsComments<- get_thread_content(TarheelsThreads$url)
TarheelsComments_df <- data.frame(comment = TarheelsComments\(comments\)comment, score = TarheelsComments\(comments\)score, date = as.POSIXct(TarheelsComments\(comments\)date, origin = “1970-01-01”, tz = “UTC”))
write.csv(TarheelsComments_df, “C:/Users/venne/OneDrive/Desktop/TarheelsComments_df.csv”, row.names = FALSE) TarheelsComments\(comments\)comment <- enc2utf8(TarheelsComments\(comments\)comment)
write.csv(TarheelsComments\(comments\)comment,“C:/Users/venne/OneDrive/Desktop/Tarheeldataset.csv”,row.names = FALSE)
write.csv(TarheelsComments$comments,“C:/Users/venne/OneDrive/Desktop/Tarheelsentiment.csv”, row.names = FALSE)
ggplot(Tarheeldataset, aes(x=SentimentGI)) + geom_histogram() + labs(title=“Sentiment for Tarheels Subreddit”)
#CHART - 1 : WORD CLOUD library(wordcloud) library(tm)
corpus <- Corpus(VectorSource(Tarheeldataset$comment))
corpus <- tm_map(corpus, content_transformer(tolower)) corpus <- tm_map(corpus, removePunctuation) corpus <- tm_map(corpus, removeNumbers) corpus <- tm_map(corpus, removeWords, stopwords(“en”)) corpus <- tm_map(corpus, stripWhitespace)
dtm <- DocumentTermMatrix(corpus)
m <- as.matrix(dtm)
word_freqs <- sort(rowSums(m), decreasing=TRUE)
wordcloud(words = names(word_freqs), freq = word_freqs, min.freq = 10, scale=c(3,0.5), colors=brewer.pal(8, “Dark2”), random.order=FALSE, max.words=100, rot.per=0.35)
corpus <- Corpus(VectorSource(Tarheeldataset$comment))
corpus <- tm_map(corpus, content_transformer(tolower)) corpus <- tm_map(corpus, removePunctuation) corpus <- tm_map(corpus, removeNumbers) corpus <- tm_map(corpus, removeWords, stopwords(“en”)) corpus <- tm_map(corpus, stripWhitespace)
dtm <- DocumentTermMatrix(corpus)
m <- as.matrix(dtm)
word_freqs <- sort(rowSums(m), decreasing=TRUE)
top_words <- data.frame(word = names(word_freqs)[1:20], frequency = word_freqs[1:20]) print(top_words)
library(ggplot2)
top_n_words <- 20 top_words <- data.frame(word = names(word_freqs)[1:top_n_words], frequency = word_freqs[1:top_n_words])
ggplot(top_words, aes(x = word, y = frequency)) + geom_bar(stat = “identity”, fill = “steelblue”) + labs(title = “Top Words and Their Frequencies”, x = “Word”, y = “Frequency”) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + geom_text(aes(label = frequency), vjust = -0.5, size = 3)
ggplot(Tarheeldataset, aes(x=score, y=SentimentGI)) + geom_hex(bins = 20, color = “white”) + scale_fill_gradient(low = “#00afbb”, high = “#fc4e07”) + labs(title =“Sentiment by Comment Score”) + geom_smooth(method = lm, formula = y ~ poly(x, 2)) + theme_minimal() + stat_poly_eq( aes(label = paste(..eq.label.., ..adj.rr.label.., sep = “~~~~”)), formula = y ~ poly(x, 2), parse = TRUE )
ggplot(Tarheeldataset, aes(x = WordCount, y = SentimentGI)) + geom_point() + geom_smooth(method = “lm”) + labs(title = “Sentiment by Word Count”)