question about facet wrap

Hi, I am relatively new to R and ggplot2. I am having problems with facet_wrap function. I appreciate your help in advance. Basically I am doing a sentiment analysis and when i use the below code, i produce a plot without any problem. But when i want to facet_wrap based on the variable document (which has two different docs), it gives error. I think it does because the variable type is a character, but when i change it to factor, it doesn't produce the plot.

merkel_sentiments %>%
count(sentiment, term, wt = count) %>%
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 bing dictionary") +
coord_flip()

As i said, this works fine, but how can I produce a plot with facet_wrap side by side based on the name of the document (or based on the variable `document')?
Thanks.

merkel_sentiments

A tibble: 339 x 4

term document count sentiment

1 accomplish PUT.txt 1 positive
2 afflict PUT.txt 1 negative
3 afford PUT.txt 2 positive
4 afraid PUT.txt 1 negative
5 aggressor PUT.txt 2 negative
6 angel PUT.txt 1 positive
7 attack PUT.txt 5 negative
8 award PUT.txt 2 positive
9 benefit PUT.txt 2 positive
10 best PUT.txt 2 positive

(The tibble has a second doc, which doesn't appear here.)

Hi!

To help us help you, could you please turn this into a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

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.

A small sample of the dataframe you are using for plotting would make things reproducible, also since your question is about plotting please narrow down your example to just the relevant part.

Thank you. But I guess I have to upload the text I am working with in the first place, so that you can reproduce. But I cannot upload the text here.
Let me simplify my question: How can I use facet_wrap function with a chr variable? The column, which I am trying to facet_wrap, is called "document", it's var type is chr, and it has two documents (4,747 rows).
If this is not clear, please just disregard. Wish. best

You should be able to do it without a problem, that is why I suspect the actual problem is with the structure of your dataset.

There is no need for uploading your text file, you just have to post a small sample of merkel_sentiments dataframe on a copy/paste friendly format, take a look at the guide I gave you on previous post to see how to do it.

Sorry for my tardy response. Thank you very much for your help.