I like word clouds because they are visually appealing and provide a ton of information in a small space. Ever since I saw Drew Conway’s post (LINK) I have been looking for ways to improve word clouds. One of the nice feature’s of Drew’s post was that he colored the words according to the gradient. Unfortunately, Drew’s cloud lacks some of the aesthetic wow factor that Ian Fellow’s wordcloud package is known for.
This post is going to show you how to color words with a gradient based on degree of usage between two individuals. For me it’s going to help me learn the following things:
How to use knitr + markdown to make a blog post (I’ve been using knitr for reproducible latex/beamer reports.
How to use gradients in base (i.e. outside of ggplot2 that I’ve come to depend on).
How to make a gradient color bar in base.
Installing and Loading qdap and wordcloud
First you’ll need some packages to get started. I’m using my own beta package qdap plus Fellow’s wordcloud packages. If you download qdapwordcloud is part of the install. For the legend we’ll be using the plotrix package.
Reading in data
Now we’ll need some data. I happen to have presidential debate data (debate # 1) left over that we can still mine.Kn
Setting Up the Data
Make a word frequency matrix
Remove Lehrer’s words
Scale the word usage
Create a binned fill variable
Convert the Binned Variable to Colors
I was not sure how to produce gradients outside of ggplot2 and so I asked on stackoverflow.com and received a terrific and simple answer (LINK). Now we’ll create a color column based on the fill.var using qdap’s lookup that uses an environment to recode.
Plot the Word Cloud and Gradient Legend
Now that we have color gradients let’s use wordcloud to plot and plotrix’s color.legend to make a legend.
Alright, this is my first knitr generated blog post. Very easy. I regret not having tried it earlier :(
I accomplished my goal of making a gradient word cloud and a gradient legend. The actual word cloud really isn’t that informative because there’s too many words and too many colors. In some situations this approach may be useful but in this one I don’t like it. Secondly, I used the blue to red theme because it plays to the political parties but in this visualization better contrasting colors would be more appropriate. Overall I don’t feel I was successful in presenting information better than Drew Conway’s post.