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