R/wordcloud.R

Defines functions word_cloud

Documented in word_cloud

#' word_cloud
#'
#' Plot a word cloud from a document-term matrix.
#'
#' @param dtm A `DocumentTermMatrix` object.
#' @param n The maximum number of words to plot.
#' @param remove_stopwords Whether to remove stopwords appearing in a language-specific list
#'   (see [`tm::stopwords`]).
#' @param ... Additional arguments passed to [`wordcloud`][wordcloud::wordcloud].
#'
#' @examples
#'
#' file <- system.file("texts", "reut21578-factiva.xml", package="tm.plugin.factiva")
#' corpus <- import_corpus(file, "factiva", language="en")
#' dtm <- build_dtm(corpus)
#' word_cloud(dtm)
#'
#' @export
word_cloud <- function(dtm, n=50, remove_stopwords=TRUE, ...) {
  terms <- colnames(dtm)
  freqs <- col_sums(dtm)

  if(remove_stopwords) {
    stopwords <- terms %in% stopwords(attr(dtm, "language"))
    terms <- terms[!stopwords]
    freqs <- freqs[!stopwords]
  }
      
  wordcloud::wordcloud(terms, freqs, max.words=n,
                       random.order=FALSE, fixed.asp=TRUE, rot.per=0, ...)
}

Try the R.temis package in your browser

Any scripts or data that you put into this service are public.

R.temis documentation built on May 13, 2021, 1:08 a.m.