I am not sure if I can put here something reproducible, because I am working with text data. Basically I am trying to do sentiment analysis. The codes are below. Everything works fine to the end, until I use facet_wrap function.
library(tm)
install.packages("tm")
merkel.corpus <- Corpus(DirSource(directory = "C:/Users/Dell/Desktop/MERKEL"))
summary(merkel.corpus)
merkel_clean_corpus <- tm_map(merkel.corpus, tolower)
merkel_clean_corpus <- tm_map(merkel_clean_corpus, removeNumbers)
merkel_clean_corpus <- tm_map(merkel_clean_corpus, removePunctuation)
merkel_clean_corpus <- tm_map(merkel_clean_corpus, removeWords, stopwords())
merkel_clean_corpus <- tm_map(merkel_clean_corpus, stemDocument)
merkel_clean_corpus
#convert the TDM into tidytext
library(tidytext)
merkel <- TermDocumentMatrix(merkel_clean_corpus)
merkel2 <- tidy(merkel)
merkel2
library(tidytext)
library(dplyr)
#merkel2
A tibble: 4,757 x 3
term document count
1 foldertypegener desktop.ini 1
2 mode desktop.ini 1
3 vid desktop.ini 1
4 viewstat desktop.ini 1
5 abandon PUT.txt 2
6 abil PUT.txt 1
7 abl PUT.txt 8
8 abm PUT.txt 1
9 abroad PUT.txt 3
10 absolut PUT.txt 6
#and i produced sentiments
merkel_sentiments <- merkel2 %>%
inner_join(get_sentiments("bing"), by = c(term = "word"))
merkel_sentiments
library(ggplot2)
merkel_sentiments %>%
count(sentiment, term, wt = count) %>%
ungroup() %>%
filter(n >= 7) %>%
mutate(n = ifelse(sentiment == "negative", -n, n)) %>%
mutate(term = reorder(term, n)) %>%
ggplot(aes(term, n, fill = sentiment)) +
geom_bar(stat = "identity") +
labs(y= "contribution to sentiment", title = "Sentiment Analysis of Angela Merkel's speeches",
caption = "according to loughran dictionary") +
coord_flip()
Everything worked fine until here, but I have two texts within the dataframe under 'document' column :RTE.txt and PUT.txt, which is a chr column.
I want to facet_wrap based on this column to see the contribution to sentiment by the name of document (which are basically two speeches of Merkel) Please do not pay attention to the file names.
I hope you could advice based on this.
Thank you.