Visualizing the Asian Cup 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.

Another year, another big soccer/football tournament! This time it’s the
top international competition in Asia, the Asian Cup hosted in the
U.A.E. In this blog post I’ll be covering (responsible) web-scraping, data wrangling
(tidyverse FTW!), and of course, data visualization with 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">scales</span><span class="p">,</span><span class="w"> </span><span class="n">lubridate</span><span class="p">,</span><span class="w"> </span><span class="n">ggrepel</span><span class="p">,</span><span class="w"> </span><span class="n">stringi</span><span class="p">,</span><span class="w"> </span><span class="n">magick</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">extrafont</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">ggtextures</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">ggimage</span><span class="p">,</span><span class="w"> </span><span class="n">polite</span><span class="p">)</span><span class="w">
</span><span class="c1"># Roboto Condensed font (from hrbrmstrthemes)</span><span class="w">
</span><span class="n">loadfonts</span><span class="p">()</span><span class="w">
</span>

Top Goalscorers of the Asian Cup

The first thing I looked at was, “Who are the top goalscorers in the
history of the Asian Cup?”

Here I use the polite package to
take a look at the robots.txt for the web page and see if it is OK to
web scrape from it. First you pass the URL to the bow() function, check that you are
indeed allowed to scrape, then use scrape() to retrieve data, and the
rest is the usual rvest web-scraping workflow.

<span class="n">topg_url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"https://en.wikipedia.org/wiki/AFC_Asian_Cup_records_and_statistics"</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">topg_url</span><span class="p">)</span><span class="w">

</span><span class="n">ac_top_scorers</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="o">%>%</span><span class="w">
  </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">"table.wikitable:nth-child(29)"</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="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">select</span><span class="p">(</span><span class="o">-</span><span class="n">Ref.</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">set_names</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s2">"total_goals"</span><span class="p">,</span><span class="w"> </span><span class="s2">"player"</span><span class="p">,</span><span class="w"> </span><span class="s2">"country"</span><span class="p">))</span><span class="w">
</span>

For brevity, let’s only take a look at the top 5 goal scorers. I’ll also
mutate() in a nice image of a soccer ball for the data points on the
plot.

<span class="n">ac_top_scorers</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ac_top_scorers</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">head</span><span class="p">(</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">mutate</span><span class="p">(</span><span class="n">image</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"https://www.emoji.co.uk/files/microsoft-emojis/activity-windows10/8356-soccer-ball.png"</span><span class="p">)</span><span class="w">
</span>

I made something slightly different to your standard bar graph as I
use the geom_isotype_col() function from ggtextures to create a bar
of soccer ball images. Compared to other functions in ggtextures,
geom_isotype_col() allows each image to correspond to the value of the
variable you are plotting, in this case 1 ball = 1 goal!

<span class="n">ac_top_graph</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ac_top_scorers</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">reorder</span><span class="p">(</span><span class="n">player</span><span class="p">,</span><span class="w"> </span><span class="n">total_goals</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">total_goals</span><span class="p">,</span><span class="w">
             </span><span class="n">image</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">image</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_isotype_col</span><span class="p">(</span><span class="n">img_width</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">grid</span><span class="o">::</span><span class="n">unit</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"native"</span><span class="p">),</span><span class="w"> </span><span class="n">img_height</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w">
    </span><span class="n">ncol</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">nrow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</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</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="o">+</span><span class="w">
  </span><span class="n">coord_flip</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">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">0</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">4</span><span class="p">,</span><span class="w"> </span><span class="m">6</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">10</span><span class="p">,</span><span class="w"> </span><span class="m">12</span><span class="p">,</span><span class="w"> </span><span class="m">14</span><span class="p">),</span><span class="w">
                     </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</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="p">),</span><span class="w"> 
                     </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">0</span><span class="p">,</span><span class="w"> </span><span class="m">15</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">ggthemes</span><span class="o">::</span><span class="n">theme_solarized</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">"Top Scorers of the Asian Cup"</span><span class="p">,</span><span class="w">
       </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Most goals in a single tournament: 8 (Ali Daei, 1996)"</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">"Number of Goals"</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="kc">NULL</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="n">glue</span><span class="p">(</span><span class="s2">"
                      Source: Wikipedia
                      By @R_by_Ryo"</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">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">"Roboto Condensed"</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">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">22</span><span class="p">),</span><span class="w">
        </span><span class="n">plot.subtitle</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="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">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">axis.title.x</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">16</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.line.y</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">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">panel.background</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">axis.ticks.y</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">ac_top_graph</span><span class="w">
</span>

OK, not bad. However, wouldn’t it be nice to add a bit more context? Specifically,
which country these players came from. So let’s add some flags along the y-axis!

There are lots of different ways to do this (like geom_flag() from the
ggimage package) but I ended up doing it the cowplot way. I had to
tweak the scales a bit as the flags came in different sizes. When you
plot, you just insert the image strip into the bar plot with
axis_canvas() and combine all the parts together with ggdraw()!

<span class="n">axis_image</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">axis_canvas</span><span class="p">(</span><span class="n">ac_top_graph</span><span class="p">,</span><span class="w"> </span><span class="n">axis</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'y'</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> 
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/commons/c/ca/Flag_of_Iran.svg"</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="m">13</span><span class="p">,</span><span class="w"> </span><span class="n">scale</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="o">+</span><span class="w">
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/commons/0/09/Flag_of_South_Korea.svg"</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="m">10</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.7</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/en/9/9e/Flag_of_Japan.svg"</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="m">7</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.7</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/commons/f/f6/Flag_of_Iraq.svg"</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="m">4</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.6</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/commons/a/aa/Flag_of_Kuwait.svg"</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="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.2</span><span class="p">)</span><span class="w">

</span><span class="n">ggdraw</span><span class="p">(</span><span class="n">insert_yaxis_grob</span><span class="p">(</span><span class="n">ac_top_graph</span><span class="p">,</span><span class="w"> </span><span class="n">axis_image</span><span class="p">,</span><span class="w"> </span><span class="n">position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"left"</span><span class="p">))</span><span class="w">
</span>

Ideally I wanted the soccer balls to be the official balls from the
tournament that the player scored in. However, I couldn’t find a nice
emoji-fied/icon-ized version and there was also the “small” problem in
that there was no “official” Asian Cup ball until the 2004 tournament in
China! You can take a look at the official Asian Cup balls
here.

Winners of the Asian Cup

We saw that the top goal scorers came from Iran, South Korea, Japan,
Iraq, and Kuwait but did their goal scoring exploits lead their nations
to glory? Let’s find out!

When web-scraping I really like using flatten_df() after
html_table() as I don’t have to use the awkward looking .[[1]]
within my piped workflow.

<span class="n">acup_url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"https://en.wikipedia.org/wiki/AFC_Asian_Cup"</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">acup_url</span><span class="p">)</span><span class="w">

</span><span class="n">acup_winners_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">session</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">"table:nth-child(31)"</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="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">flatten_df</span><span class="p">()</span><span class="w">
</span>

Now I can use the clean_names() function to quickly clean up my column names
(mainly when I can’t be bothered to set_names() them myself…).

The next steps are splitting up the number of times a team placed
between 1st and 3rd and the year that occurred with separate(). Then variants of mutate() are used to tidy the string columns of the data into numeric type. I use gather() so each team will have a row for each of the rank positions (1st-3rd). Finally, I arrange the data in a way that the facets will be ordered in the way that I want.

<span class="n">acup_winners_clean</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">acup_winners_raw</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">janitor</span><span class="o">::</span><span class="n">clean_names</span><span class="p">()</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="o">:</span><span class="m">8</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">fourth_place</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">semi_finalists</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">total_top_four</span><span class="p">)</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">winners</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">"Champions"</span><span class="p">,</span><span class="w"> </span><span class="s2">"first_place_year"</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">" "</span><span class="p">,</span><span class="w"> </span><span class="n">extra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"merge"</span><span class="p">)</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">runners_up</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">"Runners-up"</span><span class="p">,</span><span class="w"> </span><span class="s2">"second_place_year"</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">" "</span><span class="p">,</span><span class="w"> </span><span class="n">extra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"merge"</span><span class="p">)</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">third_place</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">"Third Place"</span><span class="p">,</span><span class="w"> </span><span class="s2">"third_place_year"</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">" "</span><span class="p">,</span><span class="w"> </span><span class="n">extra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"merge"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate_all</span><span class="p">(</span><span class="n">funs</span><span class="p">(</span><span class="n">str_replace_all</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="s2">"–"</span><span class="p">,</span><span class="w"> </span><span class="s2">"0"</span><span class="p">)))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate_at</span><span class="p">(</span><span class="n">vars</span><span class="p">(</span><span class="n">contains</span><span class="p">(</span><span class="s2">"num"</span><span class="p">)),</span><span class="w"> </span><span class="n">funs</span><span class="p">(</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">mutate</span><span class="p">(</span><span class="n">team</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">if_else</span><span class="p">(</span><span class="n">team</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Israel1"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Israel"</span><span class="p">,</span><span class="w"> </span><span class="n">team</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">gather</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"key"</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"value"</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">team</span><span class="p">,</span><span class="w"> 
         </span><span class="o">-</span><span class="n">first_place_year</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">second_place_year</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">third_place_year</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">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
           </span><span class="n">fct_relevel</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s2">"Champions"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Runners-up"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Third Place"</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">key</span><span class="p">,</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">mutate</span><span class="p">(</span><span class="n">team</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="n">team</span><span class="p">),</span><span class="w">
         </span><span class="n">order</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>

I plot using facets on the “key” variable (containing the rank data) so
that we can see how many times each team placed as Champions to Third
Place. I also use the glue() function here to format the multi-line
captions and titles in a neat way.

<span class="n">acup_winners_clean</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">value</span><span class="p">,</span><span class="w"> </span><span class="n">team</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="n">key</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_point</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">5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_color_manual</span><span class="p">(</span><span class="n">values</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">"Champions"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#FFCC33"</span><span class="p">,</span><span class="w">
                                </span><span class="s2">"Runners-up"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#999999"</span><span class="p">,</span><span class="w">
                                </span><span class="s2">"Third Place"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#CC6600"</span><span class="p">),</span><span class="w">
                     </span><span class="n">guide</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="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">"Number of Occurrence"</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">"Winners & Losers of the Asian Cup!"</span><span class="p">,</span><span class="w">
       </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">glue</span><span class="p">(</span><span class="s2">"
                       Ordered by number of Asian Cup(s) won.
                       Four-time Champions, Japan, only won their first in 1992!"</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="n">glue</span><span class="p">(</span><span class="s2">"
                      Note: Israel was expelled by the AFC in 1974 while Australia joined the AFC in 2006.
                      Source: Wikipedia
                      By @R_by_Ryo"</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">facet_wrap</span><span class="p">(</span><span class="o">~</span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_minimal</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">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">"Roboto Condensed"</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="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">18</span><span class="p">),</span><span class="w">
        </span><span class="n">plot.subtitle</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">12</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.title.y</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">axis.title.x</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">12</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.text.y</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="n">axis.text.x</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">12</span><span class="p">),</span><span class="w">
        </span><span class="n">plot.caption</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">hjust</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">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">10</span><span class="p">),</span><span class="w">
        </span><span class="n">panel.border</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">fill</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">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"grey20"</span><span class="p">),</span><span class="w">
        </span><span class="n">panel.grid.minor.x</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">strip.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">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">16</span><span class="p">))</span><span class="w"> 
</span>

Goals per Game

One new thing I learned very recently, while working on this viz in
fact, was using magrittr aliases! In this workflow I always wind up having to use .[x] or
.[[x]] but now I can just use extract() or extract2() respectively
to do the same thing!

<span class="n">wiki_url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"https://en.wikipedia.org"</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">wiki_url</span><span class="p">)</span><span class="w">
</span><span class="n">acup_url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"https://en.wikipedia.org/wiki/AFC_Asian_Cup"</span><span class="w">
</span><span class="n">session_cup</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">acup_url</span><span class="p">)</span><span class="w">

</span><span class="n">cup_links</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_cup</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">"br+ i a"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_attr</span><span class="p">(</span><span class="s2">"href"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">magrittr</span><span class="o">::</span><span class="n">extract</span><span class="p">(</span><span class="m">-17</span><span class="o">:</span><span class="m">-18</span><span class="p">)</span><span class="w">

</span><span class="n">acup_df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">cup_links</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">as_data_frame</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">cup</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">str_remove</span><span class="p">(</span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\/wiki\\/"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_replace_all</span><span class="p">(</span><span class="s2">"_"</span><span class="p">,</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">rename</span><span class="p">(</span><span class="n">link</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>

Another cool thing I found while scraping this data was the jump_to()
function that allows you to navigate to a new URL. This makes
map()-ing over multiple URL links from a base URL very easy! Here, the
base URL is the AFC Asian Cup Wikipedia page and the function iterates
over each of the URL links of the respective tournament pages.
Another way that I could’ve done this was to map() over the different
dates of the tournaments as the Wikipedia page of each edition of the
Asian Cup only differed in the “year” appended at the beginning of the
URL.

<span class="n">goals_info</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">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">goal_info</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="o">%>%</span><span class="w"> 
    </span><span class="n">jump_to</span><span class="p">(</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">html_nodes</span><span class="p">(</span><span class="s2">".vcalendar"</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">header</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="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">spread</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">2</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">`Goals scored`</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">`Goals scored`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">str_remove_all</span><span class="p">(</span><span class="n">`Goals scored`</span><span class="p">,</span><span class="w"> </span><span class="n">pattern</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">str_extract_all</span><span class="p">(</span><span class="s2">"\\d+\\.*\\d*"</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="p">}</span><span class="w">

</span><span class="n">team_num_info</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">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">team_num_info</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="o">%>%</span><span class="w"> 
    </span><span class="n">jump_to</span><span class="p">(</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">html_nodes</span><span class="p">(</span><span class="s2">".vcalendar"</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">header</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="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">spread</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">2</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">`Teams`</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">`Teams`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">`Teams`</span><span class="p">))</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">match_num_info</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">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">match_num_info</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="o">%>%</span><span class="w"> 
    </span><span class="n">jump_to</span><span class="p">(</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">html_nodes</span><span class="p">(</span><span class="s2">".vcalendar"</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">header</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="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">spread</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">2</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">janitor</span><span class="o">::</span><span class="n">clean_names</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">matches_played</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">matches_played</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">matches_played</span><span class="p">))</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="c1"># all together:</span><span class="w">
</span><span class="n">goals_data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">acup_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">goals_per_game</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">map</span><span class="p">(</span><span class="n">acup_df</span><span class="o">$</span><span class="n">link</span><span class="p">,</span><span class="w"> </span><span class="n">goals_info</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">unlist</span><span class="p">,</span><span class="w">
         </span><span class="n">team_num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">map</span><span class="p">(</span><span class="n">acup_df</span><span class="o">$</span><span class="n">link</span><span class="p">,</span><span class="w"> </span><span class="n">team_num_info</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">unlist</span><span class="p">,</span><span class="w">
         </span><span class="n">match_num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">map</span><span class="p">(</span><span class="n">acup_df</span><span class="o">$</span><span class="n">link</span><span class="p">,</span><span class="w"> </span><span class="n">match_num_info</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">unlist</span><span class="p">)</span><span class="w">
</span>

Next, I clean it up a bit and add in the number of teams that participated
in each tournament.

<span class="n">ac_goals_df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">goals_data</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">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cup</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_extract</span><span class="p">(</span><span class="s2">"[0-9]+"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_replace</span><span class="p">(</span><span class="s2">".."</span><span class="p">,</span><span class="w"> </span><span class="s2">"'"</span><span class="p">),</span><span class="w">
         </span><span class="n">team_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="nf">is.na</span><span class="p">(</span><span class="n">team_num</span><span class="p">)</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="m">16</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">team_num</span><span class="w">
         </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">cup</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">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">factor</span><span class="p">(</span><span class="n">label</span><span class="p">,</span><span class="w"> </span><span class="n">label</span><span class="p">),</span><span class="w">
         </span><span class="n">team_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">4</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">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="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="m">10</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">12</span><span class="p">,</span><span class="w"> </span><span class="m">12</span><span class="p">,</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w"> </span><span class="m">16</span><span class="p">))</span><span class="w">

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">ac_goals_df</span><span class="p">)</span><span class="w">
</span>
## Observations: 16
## Variables: 6
## $ link           <chr> "/wiki/1956_AFC_Asian_Cup", "/wiki/1960_AFC_Asi...
## $ cup            <chr> "1956 AFC Asian Cup", "1960 AFC Asian Cup", "19...
## $ goals_per_game <dbl> 4.50, 3.17, 2.17, 3.20, 2.92, 2.50, 3.17, 1.83,...
## $ team_num       <dbl> 4, 4, 4, 5, 6, 6, 10, 10, 10, 8, 12, 12, 16, 16...
## $ match_num      <dbl> 6, 6, 6, 10, 13, 10, 24, 24, 24, 16, 26, 26, 32...
## $ label          <fct> '56, '60, '64, '68, '72, '76, '80, '84, '88, '9...

Now we make a line graph but with lots of annotate() code to add in
comments, labels, and segments for the labels. At the end I use
geom_emoji() to add a soccer ball to the plot for each of the data
points.

<span class="n">plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ac_goals_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">label</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">goals_per_game</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="m">1</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_line</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="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="m">5.35</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.5</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">2.5</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">3.5</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">4.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">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Tournament (Year)"</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">"Goals per Game"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_minimal</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">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">"Roboto Condensed"</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"> <...

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)