Home » blog » Building A Word Cloud Using R

Building A Word Cloud Using R

In this article, I will show you how to use text data to build word clouds in R. We will use a dataset containing around 25 comments on NPS score (The Net Promoter Score is an index ranging from 1 to 5 that measures the willingness of customers to recommend a company’s products or services to others). The dataset can be downloaded here.

library(tm) # Text Mining package
library(SnowballC) # Porter's word stemming algorithm
library(wordcloud) # Word cloud package
library(RColorBrewer) # Adding color to words

# Read the data
NPS_4_and_5 <- read.csv(‘E:\\Dropbox\\Training\\Blogs\\Word Cloud Using R\\NPS_Score_4_and_5.csv’, stringsAsFactors = F)

dim(NPS_4_and_5) # 25 4

# Merging Response_1 and Response_2
NPS_4_and_5$Response <-
paste0(NPS_4_and_5$Response_1, ‘. ‘, NPS_4_and_5$Response_2)

# Create a corpus and convert it to plain text
corpus = Corpus(VectorSource(NPS_4_and_5$Response))
corpus = tm_map(corpus, PlainTextDocument)

# Convert to lower-case
corpus = tm_map(corpus, tolower)

# Remove punctuation
corpus = tm_map(corpus, removePunctuation)

# Remove stopwords
stpWords <- read.csv(‘E:\\Dropbox\\Training\\Blogs\\Word Cloud Using R\\Other Stop Words.csv’, stringsAsFactors = F)
corpus = tm_map(corpus, removeWords, c(tolower(stpWords$Stop_Words), stopwords(“english”)))

# Stem document
corpus = tm_map(corpus, stemDocument)

# Creating word cloud
wordcloud(unlist(corpus), max.words = 60, random.order = F, colors=brewer.pal(8, “Dark2”))


Leave a Reply

Your email address will not be published. Required fields are marked *