Visualizing Brooklyn Nine-Nine with R!

[This article was first published on R by R(yo), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

“Hey there criminal. It’s me, Johnny Law!” – Jake Peralta, NYPD.

Brooklyn Nine-Nine has become one of my favorite sitcoms in recent
years, probably taking over from Parks & Recreation and Community. So in
this blog post, I’m going to web scrape some very simple TV statistics,
clean it up with the tidyverse, and visualize it with ggplot2

“Terry loves ggplot2!”

Let’s get started!

Packages

<span class="n">pacman</span><span class="o">::</span><span class="n">p_load</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">,</span><span class="w"> </span><span class="n">rvest</span><span class="p">,</span><span class="w"> </span><span class="n">glue</span><span class="p">,</span><span class="w"> </span><span class="n">cowplot</span><span class="p">,</span><span class="w"> </span><span class="n">ggbeeswarm</span><span class="p">,</span><span class="w"> 
               </span><span class="n">polite</span><span class="p">,</span><span class="w"> </span><span class="n">extrafont</span><span class="p">,</span><span class="w"> </span><span class="n">knitr</span><span class="p">,</span><span class="w"> </span><span class="n">kableExtra</span><span class="p">)</span><span class="w">
</span><span class="n">loadfonts</span><span class="p">()</span><span class="w"> </span><span class="c1"># "Univers" and "Univers LT 93 ExtraBlackEx" fonts</span><span class="w">
</span>

B99 Custom Theme

First, I created a custom Brooklyn Nine-Nine theme that I can put
on every plot. This will save me time from typing in
the same options over and over again! I googled the font type that the
official Brooklyn Nine-Nine media uses, downloaded them, and got it
installed for R using the extrafont package. For some of the different
colors you’ll see in the plots I sourced them from pasting in the
Brooklyn Nine-Nine logo and other official media images into
imagecolorpicker.com and saving the
hex codes that it gave me. Otherwise, I experimented with different
palettes from perusing Emil
Hvitfeld’s
awesome
r-color-palettes
Github repository (Also special thanks to David
Smale
for giving me some advice on
using color!).

<span class="n">theme_b99</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(){</span><span class="w">
  
  </span><span class="n">base_size</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">11</span><span class="w">
  </span><span class="n">half_line</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">base_size</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="m">2</span><span class="w">
  
  </span><span class="n">theme_minimal</span><span class="p">()</span><span class="w"> </span><span class="o">%+replace%</span><span class="w">
    </span><span class="n">theme</span><span class="p">(</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Univers"</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#F9FEFF"</span><span class="p">,</span><span class="w">
                              </span><span class="n">face</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"plain"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">14</span><span class="p">,</span><span class="w"> 
                              </span><span class="n">hjust</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.5</span><span class="p">,</span><span class="w"> </span><span class="n">vjust</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.5</span><span class="p">,</span><span class="w"> </span><span class="n">angle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">,</span><span class="w">
                              </span><span class="n">lineheight</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.9</span><span class="p">,</span><span class="w"> 
                              </span><span class="n">margin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">margin</span><span class="p">(</span><span class="n">half_line</span><span class="p">,</span><span class="w"> </span><span class="n">half_line</span><span class="p">,</span><span class="w">
                                              </span><span class="n">half_line</span><span class="p">,</span><span class="w"> </span><span class="n">half_line</span><span class="p">),</span><span class="w">
                              </span><span class="n">debug</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">),</span><span class="w">
          </span><span class="n">plot.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Univers LT 93 ExtraBlackEx"</span><span class="p">,</span><span class="w">
                                    </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">20</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#F9FEFF"</span><span class="p">),</span><span class="w">
          </span><span class="n">plot.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_rect</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#0053CD"</span><span class="p">),</span><span class="w"> 
          </span><span class="n">panel.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_rect</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#0053CD"</span><span class="p">),</span><span class="w">
          </span><span class="c1"># axis options</span><span class="w">
          </span><span class="n">axis.text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Univers"</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#F9FEFF"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">12</span><span class="p">),</span><span class="w">
          </span><span class="n">axis.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">14</span><span class="p">),</span><span class="w">
          </span><span class="c1"># legend options (for ratings plot)</span><span class="w">
          </span><span class="n">legend.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Univers"</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#F9FEFF"</span><span class="p">),</span><span class="w">
          </span><span class="n">legend.text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Univers"</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#F9FEFF"</span><span class="p">,</span><span class="w">
                                     </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">9</span><span class="p">),</span><span class="w">
          </span><span class="n">legend.position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"bottom"</span><span class="p">,</span><span class="w">
          </span><span class="n">legend.key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_rect</span><span class="p">(</span><span class="n">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"black"</span><span class="p">,</span><span class="w"> </span><span class="n">linetype</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"solid"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.5</span><span class="p">),</span><span class="w">
          </span><span class="n">legend.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_rect</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"black"</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#0053CD"</span><span class="p">,</span><span class="w">
                                           </span><span class="n">linetype</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"solid"</span><span class="p">))</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>

Now the plots will all look very similar, just like a good police squad:



With that done, I can start making plots!

Episode Ratings

As in my more
recent blog
posts I used the polite package to web scrape responsibly (note
the bow() and scrape() functions).

I map_2() over the IMDB page for the episodes for each season and I
append the season number to each episode row. After I’m done with the
web scraping I mutate() in the episode number for each season.

<span class="n">url_df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="w">
  </span><span class="n">urls</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"https://www.imdb.com/title/tt2467372/episodes?season=1"</span><span class="p">,</span><span class="w">
           </span><span class="s2">"https://www.imdb.com/title/tt2467372/episodes?season=2"</span><span class="p">,</span><span class="w">
           </span><span class="s2">"https://www.imdb.com/title/tt2467372/episodes?season=3"</span><span class="p">,</span><span class="w">
           </span><span class="s2">"https://www.imdb.com/title/tt2467372/episodes?season=4"</span><span class="p">,</span><span class="w">
           </span><span class="s2">"https://www.imdb.com/title/tt2467372/episodes?season=5"</span><span class="p">),</span><span class="w">
  </span><span class="n">season_num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">))</span><span class="w"> 

</span><span class="c1"># scraping function:</span><span class="w">
</span><span class="n">brooklyn99_ep_rating</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  
  </span><span class="n">session</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">bow</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="w">
  </span><span class="n">url2</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">scrape</span><span class="p">(</span><span class="n">session</span><span class="p">)</span><span class="w">
  
  </span><span class="c1"># Grab episode names</span><span class="w">
  </span><span class="n">sX_ep_name</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">url2</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".info a"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">html_text</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">as_tibble</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gsub</span><span class="p">(</span><span class="s2">"\\n"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">))</span><span class="w">
  
  </span><span class="c1"># Grab episode rating</span><span class="w">
  </span><span class="n">sX_rate</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">url2</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".ipl-rating-widget > .ipl-rating-star .ipl-rating-star__rating"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">html_text</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">as_tibble</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w">  
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">rating</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gsub</span><span class="p">(</span><span class="s2">"\\n"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">as.numeric</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">select</span><span class="p">(</span><span class="o">-</span><span class="n">value</span><span class="p">)</span><span class="w">
  
  </span><span class="c1"># Clean episode name df</span><span class="w">
  </span><span class="n">sX_ep_name</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sX_ep_name</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">trimws</span><span class="p">(</span><span class="n">value</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">filter</span><span class="p">(</span><span class="o">!</span><span class="n">str_detect</span><span class="p">(</span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="s2">"Rate"</span><span class="p">),</span><span class="w"> </span><span class="n">title</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">select</span><span class="p">(</span><span class="o">-</span><span class="n">value</span><span class="p">)</span><span class="w">
  
  </span><span class="c1"># combine name + rating</span><span class="w">
  </span><span class="n">ep_rating</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sX_ep_name</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">bind_cols</span><span class="p">(</span><span class="n">sX_rate</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">ep_rating_df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map2</span><span class="p">(</span><span class="n">.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">url_df</span><span class="o">$</span><span class="n">urls</span><span class="p">,</span><span class="w"> </span><span class="n">.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">url_df</span><span class="o">$</span><span class="n">season_num</span><span class="p">,</span><span class="w">
                          </span><span class="o">~</span><span class="w"> </span><span class="n">brooklyn99_ep_rating</span><span class="p">(</span><span class="n">url</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">.x</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
         </span><span class="n">mutate</span><span class="p">(</span><span class="n">season</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">.y</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">reduce</span><span class="p">(</span><span class="n">bind_rows</span><span class="p">)</span><span class="w">

</span><span class="n">ep_rating_df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ep_rating_df</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">group_by</span><span class="p">(</span><span class="n">season</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">ep_num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">row_number</span><span class="p">())</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ungroup</span><span class="p">()</span><span class="w">

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">ep_rating_df</span><span class="p">)</span><span class="w">
</span>
## Observations: 112
## Variables: 4
## $ title  <chr> "Pilot", "The Tagger", "The Slump", "M.E. Time", "The V...
## $ rating <dbl> 7.9, 7.7, 7.7, 7.8, 8.1, 8.5, 8.2, 7.9, 7.8, 8.3, 8.3, ...
## $ season <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ ep_num <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, ...

OK, looks good.

Episode Ratings Plot: Heatmap and Boxplot

I used geom_tile() to create a heat map of episode ratings with the
season number as the rows and the episode number for that season as the
columns. I also used the dichromat package for the color scheme
“LightBluetoDarkBlue.10”, it meshes pretty well with the Brooklyn Nine-Nine
color theme!

<span class="n">rating_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ep_rating_df</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ep_num</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">season</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_tile</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rating</span><span class="p">),</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.5</span><span class="p">,</span><span class="w">
            </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"black"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_fill_gradientn</span><span class="p">(</span><span class="s2">"Rating"</span><span class="p">,</span><span class="w"> </span><span class="n">colors</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dichromat</span><span class="o">::</span><span class="n">colorschemes</span><span class="o">$</span><span class="n">LightBluetoDarkBlue.10</span><span class="p">,</span><span class="w">
                       </span><span class="n">breaks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">7.7</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">8.5</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">9.5</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">guides</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">guide_colourbar</span><span class="p">(</span><span class="n">frame.colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"black"</span><span class="p">,</span><span class="w">
                                </span><span class="n">barwidth</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">unit</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="s2">"in"</span><span class="p">)))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_x_continuous</span><span class="p">(</span><span class="n">expand</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0.01</span><span class="p">,</span><span class="w"> </span><span class="m">0.01</span><span class="p">),</span><span class="w"> 
                     </span><span class="n">breaks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">seq</span><span class="p">(</span><span class="n">from</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">20</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">),</span><span class="w"> </span><span class="m">23</span><span class="p">),</span><span class="w">
                     </span><span class="n">labels</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">seq</span><span class="p">(</span><span class="n">from</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">20</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">),</span><span class="w"> </span><span class="m">23</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_y_reverse</span><span class="p">(</span><span class="n">expand</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0.01</span><span class="p">,</span><span class="w"> </span><span class="m">0.01</span><span class="p">),</span><span class="w">
                  </span><span class="n">breaks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">7</span><span class="p">),</span><span class="w"> 
                  </span><span class="n">labels</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">7</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Episode Number"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Season"</span><span class="p">,</span><span class="w">
       </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Episode Ratings"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_b99</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">panel.grid</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">())</span><span class="w">

</span><span class="n">rating_plot</span><span class="w">
</span>

From what we can see on the graph there doesn’t seem to be any trends
along either axis besides the fact that the finales and the subsequent
opening episode of the following season are generally well-received. Two
stand out episodes from Season 5 can be clearly seen in the plot.
These episodes are “HalloVeen” (the fifth Halloween episode of the
series) and “The Box” (an excellent episode featuring Sterling K.
Brown
as the perp).

Box Plot

Another way to visualize this would be with the box plot. This way
allows you to make a comparison between seasons, whereas the heat map
could be used to pick out any trends over time or sequence.

The debate between violin plot vs. box plot vs. sina plot vs. etc. rages
on to this day (some examples I’ve read over the years:
1,
2,
3) and at the end of the day, it may come down to personal preference. Since the data
I’m using is quite small (~20 episode ratings for each season), in my
case it might be better to use box plots (instead of violin plots) and
sprinkle bee swarm points on top with the
ggbeeswarm package. I commented
out some of the other methods if you wanted to copy-paste this code
chunk into your R console to try them out!

I highlighted the outliers for each season in red with the unintentional
result being that the color scheme mimics that of the flag of Colorado…

<span class="n">cols</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"1"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#6CA9C3"</span><span class="p">,</span><span class="w"> </span><span class="s2">"2"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#3A3533"</span><span class="p">,</span><span class="w"> 
          </span><span class="s2">"3"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#000E33"</span><span class="p">,</span><span class="w"> </span><span class="s2">"4"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#CBCFD2"</span><span class="p">,</span><span class="w"> </span><span class="s2">"5"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#175E78"</span><span class="p">)</span><span class="w">

</span><span class="n">box_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ep_rating_df</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">season</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rating</span><span class="p">,</span><span class="w"> </span><span class="n">group</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">season</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="c1">#geom_violin(color = "#F9FEFF", fill = "#000E33") +</span><span class="w">
  </span><span class="c1">#ggthemes::geom_tufteboxplot(color = "#F9FEFF", fill = "#000E33") +</span><span class="w">
  </span><span class="c1">#ggforce::geom_sina(color = "#F9FEFF", fill = "#000E33") +</span><span class="w">
  </span><span class="n">geom_boxplot</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#F9FEFF"</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#000E33"</span><span class="p">,</span><span class="w">
              </span><span class="n">outlier.color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"red"</span><span class="p">,</span><span class="w"> </span><span class="n">outlier.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_beeswarm</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#FCF40E"</span><span class="p">,</span><span class="w"> </span><span class="n">cex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2.25</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_y_continuous</span><span class="p">(</span><span class="n">limits</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">7.5</span><span class="p">,</span><span class="w"> </span><span class="m">9.6</span><span class="p">),</span><span class="w">
                     </span><span class="n">labels</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">7.5</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">8.5</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">9.5</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Episode Ratings by Season"</span><span class="p">,</span><span class="w">
       </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Season"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Rating"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_b99</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">panel.grid.minor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">())</span><span class="w">

</span><span class="n">box_plot</span><span class="w">
</span>

Noice, Smort! We get the best of both worlds by combining two different
types of visualizing distributions. Let’s check out which episodes those
red dots are…

<span class="n">ep_rating_df</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">group_by</span><span class="p">(</span><span class="n">season</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="p">(),</span><span class="w">
         </span><span class="n">third_quantile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">quantile</span><span class="p">(</span><span class="n">rating</span><span class="p">)[</span><span class="m">4</span><span class="p">])</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">rating</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">third_quantile</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1.58</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="n">IQR</span><span class="p">(</span><span class="n">rating</span><span class="p">)))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">select</span><span class="p">(</span><span class="o">-</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">third_quantile</span><span class="p">)</span><span class="w">
</span>
## # A tibble: 3 x 4
## # Groups:   season [2]
##   title           rating season ep_num
##   <chr>            <dbl>  <dbl>  <int>
## 1 Johnny and Dora    9.1      2     23
## 2 HalloVeen          9.5      5      4
## 3 The Box            9.4      5     14

Besides the (re)appearances of Doug Judy, Brooklyn Nine-Nine is known
for its Halloween Episodes. I wanted to use gt somewhere in this blog
post but that’s little bit overkill so a kable table will suffice for
now.

<span class="n">ep_rating_df</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">group_by</span><span class="p">(</span><span class="n">season</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">top_n</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="n">wt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rating</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">arrange</span><span class="p">(</span><span class="n">desc</span><span class="p">(</span><span class="n">rating</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">rank</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">row_number</span><span class="p">())</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">arrange</span><span class="p">(</span><span class="n">season</span><span class="p">,</span><span class="w"> </span><span class="n">desc</span><span class="p">(</span><span class="n">rating</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">str_detect</span><span class="p">(</span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="s2">"Hallo"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">rename</span><span class="p">(</span><span class="n">Title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="n">Rating</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rating</span><span class="p">,</span><span class="w"> </span><span class="n">Season</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">season</span><span class="p">,</span><span class="w">
         </span><span class="n">`Episode Number`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ep_num</span><span class="p">,</span><span class="w"> </span><span class="n">Rank</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rank</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">kable</span><span class="p">(</span><span class="n">format</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"html"</span><span class="p">,</span><span class="w">
        </span><span class="n">caption</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"The Halloween Episodes and Rank in Season"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">kable_styling</span><span class="p">(</span><span class="n">full_width</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">
</span>
The Halloween Episodes and Rank in Season
Title Rating Season Episode Number Rank
Halloween 8.5 1 6 4
Halloween II 8.7 2 4 2
Halloween III 8.9 3 5 2
Halloween IV 8.7 4 5 2
HalloVeen 9.5 5 4 1

It is very clear that the Halloween episodes are highly regarded to
those that rate Brooklyn Nine-Nine on IMDB (although I’m sure most
fans including myself will agree).



A more recent variant for showing a distribution is the ridge-line plot,
courtesy of the ggridges
package:

<span class="n">ep_rating_df2</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ep_rating_df</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">season</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as_factor</span><span class="p">(</span><span class="nf">as.character</span><span class="p">(</span><span class="n">season</span><span class="p">)))</span><span class="w">

</span><span class="n">ep_rating_df2</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rating</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">season</span><span class="p">,</span><span class="w"> </span><span class="n">height</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">..density..</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">ggridges</span><span class="o">::</span><span class="n">geom_density_ridges</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#F9FEFF"</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#000E33"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Episode Ratings by Season"</span><span class="p">,</span><span class="w">
       </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Rating"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Season"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_x_continuous</span><span class="p">(</span><span class="n">limits</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">7.25</span><span class="p">,</span><span class="w"> </span><span class="m">9.8</span><span class="p">),</span><span class="w">
                     </span><span class="n">breaks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">7.5</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">8.5</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">9.5</span><span class="p">),</span><span class="w">
                     </span><span class="n">labels</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">7.5</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">8.5</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">9.5</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_y_discrete</span><span class="p">(</span><span class="n">limits</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rev</span><span class="p">(</span><span class="n">levels</span><span class="p">(</span><span class="n">ep_rating_df2</span><span class="o">$</span><span class="n">season</span><span class="p">)))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_b99</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">panel.grid.minor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">())</span><span class="w">
</span>
## Picking joint bandwidth of 0.155



Well OK, we’re just looking at distributions… but I wanted to use this
GIF in some capacity!

Cast Appearances

The main cast of Brooklyn Nine-Nine are pretty tightly knit and as
members of the same precinct it makes sense that they’ll generally
appear together. So instead, I wanted to look at which non-main cast
members and guests made the most appearances on the show. Special
note
: Hitchcock and Scully weren’t officially “main cast” until
Season 2 but I left them out of the non-main cast list.

After scraping for the full cast list and main cast list, I anti_join() them
so I am left with the non-main cast and the number of episodes that they
appeared in.

<span class="c1"># Entire cast:</span><span class="w">
</span><span class="n">cast_url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">bow</span><span class="p">(</span><span class="s2">"https://www.imdb.com/title/tt2467372/fullcredits?ref_=tt_cl_sm#cast"</span><span class="p">)</span><span class="w">

</span><span class="n">cast_info_raw</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">scrape</span><span class="p">(</span><span class="n">cast_url</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".character"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_text</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">as_tibble</span><span class="p">()</span><span class="w">

</span><span class="n">cast_info_clean</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">cast_info_raw</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">separate</span><span class="p">(</span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n">into</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"blank"</span><span class="p">,</span><span class="w"> </span><span class="s2">"name"</span><span class="p">,</span><span class="w"> </span><span class="s2">"episode_num"</span><span class="p">,</span><span class="w"> </span><span class="s2">"dots"</span><span class="p">),</span><span class="w"> </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\n"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">episode_num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">case_when</span><span class="p">(</span><span class="w">
    </span><span class="n">episode_num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"         / ...  "</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">dots</span><span class="p">,</span><span class="w">
    </span><span class="kc">TRUE</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">episode_num</span><span class="p">),</span><span class="w">
    </span><span class="n">episode_num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">episode_num</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">word</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"e"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">as.numeric</span><span class="p">,</span><span class="w">
    </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">str_trim</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">side</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"both"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">select</span><span class="p">(</span><span class="o">-</span><span class="n">blank</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">dots</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">case_when</span><span class="p">(</span><span class="w">
    </span><span class="n">name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Captain Ray Holt"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ray Holt"</span><span class="p">,</span><span class="w">
    </span><span class="n">name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Scully"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Norm Scully"</span><span class="p">,</span><span class="w">
    </span><span class="n">name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Hitchcock"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Michael Hitchcock"</span><span class="p">,</span><span class="w">
    </span><span class="n">name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Deputy Chief Madeline Wuntch"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Madeline Wuntch"</span><span class="p">,</span><span class="w">
    </span><span class="kc">TRUE</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">name</span><span class="p">))</span><span class="w">
  
</span><span class="c1"># Main cast:</span><span class="w">
</span><span class="n">cast_main_url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">bow</span><span class="p">(</span><span class="s2">"https://en.wikipedia.org/wiki/List_of_Brooklyn_Nine-Nine_characters"</span><span class="p">)</span><span class="w">

</span><span class="n">cast_main_raw</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">scrape</span><span class="p">(</span><span class="n">cast_main_url</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".wikitable"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_table</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">flatten_df</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">as_tibble</span><span class="p">()</span><span class="w">

</span><span class="n">cast_main_clean</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">cast_main_raw</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">slice</span><span class="p">(</span><span class="m">-1</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">select</span><span class="p">(</span><span class="n">Character</span><span class="p">)</span><span class="w">

</span><span class="c1"># anti-join</span><span class="w">
</span><span class="n">non_main_cast</span><span class="w"> </span><span clas...

To leave a comment for the author, please follow the link and comment on their blog: R by R(yo).

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)