<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/awesome-twitter-word-clouds-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.littlemissdata.com/blog/wordclouds"> Little Miss Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> </p>
<p> <img data-image-dimensions="1368x564" data-image-focal-point="0.5,0.5" src="https://i0.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c5f090e2e72e10f206d43/1529634582254/Screen+Shot+2018-06-21+at+8.17.38+AM.png?w=456&ssl=1" alt="Screen Shot 2018-06-21 at 8.17.38 AM.png" data-load="false" data-type="image" data-image-id="5b2c5f090e2e72e10f206d43" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c5f090e2e72e10f206d43/1529634582254/Screen+Shot+2018-06-21+at+8.17.38+AM.png"></p>
<p> </p>
<p>We have officially kicked off summer of 2018!  Y’all know what that means right?  SUMMER OF DATA SCIENCE!!! Imagine it now, you could be soaking up the rays while harnessing your inner data science skills ALL. SUMMER. LONG!!</p>
<p><a rel="nofollow" href="https://www.jessemaegan.com/post/r4ds-june-challenge-summer-of-data-science-2018/" target="_blank">Summer of data science</a> (aka <a rel="nofollow" href="https://twitter.com/search?f=tweets&q=%23SoDS18&src=typd" target="_blank">#SoDS18 </a>on twitter) is a community created by <a rel="nofollow" href="https://twitter.com/BecomingDataSci" target="_blank">Data Science Renee</a> to support and promote learning new data science skills in a collaborative environment.  The community releases a list of books to collectively tackle during the program.  To <a rel="nofollow" href="https://www.jessemaegan.com/post/r4ds-june-challenge-summer-of-data-science-2018/" target="_blank">participate</a>; you pick a book, work through it and collaborate in the social channels of your choice (slack, twitter etc).</p>
<p>For my goals, I decided to work through the book <a rel="nofollow" href="https://www.tidytextmining.com/" target="_blank">Tidy Text Mining with R</a> by <a rel="nofollow" href="https://twitter.com/juliasilge" target="_blank">Julia Silge</a> and <a rel="nofollow" href="https://twitter.com/drob" target="_blank">David Robinson</a>   I chose to tap into Twitter data for my text analysis using the <a rel="nofollow" href="https://github.com/mkearney/rtweet" target="_blank">rtweets</a> package.  Inspired by some of the word clouds in the Tidy Text book, I decided to plot the data in fancy word clouds using <a rel="nofollow" href="https://cran.r-project.org/web/packages/wordcloud2/vignettes/wordcloud.html" target="_blank">wordcloud2.</a></p>
<h2> </h2>
<h2>1) Install and Load the Libraries</h2>
<p>The first thing we need to do is install and load all required packages for our summertime fun work with R.  These are all of the install.packages() and library() commands below.  Note that some packages can be installed directly via CRAN and some need to be installed from github via the devtools package.  I wrote a blog on navigating various R package install issues <a rel="nofollow" href="https://www.littlemissdata.com/blog/r-package-install" target="_blank">here</a>. </p>
<pre>install.packages(&quot;rtweet&quot;)
install.packages(&quot;tidytext&quot;)
install.packages(&quot;dplyr&quot;)
install.packages(&quot;stringr&quot;)
require(devtools)
install_github(&quot;lchiffon/wordcloud2&quot;)

library(tidytext)
library(dplyr)
library(stringr)
library(rtweet)
library(wordcloud2)</pre>
<h2> </h2>
<h2><strong>2) API Authorization</strong></h2>
<p>Before we get rolling, we need some data.  Let’s tap into twitter using the rtweets package.  rtweet is an excellent package for pulling twitter data.  However, first you need to be authorized to pull from the twitter API.  To get my API credentials, I followed the processes documented in <a rel="nofollow" href="http://rtweet.info/articles/auth.html" target="_blank">rtweet reference article</a> and the <a rel="nofollow" href="https://github.com/mkearney/rtweet#api-authorization" target="_blank">github readme</a> page.  Following these steps, the process was pretty seamless.  I would only add that I had to uncheck “Enable Callback Locking” to establish my token in step 2.</p>
<h2><strong>3) Establish your token</strong></h2>
<p>Execute the following code to to establish your token.  As per the <a rel="nofollow" href="http://rtweet.info/articles/auth.html" target="_blank">rtweet reference article</a>, the <a rel="nofollow" href="http://rtweet.info/reference/create_token.html" target="_blank">create_token()</a> function automatically saves your token as an environment variable for future use.  </p>
<pre>create_token(
  app = &quot;PLACE YOUR APP NAME HERE&quot;,
  consumer_key = &quot;PLACE YOUR CONSUMER KEY HERE&quot;,
  consumer_secret = &quot;PLACE YOUR CONSUMER SECRET HERE&quot;)</pre>
<h2><strong>4) Gather Tweets</strong></h2>
<p>Use the search_tweets() function to grab 2000 tweets with the hashtag of your choice.  I chose to look into #HandmaidsTale because the show is AMAZING.  We will then look at the data collected from the tweets via the head() and dim() commands.  We will then use the unnest() command to transform the data to have only one word per row.  Next, we remove common words such as “the”, “it” etc via the stop_words data set.  Finally, we will filter out a custom set of words of our choice.</p>
<pre>#Grab tweets - note: reduce to 1000 if it's slow
hmt &lt;- search_tweets(
  &quot;#HandmaidsTale&quot;, n = 2000, include_rts = FALSE
)

#Look at tweets
head(hmt)
dim(hmt)
hmt$text

#Unnest the words - code via <a rel="nofollow" href="https://www.tidytextmining.com/tidytext.html" target="_blank">Tidy Text</a>
hmtTable &lt;- hmt %>% 
  unnest_tokens(word, text)

#remove stop words - aka typically very common words such as &quot;the&quot;, &quot;of&quot; etc
data(stop_words)
hmtTable &lt;- hmtTable %>%
  anti_join(stop_words)

#do a word count
hmtTable &lt;- hmtTable %>%
  count(word, sort = TRUE) 
hmtTable 

#Remove other nonsense words
hmtTable &lt;-hmtTable %>%
  filter(!word %in% c('t.co', 'https', 'handmaidstale', &quot;handmaid's&quot;, 'season', 'episode', 'de', 'handmaidsonhulu',  'tvtime', 
                      'watched', 'watching', 'watch', 'la', &quot;it's&quot;, 'el', 'en', 'tv',
                      'je', 'ep', 'week', 'amp'))
</pre>
<p> </p>
<h2><strong>5) Create a Word Cloud</strong></h2>
<p>Use the wordcloud2() function out of the box to create a wordcloud on the #HandmaidsTale tweets.</p>
<pre>wordcloud2(hmtTable, size=0.7)
</pre>
<p> </p>
<p> <img data-image-dimensions="1704x1104" data-image-focal-point="0.5,0.5" src="https://i1.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c6cfc575d1f4d46436c66/1529638160268/Screen+Shot+2018-06-21+at+10.27.39+PM.png?w=456&ssl=1" alt="Screen Shot 2018-06-21 at 10.27.39 PM.png" data-load="false" data-type="image" data-image-id="5b2c6cfc575d1f4d46436c66" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c6cfc575d1f4d46436c66/1529638160268/Screen+Shot+2018-06-21+at+10.27.39+PM.png"></p>
<p> </p>
<h2><strong>6) Create a Better Word Cloud</strong></h2>
<p>That word cloud was nice, but I think we can do better!  We need a color palette that suits the show and we would like a more suitable shape.  To do this we create a list of hex codes for our redPalette.  We then use the download.file() function to download some symbols from my github.  Note that you can also very easily create the word cloud shape by referencing local files.  Finally we create the word cloud via the wordcloud2() function and we set the shape with the figPath attribute.  </p>
<pre>#Create Palette
redPalette &lt;- c(&quot;#5c1010&quot;, &quot;#6f0000&quot;, &quot;#560d0d&quot;, &quot;#c30101&quot;, &quot;#940000&quot;)
redPalette

#Download images for plotting
url = &quot;https://raw.githubusercontent.com/lgellis/MiscTutorial/master/twitter_wordcloud/handmaiden.jpeg&quot;
handmaiden &lt;- &quot;handmaiden.jpg&quot;
download.file(url, handmaiden) # download file
url = &quot;https://raw.githubusercontent.com/lgellis/MiscTutorial/master/twitter_wordcloud/resistance.jpeg&quot;
resistance &lt;- &quot;resistance.jpeg&quot;
download.file(url, resistance) # download file

#plots
wordcloud2(hmtTable, size=1.6, figPath = handmaiden, color=rep_len( redPalette, nrow(hmtTable) ) )
wordcloud2(hmtTable, size=1.6, figPath = resistance, color=rep_len( redPalette, nrow(summary4) ) )
wordcloud2(hmtTable, size=1.6, figPath = resistance, color=&quot;#B20000&quot;)</pre>
<p> </p>
<h2>7) Marvel at your amazing word cloud</h2>
<h2> </h2>
<p>Yes, you have done it!  You’ve made a pretty sweet word cloud about an epic tv show.  Word clouds with custom symbols are a great way to make an immediate impact on your audience.  </p>
<p> </p>
<p> <img data-image-dimensions="1522x1364" data-image-focal-point="0.5,0.5" src="https://i2.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c61e66d2a739de292a3ac/1529635324966/Screen+Shot+2018-06-21+at+8.25.39+AM.png?w=456&ssl=1" alt="Screen Shot 2018-06-21 at 8.25.39 AM.png" data-load="false" data-type="image" data-image-id="5b2c61e66d2a739de292a3ac" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c61e66d2a739de292a3ac/1529635324966/Screen+Shot+2018-06-21+at+8.25.39+AM.png"></p>
<p>   </p>
<p> <img data-image-dimensions="1490x1350" data-image-focal-point="0.5,0.5" src="https://i0.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c632b6d2a739de292ccb2/1529635653394/Screen+Shot+2018-06-21+at+9.45.06+PM.png?w=456&ssl=1" alt="Screen Shot 2018-06-21 at 9.45.06 PM.png" data-load="false" data-type="image" data-image-id="5b2c632b6d2a739de292ccb2" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c632b6d2a739de292ccb2/1529635653394/Screen+Shot+2018-06-21+at+9.45.06+PM.png"></p>
<p>   </p>
<h2>8) Go wild on word clouds</h2>
<p>Now that you’ve learned this trick, it’s time to unleash your new #SoDS18 powers on the subjects of your choice! In keeping with the TV theme, I can’t forget Westworld.  Below are a few more Westworld visualizations I created following similar steps to above. All code is available in my github repo.</p>
<p> <img data-image-dimensions="2192x1366" data-image-focal-point="0.5,0.5" src="https://i1.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c636af950b7e7eeeadff6/1529635704898/Screen+Shot+2018-06-19+at+11.33.27+PM+1.png?w=456&ssl=1" alt="Screen Shot 2018-06-19 at 11.33.27 PM 1.png" data-load="false" data-type="image" data-image-id="5b2c636af950b7e7eeeadff6" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c636af950b7e7eeeadff6/1529635704898/Screen+Shot+2018-06-19+at+11.33.27+PM+1.png"></p>
<p>   </p>
<p> <img data-image-dimensions="1086x1137" data-image-focal-point="0.5,0.5" src="https://i2.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c71f70e2e72e10f22c5d2/1529639437438/Screen+Shot+2018-06-19+at+8.32.07+AM.png?w=456&ssl=1" alt="Screen Shot 2018-06-19 at 8.32.07 AM.png" data-load="false" data-type="image" data-image-id="5b2c71f70e2e72e10f22c5d2" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b2c71f70e2e72e10f22c5d2/1529639437438/Screen+Shot+2018-06-19+at+8.32.07+AM.png"></p>
<p>  </p>
<h2>Thank You</h2>
<p>Thanks for reading along while we explored twitter data, the beginnings of text analysis and cool word clouds.  Please share your thoughts and creations with me on <a rel="nofollow" href="https://twitter.com/LittleMissData" target="_blank">twitter</a>. </p>
<p>Note that the full code is available on my <a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/twitter_wordcloud" target="_blank"> </a><a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/twitter_wordcloud" target="_blank">github</a><a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/twitter_wordcloud" target="_blank"> repo</a><a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/Heatmap" target="_blank">.</a>  If you have trouble downloading the file from github, go to the <a rel="nofollow" href="https://github.com/lgellis/MiscTutorial" target="_blank">main page of the repo</a> and select “Clone or Download” and then “Download Zip”.</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="https://www.littlemissdata.com/blog/wordclouds"> Little Miss Data</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/TUlQK6jzTnM" height="1"></a></div>