<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/enter-sadman/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.johnmackintosh.com/2018-02-03-enter-sadman/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Yet more Metalliplots</h2>
<p>I’m just going to tidy up some issues from my last post where I did some tidytext analysis on the legendary rockers – Metallica.</p>
<p>It was a fun way to pass some time and keep my hand in with text mining, something I still don’t seem to get around to doing in the day job.</p>
<p>I was pretty annoyed at not getting my faceted split of top terms by descending order to plot by actual descending order.</p>
<p>Attempt 1:</p>
<p><img alt="2018-01-28-Most-Common-Words-by-Album.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-28-Most-Common-Words-by-Album.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Many failed efforts later, I arrive at this:</p>
<p>Attempt 2 -getting better, but still not right. <a rel="nofollow" href="https://juliasilge.com/" target="_blank">Julia</a>, in the unlikely event you read this, WHAT am I doing wrong?</p>
<p><img alt="2018-02-03-Most-Common-Words-by-Album.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-02-03-Most-Common-Words-by-Album.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Enough of my failures (not enough time,folks) lets look at sentiment on a line by line basis, instead of just a net score by track. To do this, I used the <a rel="nofollow" href="https://cran.r-project.org/web/packages/sentimentr/index.html" target="_blank">sentimentr</a> package, by Tyler Rinker.</p>
<p>Stolen from the the package homepage on GitHub:<br>
“sentimentr is designed to quickly calculate text polarity sentiment at the sentence level and optionally aggregate by rows or grouping variable(s)”…“sentimentr attempts to take into account valence shifters (i.e., negators, amplifiers (intensifiers), de-amplifiers (downtoners), and adversative conjunctions) while maintaining speed.”</p>
<p>It was certainly fast, allowing me to take my original data frame of lyrics (grouped by album, track and line number) and calculate a sentiment score. I then set upper and lower bounds for each line so I could use geom_ribbon as follows:</p>
<div>
<div>
<pre><code><span>line_sentiment</span><span> </span><span>&lt;-</span><span> </span><span>sentimentr</span><span>::</span><span>sentiment</span><span>(</span><span>data</span><span>$</span><span>text</span><span>)</span><span>
</span><span>data2</span><span> </span><span>&lt;-</span><span> </span><span>data</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>title</span><span> </span><span>%notin%</span><span> </span><span>c</span><span>(</span><span>&quot;Am I Evil?&quot;</span><span>,</span><span> </span><span>&quot;(Anesthesia) - Pulling Teeth&quot;</span><span>))</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>linenumber</span><span> </span><span>=</span><span> </span><span>row_number</span><span>())</span><span> </span><span>%>%</span><span> </span><span>left_join</span><span>(</span><span>line_sentiment</span><span>,</span><span> </span><span>by</span><span>=</span><span>c</span><span>(</span><span>&quot;linenumber&quot;</span><span>=</span><span>&quot;element_id&quot;</span><span>))</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>Album</span><span>,</span><span>title</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>linenumber</span><span>=</span><span> </span><span>row_number</span><span>())</span><span> </span><span>%>%</span><span> </span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>max</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>sentiment</span><span> </span><span>></span><span> </span><span>0</span><span>,</span><span> </span><span>sentiment</span><span>,</span><span> </span><span>0</span><span>),</span><span>
</span><span>min</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>sentiment</span><span> </span><span></span><span> </span><span>0</span><span>,</span><span> </span><span>sentiment</span><span>,</span><span>0</span><span>))</span><span> </span><span>ggthemr</span><span>(</span><span>KEA</span><span>)</span><span>#custom palette</span><span>
</span><span>filter</span><span>(</span><span>data2</span><span>,</span><span>Album</span><span> </span><span>==</span><span>&quot;Kill_Em_All&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>linenumber</span><span>,</span><span>sentiment</span><span>),</span><span>fill</span><span> </span><span>=</span><span>title</span><span>,</span><span>colour</span><span>=</span><span>&quot;grey50&quot;</span><span>)</span><span>+</span><span>
</span><span>#geom_col(show.legend = FALSE)+</span><span>
</span><span>geom_ribbon</span><span>(</span><span>aes</span><span>(</span><span>ymin</span><span>=</span><span> </span><span>min</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>max</span><span>))</span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Kill Em All - Lyric Sentiment Polarity Line by Line&quot;</span><span>)</span><span>+</span><span>
</span><span>xlab</span><span>(</span><span>&quot;Song Line #&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylab</span><span>(</span><span>&quot;Sentiment polarity&quot;</span><span>)</span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span> </span><span>track_n</span><span> </span><span>~</span><span> </span><span>title</span><span>,</span><span> </span><span>ncol</span><span>=</span><span>5</span><span>)</span><span>+</span><span>
</span><span>theme_minimal</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>8</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>In chronological order, and album specific colours, natch:</p>
<p><img alt="2018-02-01-Sentiment-by-track-KEA.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-KEA.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-RTL.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-RTL.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-MOP.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-MOP.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-AJFA.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-AJFA.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-Black.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-Black.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-Load.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-Load.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-Reload.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-Reload.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-Anger.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-Anger.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-Magnetic.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-Magnetic.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-02-01-Sentiment-by-track-Hardwired.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-02-01-Sentiment-by-track-Hardwired.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Those of you paying attention last time might remember this plot:</p>
<p><img alt="2018-01-28-album-word-correlations-original.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-album-word-correlations-original.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I tried recreating it with different seeds and limits, and that link between album one and ten still holds:</p>
<p><img alt="2018-01-28-album-word-correlations.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-28-album-word-correlations.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>So I thought I should revisit the topic modelling, this time, instead of by album, grouping them into the 90’s releases (Metallica,Load &amp; Reload) and the first, and latest 2 (Kill Em All, Death Magnetic and Hardwired to Self Destruct).</p>
<p>Here are the resulting plots. Alhough I can determine where most of the words are coming from, I’m still not getting much of a feel for a definite “topic” as such:</p>
<p>The 90’s :</p>
<p><img alt="2018-02-03-Topic_Models-Nineties.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-02-03-Topic_Models-Nineties.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Hardwire ‘Em All :</p>
<p><img alt="2018-02-03-Topic_Models-Hardwire-Em-All.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-02-03-Topic_Models-Hardwire-Em-All.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>And to finish, you write posts and hope folk like them, and this one went down quite well. Not everyone liked it (hence the title – I am “sad”, apparently) but even so, who cares, because Julia Silge made 2018 an awesome year already :</p>
<p><img alt="2018-01-28-reaction-good.PNG" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-28-reaction-good.PNG?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Stuff like this makes staying up late battling with code worthwhile.</p>
<p>And of course, I get to use cutting edge data science tools and techniques.</p>
<p>That’s got to be worth a “Yeah!”</p>
<p><img alt="" src="https://i2.wp.com/media.giphy.com/media/TVzAViCoz0G2I/giphy.gif?w=456&ssl=1" data-recalc-dims="1"></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.johnmackintosh.com/2018-02-03-enter-sadman/"> HighlandR</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/RqKElrNG0Qw" height="1"></a></div>