R/wordcount_plot.R

Defines functions wordcount_plot

Documented in wordcount_plot

#' Plot Word Count
#'
#' The speech_tkn function receives as input a tidy tokenized tibble of text
#' from a speech, counts the number of occurences of each word
#'
#' @param speech_tkn tokenized speech object input
#' @return Horizontal bar graph that displays the most frequently occuring
#'   words, by frequency, in a tokenized collection of words
#' @export
#' @importFrom magrittr %>%
#' @import ggplot2
#' @import dplyr
#' @examples
#' url <- "https://er.jsc.nasa.gov/seh/ricetalk.htm"
#' title <- "Moon Speech"
#' author <- "John F. Kennedy"
#' # wordcount_plot(speech_tokens(url, title, author))
wordcount_plot <- function(speech_tkn){

  speech_tkn %>%
    count(.data$word, .data$title, .data$author) %>%
    top_n(10) %>%
    arrange(desc(n)) %>%
    mutate(word = factor(.data$word, levels = rev(unique(.data$word)))) %>%
    ggplot(aes(.data$word, n)) +
    geom_col(show.legend = FALSE) +
    coord_flip()
}
JCRascal/speechiespeech documentation built on Nov. 27, 2020, 5:18 p.m.