<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/analysing-the-gilmore-girls-coffee-addiction-with-r/"></div></div><div><a data-url="https://www.r-bloggers.com/analysing-the-gilmore-girls-coffee-addiction-with-r/" data-counturl="https://www.r-bloggers.com/analysing-the-gilmore-girls-coffee-addiction-with-r/" data-count="horizontal" data-text="Analysing the Gilmore Girls’ coffee addiction with R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shiring.github.io/text_analysis/2016/11/20/gilmore_girls_part2_post"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Last week’s post showed how to create a <a target="_blank" rel="nofollow" href="https://shiring.github.io/text_analysis/2016/11/13/gilmore_girls_part1_post">Gilmore Girls character network</a>.</p>
<p>In this week’s short post, I want to explore the Gilmore Girls’ famous coffee addiction by analysing the same episode transcripts that were also used last week.</p>
<p>I am also showcasing how to use the <a target="_blank" rel="nofollow" href="https://blog.rstudio.org/2016/11/14/ggplot2-2-2-0/">recently updated ggplot2 2.2.0</a>.</p>
<p>The transcripts were prepared as described in last week’s post. Full R code can be found below.</p>
<p></p>
<h2>How often do they talk about coffee?</h2>
<p>The first question I want to explore is how often Lorelai, Rory, Luke, Sookie, Lane and Paris talked about coffee. For this, I subsetted the lines of these characters to where they talk about coffee (i.e. lines that include the word “coffee” at least once). I then created a plot showing how often they talk about coffee per episode and season.</p>
<p></p>
<p><img src="https://i2.wp.com/shiring.github.io/text_analysis/2016/11/20/gilmore_girls_part2_post_files/figure-markdown_github/unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<p>The differences in coffee mention between characters are of course somewhat biased by differences in the total number of lines of each character in the respective episodes, as well by the length of the episodes.</p>
<p>This led me to wonder what proportion of each episode was occupied by which character.</p>
<p></p>
<p><img src="https://i0.wp.com/shiring.github.io/text_analysis/2016/11/20/gilmore_girls_part2_post_files/figure-markdown_github/unnamed-chunk-9-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<p><img src="https://i1.wp.com/shiring.github.io/text_analysis/2016/11/20/gilmore_girls_part2_post_files/figure-markdown_github/unnamed-chunk-11-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<hr>
<p></p>
<h1>R code</h1>
<div>
<pre><code><span># set names
</span><span>names</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;LORELAI&quot;</span><span>,</span><span> </span><span>&quot;RORY&quot;</span><span>,</span><span> </span><span>&quot;LUKE&quot;</span><span>,</span><span> </span><span>&quot;SOOKIE&quot;</span><span>,</span><span> </span><span>&quot;LANE&quot;</span><span>,</span><span> </span><span>&quot;PARIS&quot;</span><span>)</span><span>
</span><span># extract lines for each character in names
</span><span>for</span><span> </span><span>(</span><span>name</span><span> </span><span>in</span><span> </span><span>names</span><span>){</span><span>
</span><span>assign</span><span>(</span><span>paste</span><span>(</span><span>&quot;lines&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>),</span><span> </span><span>transcripts_2</span><span>[</span><span>grep</span><span>(</span><span>paste0</span><span>(</span><span>name</span><span>),</span><span> </span><span>transcripts_2</span><span>$</span><span>character</span><span>),</span><span> </span><span>])</span><span>
</span><span>}</span><span>
</span><span># extract lines not from characters in names
</span><span>lines_OTHER</span><span> </span><span>&lt;-</span><span> </span><span>transcripts_2</span><span>[</span><span>!</span><span>grepl</span><span>(</span><span>&quot;LORELAI|RORY|LUKE|SOOKIE|LANE|PARIS&quot;</span><span>,</span><span> </span><span>transcripts_2</span><span>$</span><span>character</span><span>),</span><span> </span><span>]</span><span>
</span><span># update names to include &quot;other&quot;
</span><span>names</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>names</span><span>,</span><span> </span><span>&quot;OTHER&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span># which lines mention coffee.
</span><span>for</span><span> </span><span>(</span><span>name</span><span> </span><span>in</span><span> </span><span>names</span><span>){</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste</span><span>(</span><span>&quot;lines&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span>assign</span><span>(</span><span>paste</span><span>(</span><span>&quot;coffee&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>),</span><span> </span><span>df</span><span>[</span><span>grep</span><span>(</span><span>&quot;coffee&quot;</span><span>,</span><span> </span><span>df</span><span>$</span><span>dialogue</span><span>),</span><span> </span><span>])</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span># calculate number of lines in total and with coffee mentions per episode
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>name</span><span> </span><span>in</span><span> </span><span>names</span><span>){</span><span>
</span><span>lines_p_ep</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste</span><span>(</span><span>&quot;lines&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span>lines_p_ep</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>table</span><span>(</span><span>lines_p_ep</span><span>$</span><span>episode</span><span>))</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste</span><span>(</span><span>&quot;coffee&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span>df_2</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>table</span><span>(</span><span>df</span><span>$</span><span>episode</span><span>))</span><span>
</span><span># calculate coffee lines per episode
</span><span> </span><span>coffee_lines_p_ep</span><span> </span><span>&lt;-</span><span> </span><span>full_join</span><span>(</span><span>lines_p_ep</span><span>,</span><span> </span><span>df_2</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;Var1&quot;</span><span>)</span><span>
</span><span>coffee_lines_p_ep</span><span>$</span><span>ratio_p_ep</span><span> </span><span>&lt;-</span><span> </span><span>coffee_lines_p_ep</span><span>$</span><span>Freq.y</span><span>/</span><span>coffee_lines_p_ep</span><span>$</span><span>Freq.x</span><span>
</span><span>coffee_lines_p_ep</span><span>$</span><span>Season</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;_.*&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>coffee_lines_p_ep</span><span>$</span><span>Var1</span><span>)</span><span>
</span><span>coffee_lines_p_ep</span><span>$</span><span>character</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>name</span><span>)</span><span>
</span><span>assign</span><span>(</span><span>paste</span><span>(</span><span>&quot;coffee_lines_p_ep&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>),</span><span> </span><span>coffee_lines_p_ep</span><span>)</span><span>
</span><span>}</span><span>
</span><span>coffee_df</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>coffee_lines_p_ep_LORELAI</span><span>,</span><span> </span><span>coffee_lines_p_ep_RORY</span><span>,</span><span> </span><span>coffee_lines_p_ep_LUKE</span><span>,</span><span> </span><span>coffee_lines_p_ep_SOOKIE</span><span>,</span><span> </span><span>coffee_lines_p_ep_LANE</span><span>,</span><span> </span><span>coffee_lines_p_ep_PARIS</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>my_theme</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;sans&quot;</span><span>){</span><span>
</span><span>theme_grey</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>base_size</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>base_family</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>),</span><span>
</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>45</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>14</span><span>),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;aliceblue&quot;</span><span>),</span><span>
</span><span>strip.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;lightgrey&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>strip.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>),</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;bottom&quot;</span><span>,</span><span>
</span><span>legend.justification</span><span> </span><span>=</span><span> </span><span>&quot;top&quot;</span><span>,</span><span> </span><span>legend.box</span><span> </span><span>=</span><span> </span><span>&quot;horizontal&quot;</span><span>,</span><span>
</span><span>legend.box.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>),</span><span>
</span><span>legend.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>coffee_df_gather</span><span> </span><span>&lt;-</span><span> </span><span>coffee_df</span><span> </span><span>%>%</span><span> </span><span>gather</span><span>(</span><span>group</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>Freq.x</span><span>:</span><span>ratio_p_ep</span><span>)</span><span>
</span><span>coffee_df_gather</span><span>[</span><span>which</span><span>(</span><span>coffee_df_gather</span><span>$</span><span>group</span><span> </span><span>==</span><span> </span><span>&quot;Freq.x&quot;</span><span>),</span><span> </span><span>&quot;group&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;Lines per episode&quot;</span><span>
</span><span>coffee_df_gather</span><span>[</span><span>which</span><span>(</span><span>coffee_df_gather</span><span>$</span><span>group</span><span> </span><span>==</span><span> </span><span>&quot;Freq.y&quot;</span><span>),</span><span> </span><span>&quot;group&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;Coffee lines per episode&quot;</span><span>
</span><span>coffee_df_gather</span><span>[</span><span>which</span><span>(</span><span>coffee_df_gather</span><span>$</span><span>group</span><span> </span><span>==</span><span> </span><span>&quot;ratio_p_ep&quot;</span><span>),</span><span> </span><span>&quot;group&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;Ratio of coffee lines per episode&quot;</span><span>
</span><span>coffee_df_gather</span><span>$</span><span>character</span><span> </span><span>&lt;-</span><span> </span><span>factor</span><span>(</span><span>coffee_df_gather</span><span>$</span><span>character</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>names</span><span>)</span><span>
</span><span>coffee_df_gather</span><span>$</span><span>group</span><span> </span><span>&lt;-</span><span> </span><span>factor</span><span>(</span><span>coffee_df_gather</span><span>$</span><span>group</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Lines per episode&quot;</span><span>,</span><span> </span><span>&quot;Coffee lines per episode&quot;</span><span>,</span><span> </span><span>&quot;Ratio of coffee lines per episode&quot;</span><span>))</span><span>
</span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>coffee_df_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>character</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>Season</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Season</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_boxplot</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Number or ratio of lines&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Lorelai &amp; Luke are the coffee queen and king&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Coffee mentions per episode and season&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nThese boxplots show the number of total lines per episode and season, lines with coffee mentions per epsiode and season,
as well as the ratio between the two for the main characters Lorelai, Rory, Luke, Sookie, Lane and Paris. Lorelai consistently had
the most lines per episode, followed closely by Rory and Luke. Sookie, Lane and Paris had roughly the same numbers of lines per episode,
although their variance is higher. The same trend is reflected by the number of lines with coffee mentions, except that Rory and Luke seem
to talk similarly often about coffee. Interestingly, Lorelai's (verbal) coffee obsession seems to have decreased slightly over the seasons.
The ratio of coffee lines divided by the total number of lines for each episode reflects that even though Lorelai, Rory and Luke talk a lot about coffee,
they also talk a lot in general, so their ratio of coffee mentions is only slightly higher than Sookie's, Lane's and Paris'. While the latter's mean ratio is lower. they have a much higher variance with a few episodes where they seem to have talked relatively much about coffee.
During the first seasons, Luke is the character with the highest ratio of coffee vs general talk but this is decreasing over time and becomes more
similar to Lorelai and Rory's coffee talk ratio.&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span> </span><span>group</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>nrow</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_fill_brewer</span><span>(</span><span>palette</span><span>=</span><span>&quot;Dark2&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_color_brewer</span><span>(</span><span>palette</span><span>=</span><span>&quot;Dark2&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span># calculate number of lines in total
</span><span>for</span><span> </span><span>(</span><span>name</span><span> </span><span>in</span><span> </span><span>names</span><span>){</span><span>
</span><span>lines_p_ep</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste</span><span>(</span><span>&quot;lines&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span>lines_p_ep</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>table</span><span>(</span><span>lines_p_ep</span><span>$</span><span>episode</span><span>))</span><span>
</span><span>lines_p_ep</span><span>$</span><span>character</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>name</span><span>)</span><span>
</span><span>assign</span><span>(</span><span>paste</span><span>(</span><span>&quot;lines_p_ep&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>),</span><span> </span><span>lines_p_ep</span><span>)</span><span>
</span><span>}</span><span>
</span><span># combine and convert to wide format to make calculating proportions easier
</span><span>lines_df_wide</span><span> </span><span>&lt;-</span><span> </span><span>spread</span><span>(</span><span>rbind</span><span>(</span><span>lines_p_ep_LORELAI</span><span>,</span><span> </span><span>lines_p_ep_RORY</span><span>,</span><span> </span><span>lines_p_ep_LUKE</span><span>,</span><span> </span><span>lines_p_ep_SOOKIE</span><span>,</span><span> </span><span>lines_p_ep_LANE</span><span>,</span><span> </span><span>lines_p_ep_PARIS</span><span>,</span><span> </span><span>lines_p_ep_OTHER</span><span>),</span><span> </span><span>Var1</span><span>,</span><span> </span><span>Freq</span><span>)</span><span>
</span><span>rownames</span><span>(</span><span>lines_df_wide</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>lines_df_wide</span><span>[,</span><span> </span><span>1</span><span>]</span><span>
</span><span>lines_df_wide</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>t</span><span>(</span><span>lines_df_wide</span><span>[,</span><span> </span><span>-1</span><span>]))</span><span>
</span><span># calculate proportions and percentage
</span><span>prop_lines_df_wide</span><span> </span><span>&lt;-</span><span> </span><span>sweep</span><span>(</span><span>lines_df_wide</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>rowSums</span><span>(</span><span>lines_df_wide</span><span>),</span><span> </span><span>FUN</span><span> </span><span>=</span><span> </span><span>&quot;/&quot;</span><span>)</span><span>
</span><span>percent_lines_df_wide</span><span> </span><span>&lt;-</span><span> </span><span>prop_lines_df_wide</span><span> </span><span>*</span><span> </span><span>100</span><span>
</span><span>percent_lines_df_wide</span><span>$</span><span>Episode</span><span> </span><span>&lt;-</span><span> </span><span>rownames</span><span>(</span><span>percent_lines_df_wide</span><span>)</span><span>
</span><span>percent_lines_df_wide</span><span>$</span><span>Season</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;_.*&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>percent_lines_df_wide</span><span>$</span><span>Episode</span><span>)</span><span>
</span><span># gather for plotting
</span><span>percent_lines_df_gather</span><span> </span><span>&lt;-</span><span> </span><span>percent_lines_df_wide</span><span> </span><span>%>%</span><span> </span><span>gather</span><span>(</span><span>Character</span><span>,</span><span> </span><span>Percent</span><span>,</span><span> </span><span>LANE</span><span>:</span><span>SOOKIE</span><span>)</span><span>
</span><span>percent_lines_df_gather</span><span>$</span><span>Character</span><span> </span><span>&lt;-</span><span> </span><span>factor</span><span>(</span><span>percent_lines_df_gather</span><span>$</span><span>Character</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>names</span><span>)</span><span>
</span><span>percent_lines_df_gather</span><span> </span><span>&lt;-</span><span> </span><span>merge</span><span>(</span><span>percent_lines_df_gather</span><span>,</span><span> </span><span>subset</span><span>(</span><span>lines_LORELAI</span><span>,</span><span> </span><span>select</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>episode</span><span>,</span><span> </span><span>episode_running_nr</span><span>)),</span><span> </span><span>by.x</span><span> </span><span>=</span><span> </span><span>&quot;Episode&quot;</span><span>,</span><span> </span><span>by.y</span><span> </span><span>=</span><span> </span><span>&quot;episode&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>percent_lines_df_gather</span><span>$</span><span>Episode</span><span> </span><span>&lt;-</span><span> </span><span>factor</span><span>(</span><span>percent_lines_df_gather</span><span>$</span><span>Episode</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>percent_lines_df_gather</span><span>[</span><span>order</span><span>(</span><span>percent_lines_df_gather</span><span>$</span><span>episode_running_nr</span><span>,</span><span> </span><span>decreasing</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span> </span><span>&quot;Episode&quot;</span><span>])</span><span>
</span><span>percent_lines_df_gather</span><span> </span><span>&lt;-</span><span> </span><span>percent_lines_df_gather</span><span>[</span><span>!</span><span>duplicated</span><span>(</span><span>percent_lines_df_gather</span><span>),</span><span> </span><span>]</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>percent_lines_df_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Character</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Percent</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Season</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_boxplot</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Percent&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Who's the biggest talker? (a)&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Percentage of lines per episode spoken by main characters&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nThe boxplot shows the percentages of lines spoken by the main characters per episode and season.
In the first three seasons the majority of lines are clearly spoken by Lorelai and Rory, followed with some distance by Luke.
During the later seasons, the gap between Lorelai, Rory and the rest is not as pronounced any more.&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span> </span><span>Season</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>nrow</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_fill_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Dark2&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_color_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Dark2&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span># calculate number of lines in total
</span><span>for</span><span> </span><span>(</span><span>name</span><span> </span><span>in</span><span> </span><span>names</span><span>){</span><span>
</span><span>lines_p_season</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste</span><span>(</span><span>&quot;lines&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span>lines_p_season</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>table</span><span>(</span><span>lines_p_season</span><span>$</span><span>season</span><span>))</span><span>
</span><span>lines_p_season</span><span>$</span><span>character</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>name</span><span>)</span><span>
</span><span>assign</span><span>(</span><span>paste</span><span>(</span><span>&quot;lines_p_season&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>),</span><span> </span><span>lines_p_season</span><span>)</span><span>
</span><span>}</span><span>
</span><span># combine and convert to wide format to make calculating proportions easier
</span><span>lines_df_wide</span><span> </span><span>&lt;-</span><span> </span><span>spread</span><span>(</span><span>rbind</span><span>(</span><span>lines_p_season_LORELAI</span><span>,</span><span> </span><span>lines_p_season_RORY</span><span>,</span><span> </span><span>lines_p_season_LUKE</span><span>,</span><span> </span><span>lines_p_season_SOOKIE</span><span>,</span><span> </span><span>lines_p_season_LANE</span><span>,</span><span> </span><span>lines_p_season_PARIS</span><span>,</span><span> </span><span>lines_p_season_OTHER</span><span>),</span><span> </span><span>Var1</span><span>,</span><span> </span><span>Freq</span><span>)</span><span>
</span><span>rownames</span><span>(</span><span>lines_df_wide</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>lines_df_wide</span><span>[,</span><span> </span><span>1</span><span>]</span><span>
</span><span>lines_df_wide</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>t</span><span>(</span><span>lines_df_wide</span><span>[,</span><span> </span><span>-1</span><span>]))</span><span>
</span><span># calculate proportions and percentage
</span><span>prop_lines_df_wide</span><span> </span><span>&lt;-</span><span> </span><span>sweep</span><span>(</span><span>lines_df_wide</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>rowSums</span><span>(</span><span>lines_df_wide</span><span>),</span><span> </span><span>FUN</span><span> </span><span>=</span><span> </span><span>&quot;/&quot;</span><span>)</span><span>
</span><span>percent_lines_df_wide</span><span> </span><span>&lt;-</span><span> </span><span>prop_lines_df_wide</span><span> </span><span>*</span><span> </span><span>100</span><span>
</span><span>percent_lines_df_wide</span><span>$</span><span>Season</span><span> </span><span>&lt;-</span><span> </span><span>rownames</span><span>(</span><span>percent_lines_df_wide</span><span>)</span><span>
</span><span># gather for plotting
</span><span>percent_lines_df_gather</span><span> </span><span>&lt;-</span><span> </span><span>percent_lines_df_wide</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>Character</span><span>,</span><span> </span><span>Percent</span><span>,</span><span> </span><span>LANE</span><span>:</span><span>SOOKIE</span><span>)</span><span>
</span><span>percent_lines_df_gather</span><span>$</span><span>Character</span><span> </span><span>&lt;-</span><span> </span><span>factor</span><span>(</span><span>percent_lines_df_gather</span><span>$</span><span>Character</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>names</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span># plot
</span><span>bp</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>percent_lines_df_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Percent</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Character</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bar</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>stat</span><span> </span><span>=</span><span> </span><span>&quot;identity&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>theme_minimal</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_fill_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Spectral&quot;</span><span>)</span><span>
</span><span>pie</span><span> </span><span>&lt;-</span><span> </span><span>bp</span><span> </span><span>+</span><span> </span><span>coord_polar</span><span>(</span><span>&quot;y&quot;</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>axis.title.x</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.title.y</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.grid</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>plot.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>14</span><span>,</span><span> </span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>),</span><span>
</span><span>legend.title</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;bottom&quot;</span><span>,</span><span>
</span><span>legend.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>8</span><span>),</span><span>
</span><span>legend.justification</span><span> </span><span>=</span><span> </span><span>&quot;top&quot;</span><span>,</span><span> </span><span>legend.box</span><span> </span><span>=</span><span> </span><span>&quot;horizontal&quot;</span><span>,</span><span>
</span><span>legend.box.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>),</span><span>
</span><span>legend.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>)</span><span> </span><span>+</span><span> </span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>nrow</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span>
</span><span>pie</span><span> </span><span>+</span><span> </span><span>facet_wrap</span><span>(</span><span> </span><span>~</span><span> </span><span>Season</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Who's the biggest talker? (b)&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Percent of lines per season spoken by main characters&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<hr>
<div>
<pre><code><span>sessionInfo</span><span>()</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12
## 
## locale:
## [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_2.2.0         dplyr_0.5.0           splitstackshape_1.4.2
## [4] data.table_1.9.6      tidyr_0.6.0          
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.8        knitr_1.15         magrittr_1.5      
##  [4] munsell_0.4.3      colorspace_1.3-1   R6_2.2.0          
##  [7] stringr_1.1.0      plyr_1.8.4         tools_3.3.2       
## [10] grid_3.3.2         gtable_0.2.0       DBI_0.5-1         
## [13] htmltools_0.3.5    lazyeval_0.2.0     yaml_2.1.14       
## [16] assertthat_0.1     digest_0.6.10      tibble_1.2        
## [19] RColorBrewer_1.1-2 evaluate_0.10      rmarkdown_1.1     
## [22] labeling_0.3       stringi_1.1.2      scales_0.4.1      
## [25] chron_2.3-47
</code></pre>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/analysing-the-gilmore-girls-coffee-addiction-with-r/"></div></div><div><a data-url="https://www.r-bloggers.com/analysing-the-gilmore-girls-coffee-addiction-with-r/" data-counturl="https://www.r-bloggers.com/analysing-the-gilmore-girls-coffee-addiction-with-r/" data-count="vertical" data-text="Analysing the Gilmore Girls’ coffee addiction with R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://shiring.github.io/text_analysis/2016/11/20/gilmore_girls_part2_post"> Shirin's playgRound</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/rx9Z4S-6rzY"></a></div>