#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)

Convert to data frame

TarheelsComments_df <- data.frame(comment = TarheelsComments\(comments\)comment, score = TarheelsComments\(comments\)score, date = as.POSIXct(TarheelsComments\(comments\)date, origin = “1970-01-01”, tz = “UTC”))

Save as CSV file

write.csv(TarheelsComments_df, “C:/Users/venne/OneDrive/Desktop/TarheelsComments_df.csv”, row.names = FALSE) TarheelsComments\(comments\)comment <- enc2utf8(TarheelsComments\(comments\)comment)

Save TarheelsComments as a CSV file

write.csv(TarheelsComments\(comments\)comment,“C:/Users/venne/OneDrive/Desktop/Tarheeldataset.csv”,row.names = FALSE)

Save TarheelsThreads as a CSV file

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)

Create a Corpus

corpus <- Corpus(VectorSource(Tarheeldataset$comment))

Preprocess the text in the corpus

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)

Create a document-term matrix

dtm <- DocumentTermMatrix(corpus)

Convert the document-term matrix to a matrix

m <- as.matrix(dtm)

Calculate word frequencies

word_freqs <- sort(rowSums(m), decreasing=TRUE)

Create a word cloud

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)

CHART 2 : FREQUENCY OF WORDS

Create a Corpus

corpus <- Corpus(VectorSource(Tarheeldataset$comment))

Preprocess the text in the corpus

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)

Create a document-term matrix

dtm <- DocumentTermMatrix(corpus)

Convert the document-term matrix to a matrix

m <- as.matrix(dtm)

Calculate word frequencies

word_freqs <- sort(rowSums(m), decreasing=TRUE)

Display the top 20 words and their frequencies

top_words <- data.frame(word = names(word_freqs)[1:20], frequency = word_freqs[1:20]) print(top_words)

CHART 2: Frequency of Words (Bar Plot)

library(ggplot2)

Display the top N words in the bar plot (adjust N based on your preference)

top_n_words <- 20 top_words <- data.frame(word = names(word_freqs)[1:top_n_words], frequency = word_freqs[1:top_n_words])

Create a bar plot

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)

CHART - 3 HEXPLOT BETWEEN SENTIMENT AND COMMENT SCORE

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 )

CHART - 4 SCATTER PLOT OF SENTIMENT BY WORD COUNT

ggplot(Tarheeldataset, aes(x = WordCount, y = SentimentGI)) + geom_point() + geom_smooth(method = “lm”) + labs(title = “Sentiment by Word Count”)