Sentiment analysis of quotes about R in fortunes using the tidytext package.

library(dplyr)
library(fortunes)
library(tidytext)
library(ggplot2)
theme_set(theme_bw())

AFINN <- sentiments %>%
  filter(lexicon == "AFINN")

read.fortunes() %>%
  mutate(id = row_number()) %>%
  unnest_tokens(word, quote) %>%
  anti_join(stop_words, by = "word") %>%
  inner_join(AFINN, by = "word") %>%
  group_by(author) %>%
  summarize(words = n(),
            quotes = n_distinct(id),
            sentiment = mean(score)) %>%
  filter(quotes >= 5) %>%
  mutate(author = reorder(author, sentiment)) %>%
  ggplot(aes(author, sentiment, fill = sentiment > 0)) +
  geom_bar(stat = "identity", show.legend = FALSE) +
  ylab("Average AFINN sentiment") +
  ggtitle("Sentiment by author in the 'fortunes' package") +
  coord_flip()