<div>Lots of fun projects in the works, so today’s post will be short – a demonstration on how to create wordclouds, both with and without sentiment analysis results. While I could use song lyrics again, I decided to use a different dataset that comes with the quanteda packages: all 58 Inaugural Addresses, from Washington’s first speech in 1789 to Trump’s in 2017.
<div>
<div>
<div>
<pre><span>library</span><span>(quanteda)</span> <span>#install with install.packages(&quot;quanteda&quot;) if needed</span><br></pre>
</div>
<div>
<pre><span>data</span><span>(data_corpus_inaugural)</span><br><span>speeches</span> <span>&lt;-</span> <span>data_corpus_inaugural</span><span>$</span><span>documents</span><br><span>row.names</span><span>(speeches)</span> <span>&lt;-</span> <span>NULL</span><br></pre>
</div>
</div>
</div>
<p>As you can see, this dataset has each Inaugural Address in a column called “texts,” with year and President’s name as additional variables. To do analysis on the words in speeches, and generate a wordcloud, we’ll want to unnest the words in the texts column.</p>
<div>
<div>
<div>
<pre><span>library</span><span>(tidytext)</span><br><span>library</span><span>(tidyverse)</span><br></pre>
</div>
<div>
<pre><span>speeches_tidy</span> <span>&lt;-</span> <span>speeches</span> <span>%>%</span><br>  <span>unnest_tokens</span><span>(word, texts)</span> <span>%>%</span><br>  <span>anti_join</span><span>(stop_words)</span><br></pre>
</div>
<div>
<pre>## Joining, by = &quot;word&quot;<br></pre>
</div>
</div>
</div>
<p>For our first wordcloud, let’s see what are the most common words across all speeches.</p>
<div>
<div>
<div>
<pre><span>library</span><span>(wordcloud)</span> <span>#install.packages(&quot;wordcloud&quot;) if needed</span><br></pre>
</div>
<div>
<pre><span>speeches_tidy</span> <span>%>%</span><br>  <span>count</span><span>(word,</span> <span>sort</span> <span>=</span> <span>TRUE</span><span>)</span> <span>%>%</span><br>  <span>with</span><span>(</span><span>wordcloud</span><span>(word, n,</span> <span>max.words</span> <span>=</span> <span>50</span><span>))</span><br></pre>
</div>
</div>
<div>
<div><a rel="nofollow" href="https://i2.wp.com/3.bp.blogspot.com/-z7PnU0ZByRI/WxyIFAUVUdI/AAAAAAAALtg/2jXj3Nz5mxouFdWvgkayyMyXcVu0817UgCLcBGAs/s1600/unnamed-chunk-3-1.png?ssl=1" target="_blank"><img border="0" src="https://i2.wp.com/3.bp.blogspot.com/-z7PnU0ZByRI/WxyIFAUVUdI/AAAAAAAALtg/2jXj3Nz5mxouFdWvgkayyMyXcVu0817UgCLcBGAs/s320/unnamed-chunk-3-1.png?resize=320%2C320&ssl=1" data-original-width="450" width="320" data-original-height="504" data-recalc-dims="1" height="320"></a></div>
</div>
</div>
<p>While the language used by Presidents certainly varies by time period and the national situation, these speeches refer often to the people and the government; in fact, most of the larger words directly reference the United States and Americans. The speeches address the role of “president” and likely the “duty” that role entails. The word “peace” is only slightly larger than “war,” and one could probably map out which speeches were given during wartime and which weren’t.</p>
<p>We could very easily create a wordcloud for one President specifically. For instance, let’s create one for Obama, since he provides us with two speeches worth of words. But to take things up a notch, let’s add sentiment information to our wordcloud. To do that, we’ll use the comparison.cloud function; we’ll also need the reshape2 library.</p>
<div>
<div>
<div>
<pre><span>library</span><span>(reshape2)</span> <span>#install.packages(&quot;reshape2&quot;) if needed</span><br></pre>
</div>
<div>
<pre><span>obama_words</span> <span>&lt;-</span> <span>speeches_tidy</span> <span>%>%</span><br>  <span>filter</span><span>(President</span> <span>==</span> <span>&quot;Obama&quot;</span><span>)</span> <span>%>%</span><br>  <span>count</span><span>(word,</span> <span>sort</span> <span>=</span> <span>TRUE</span><span>)</span><br><br><span>obama_words</span> <span>%>%</span><br>  <span>inner_join</span><span>(</span><span>get_sentiments</span><span>(</span><span>&quot;nrc&quot;</span><span>)</span> <span>%>%</span><br>               <span>filter</span><span>(sentiment</span> <span>%in%</span> <span>c</span><span>(</span><span>&quot;positive&quot;</span><span>,</span><br>                                       <span>&quot;negative&quot;</span><span>)))</span> <span>%>%</span><br>  <span>filter</span><span>(n</span> <span>></span> <span>1</span><span>)</span> <span>%>%</span><br>  <span>acast</span><span>(word</span> <span>~</span> <span>sentiment,</span> <span>value.var</span> <span>=</span> <span>&quot;n&quot;</span><span>,</span> <span>fill</span> <span>=</span> <span>0</span><span>)</span> <span>%>%</span><br>  <span>comparison.cloud</span><span>(</span><span>colors</span> <span>=</span> <span>c</span><span>(</span><span>&quot;red&quot;</span><span>,</span><span>&quot;blue&quot;</span><span>))</span><br></pre>
</div>
<div>
<pre>## Joining, by = &quot;word&quot;<br></pre>
</div>
</div>
<div>
<div><a rel="nofollow" href="https://i2.wp.com/2.bp.blogspot.com/-K7S5kqCS2kc/WxyINVNEJZI/AAAAAAAALtk/qsKbECEwsn8KEsYDlYy4YqXWtjeR-RXcACLcBGAs/s1600/unnamed-chunk-4-1.png?ssl=1" target="_blank"><img border="0" src="https://i0.wp.com/2.bp.blogspot.com/-K7S5kqCS2kc/WxyINVNEJZI/AAAAAAAALtk/qsKbECEwsn8KEsYDlYy4YqXWtjeR-RXcACLcBGAs/s320/unnamed-chunk-4-1.png?resize=320%2C320&ssl=1" data-original-width="450" width="320" data-original-height="504" data-recalc-dims="1" height="320"></a></div>
</div>
</div>
<p>The acast statement reshapes the data, putting our sentiments of positive and negative as separate columns. Setting fill = 0 is important, since a negative word will be missing a value for the positive column and vice versa; without fill = 0, it would drop any row with NA in one of the columns (which would be every word in the set). As a sidenote, we could use the comparison cloud to compare words across two documents, such as comparing two Presidents. The columns would be counts for each President, as opposed to count by sentiment.</p>
<p>Interestingly, the NRC classifies “government” and “words” as negative. But even if we ignore those two words, which are Obama’s most frequent, the negatively-valenced words are much larger than most of his positively-valenced words. So while he uses many more positively-valenced words than negatively-valenced words – seen by the sheer number of blue words – he uses the negatively-valenced words more often. If you were so inclined, you could probably run a sentiment analysis on his speeches and see if they tend to be more positive or negative, and/or if they follow arcs of negativity and positivity. And feel free to generate your own wordcloud: all you’d need to do is change the filter(President == “”) to whatever President you’re interested in examining (or whatever text data you’d like to use, if President’s speeches aren’t your thing). </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.deeplytrivial.com/2018/06/statistics-sunday-creating-wordclouds.html"> Deeply Trivial</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/keP7twLR-AQ" height="1"></a></div></div>