Book Image

R Data Visualization Cookbook

By : Gohil
Book Image

R Data Visualization Cookbook

By: Gohil

Overview of this book

If you are a data journalist, academician, student or freelance designer who wants to learn about data visualization, this book is for you. Basic knowledge of R programming is expected.
Table of Contents (12 chapters)
11
Index

Constructing a word cloud from a document


In the previous recipe, we studied a quick and easy way to generate a word cloud. In this recipe, we will learn how to create a word cloud using an entire document, such as a transcript of the complete inaugural speech by President Obama. We will also learn how to process the text and structure it using the text mining package.

Getting ready

To generate a word cloud and structure our data, we will use the following packages:

  • wordcloud

  • tm

How to do it…

We will start this recipe by installing and loading the required packages in R using the install.packages() and library() functions:

install.packages(c("wordcloud","tm"))
library(tm)
library(wordcloud)

The readLines() function allows us to read the file in R that contains our text. The obama.txt file should be saved in our current R directory:

file = readLines("obama.txt")

The text in our file is not well structured. The text file consists of punctuation, numbers, and stop words, which need to be cleaned...