<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/analyzing-the-greatest-strikers-in-football-ii-visualizing-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.schochastics.net/post/analyzing-the-greatest-strikers-in-football-ii-visualizing-data/"> schochastics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is the second part of <em>Analyzing the Greatest Strikers in Football</em>. In the <a rel="nofollow" href="http://blog.schochastics.net/post/analyzing-the-greatest-strikers-in-football-i-getting-data/" target="_blank">first part</a>,<br>
we created the function <code>get_goals()</code> which allows us to conveniently scrape detailed information of players<br>
career goals from <a rel="nofollow" href="https://www.transfermarkt.co.uk/" target="_blank">transfermarkt.co.uk</a>. In this part,<br>
we are going to explore the data.</p>
<pre><code>library(tidyverse) # for data wrangling
library(lubridate) # for date formats
library(ggimage)   # adding images to ggplot
library(patchwork) # attaching ggplot objects
library(viridis)   # viridis color schemes</code></pre>
<p>We start by getting the data of Zlatan with the <code>get_goals()</code> function.</p>
<pre><code>zlatan_tbl &lt;- get_goals(player = &quot;zlatan-ibrahimovic&quot;, id = &quot;3455&quot;)

glimpse(zlatan_tbl)</code></pre>
<pre><code>## List of 4
##  $ data    :'data.frame':    423 obs. of  13 variables:
##   ..$ competition: chr [1:423] &quot;Eredivisie&quot; &quot;Eredivisie&quot; &quot;UEFA Cup&quot; &quot;UEFA Cup&quot; ...
##   ..$ day        : chr [1:423] &quot;3&quot; &quot;4&quot; &quot;First Round&quot; &quot;First Round&quot; ...
##   ..$ date       : Date[1:423], format: &quot;2001-08-26&quot; ...
##   ..$ venue      : chr [1:423] &quot;A&quot; &quot;A&quot; &quot;H&quot; &quot;A&quot; ...
##   ..$ against    : chr [1:423] &quot;Feyenoord  &quot; &quot;FC Twente  &quot; &quot;Apol. Limassol&quot; &quot;Apol. Limassol&quot; ...
##   ..$ minute     : num [1:423] 64 19 4 64 68 79 68 78 95 47 ...
##   ..$ standing   : chr [1:423] &quot;0:1&quot; &quot;0:1&quot; &quot;1:0&quot; &quot;0:2&quot; ...
##   ..$ type       : chr [1:423] &quot;Tap-in&quot; &quot;&quot; &quot;&quot; &quot;Header&quot; ...
##   ..$ provider   : chr [1:423] &quot;Hatem Trabelsi&quot; &quot;&quot; &quot;Nikolaos Machlas&quot; &quot;Wamberto&quot; ...
##   ..$ club       : Factor w/ 8 levels &quot;Ajax Amsterdam&quot;,..: 1 1 1 1 1 1 1 1 1 1 ...
##   ..$ club_crest : chr [1:423] &quot;https://tmssl.akamaized.net//images/wappen/head/610.png&quot; &quot;https://tmssl.akamaized.net//images/wappen/head/610.png&quot; &quot;https://tmssl.akamaized.net//images/wappen/head/610.png&quot; &quot;https://tmssl.akamaized.net//images/wappen/head/610.png&quot; ...
##   ..$ age        : num [1:423] 19.9 19.9 20 20 20.2 ...
##   ..$ goals      : int [1:423] 1 2 3 4 5 6 7 8 9 10 ...
##  $ name    : chr &quot;Zlatan Ibrahimovic&quot;
##  $ birthday: Date[1:1], format: &quot;1981-10-03&quot;
##  $ portrait: chr &quot;https://tmssl.akamaized.net//images/portrait/header/3455-1521831775.jpg?lm=1521831871&quot;</code></pre>
<p>For comparisons, we also grab the data for Messi and CR7.</p>
<pre><code>messi_tbl &lt;- get_goals(&quot;lionel-messi&quot;,&quot;28003&quot;) 
cr7_tbl &lt;- get_goals(&quot;cristiano-ronaldo&quot;,&quot;8198&quot;)</code></pre>
<div>
<h1>Greating a ggplot theme</h1>
<p>This is a totally optional step, but I like to use a customized ggplot theme to make the<br>
plots more unique.</p>
<pre><code>theme_goals &lt;- function(base_family=&quot;Roboto Condensed&quot;,
                        ticks=TRUE,axis=TRUE,grid=&quot;&quot;){
  ret &lt;- theme_bw()+
    theme(panel.background = element_rect(fill=&quot;#666666&quot;,colour = &quot;#666666&quot;),
        plot.background = element_rect(fill=&quot;#666666&quot;,colour = &quot;#666666&quot;),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(fill=NA,colour=&quot;#666666&quot;),
        legend.background = element_rect(fill=&quot;#666666&quot;,colour = &quot;#666666&quot;),
        legend.box.background = element_rect(fill=&quot;#666666&quot;,colour = &quot;#666666&quot;),
        legend.key = element_rect(fill=&quot;#666666&quot;,colour = &quot;#666666&quot;),
        legend.text = element_text(family = base_family,colour=&quot;white&quot;,size=rel(1.1)),
        axis.text = element_text(colour=&quot;white&quot;,size=rel(0.9),family=base_family),
        axis.title = element_text(family = base_family,colour=&quot;white&quot;,size=rel(1.1)),
        legend.position = &quot;bottom&quot;,
        plot.title = element_text(family = base_family,color=&quot;white&quot;,face = &quot;bold&quot;,size=rel(1.5)),
        plot.subtitle = element_text(family = base_family,color=&quot;white&quot;))
  if(ticks){
    ret &lt;- ret + theme(axis.ticks = element_line(colour=&quot;white&quot;))
  } else {
    ret &lt;- ret + theme(axis.ticks = element_blank())
  }
  if(axis){
    ret &lt;- ret + theme(axis.line = element_line(colour=&quot;white&quot;))
  } else {
    ret &lt;- ret + theme(axis.line = element_blank())
  }
  if(grid==&quot;&quot;){
    ret &lt;- ret + theme(panel.grid = element_blank())
  } else if(grid==&quot;y&quot;){
    ret &lt;- ret + theme(panel.grid.major.x = element_blank(),
                       panel.grid.major.y = element_line(colour=&quot;white&quot;,size=0.1))
  } else if(grid==&quot;x&quot;){
    ret &lt;- ret + theme(panel.grid.major.y = element_blank(),
                       panel.grid.major.x = element_line(colour=&quot;white&quot;,size=0.1))
  }
  ret
}</code></pre>
<p>Let’s move on to the more interesting part.</p>
</div>
<div>
<h1>Goals over time and per club</h1>
<pre><code># calculate position of crests
crest_pos &lt;- zlatan_tbl$data %>% 
  group_by(club) %>%
  summarise(x = mean(age),
            y = mean(goals),
            crest = club_crest[1])

ggplot(zlatan_tbl$data,aes(x=age,y=goals,col=club))+
  geom_image(data=crest_pos,aes(x=x,y=y,image=crest),size=0.05,alpha=0.5)+
  geom_line()+
  scale_color_viridis_d(option = &quot;viridis&quot;, name = &quot;&quot;)+
  theme_goals(grid = &quot;y&quot;)+
  labs(title=zlatan_tbl$name,
       subtitle=paste(nrow(zlatan_tbl$data),&quot;goals between&quot;,
                min(zlatan_tbl$data$date),&quot;and&quot;,max(zlatan_tbl$data$date))) -> p_date

p_date</code></pre>
<p><img src="https://i0.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/plot_goals-1.png?w=450" data-recalc-dims="1"></p>
<p>Zlatan truly is a prolific player, having scored more than 400 goals for 8 different clubs.<br>
But his achievements in terms of goals scored fade compared to Messi and CR7.</p>
<p><img src="https://i1.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/messi_cr7_zlatan_goals-1.png?w=450" data-recalc-dims="1"></p>
</div>
<div>
<h1>Goals per minute and standing</h1>
<p>Below we look at the number of goals scored per ingame minute, excluding extra time.</p>
<pre><code>zlatan_tbl$data$y &lt;- floor(zlatan_tbl$data$minute/10)
zlatan_tbl$data$x &lt;- zlatan_tbl$data$minute%%10
zlatan_tbl$data$y[zlatan_tbl$data$x==0] &lt;- zlatan_tbl$data$y[zlatan_tbl$data$x==0]-1
zlatan_tbl$data$x[zlatan_tbl$data$x==0] &lt;- 10
zlatan_tbl$data %>%
  dplyr::filter(minute&lt;=90) %>%
  group_by(x,y,minute) %>%
  count() %>%
  ggplot(aes(x,y))+geom_tile(aes(fill=n),colour=&quot;#666666&quot;,size=1)+
  geom_text(aes(label=n))+
  scale_y_reverse(breaks=0:8,
                  labels=c(&quot;1'-10'&quot;,&quot;11'-20'&quot;,&quot;21'-30'&quot;,&quot;31'-40'&quot;,&quot;41'-50'&quot;,&quot;51'-60'&quot;,
                           &quot;61'-70'&quot;,&quot;71'-80'&quot;,&quot;81'-90'&quot;))+
  viridis::scale_fill_viridis(option = &quot;D&quot;,name=&quot;&quot;,limits=c(0,15),na.value=&quot;#FF7F00&quot;)+
  theme_goals(ticks = F,axis = F,grid = &quot;&quot;)+
  theme(axis.text.x = element_blank(),
        legend.position = &quot;none&quot;)+
  coord_fixed()+
  labs(x=&quot;&quot;,y=&quot;&quot;,subtitle=&quot;Goals scored per ingame minute&quot;) -> p_min

p_min</code></pre>
<p><img src="https://i2.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/goals_minute-1.png?w=450" data-recalc-dims="1"></p>
<p>The only ingame minute he has not scored yet is the 3rd and the 24th<a rel="nofollow" href="http://blog.schochastics.net/post/analyzing-the-greatest-strikers-in-football-ii-visualizing-data/#fn1" target="_blank"><sup>1</sup></a>. Let’s see how he compares with Messi an CR7 in that matter.</p>
<p><img src="https://i2.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/messi_cr7_minute-1.png?w=450" data-recalc-dims="1"></p>
<p>Messi seems to still miss a goal in the first minute. CR7 on the other hand managed to<br>
score in every single minute of a game. He is, however, not alone in this exclusive club.<br>
I found three more strikers (Alessandro Del Piero, Gerd Müller and Raúl) who, according to<br>
transfermarkt.co.uk, also scored in every minute of the game. The figure below shows their<br>
break downs per minute, together with five more strikers who come close to scoring in each minute.</p>
<p><img src="https://i1.wp.com/blog.schochastics.net/img/goals_per_min.png?w=456" data-recalc-dims="1"></p>
<p><em>I have to shamefully admit that I forgot to check Luis Suárez and Sergio Agüero in my initial analysis. Lo and behold,<br>
both have scored in 89 minutes!</em></p>
<p><img src="https://i2.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/suarez-1.png?w=450" data-recalc-dims="1"></p>
<p>Next, we will look at the standing of the match a goal was scored. Note that we have<br>
to adjust the standing for away games since it is given in the “wrong” order. That is,<br>
we need to treat 0:1 as 1:0. This can be done by reversing the string with <code>stringi::stri_reverse()</code>.<br>
Be careful though! This only works for single digit standings.</p>
<pre><code>stringi::stri_reverse(&quot;1:10&quot;)</code></pre>
<pre><code>## [1] &quot;01:1&quot;</code></pre>
<pre><code>zlatan_tbl$data %>% 
  mutate(standing_adj = ifelse(venue==&quot;A&quot;,stringi::stri_reverse(standing),standing)) %>% 
  group_by(standing_adj) %>% 
  summarise(goals = n()) %>% 
  ggplot(aes(x = reorder(standing_adj,-goals),y = goals))+
  geom_col(fill=&quot;#FF7F00&quot;)+
  theme_goals(grid = &quot;y&quot;)+
  labs(x = &quot;standing after goal&quot;, y = &quot;count&quot;) -> p_stand

p_stand</code></pre>
<p><img src="https://i2.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/goals_standing-1.png?w=450" data-recalc-dims="1"></p>
</div>
<div>
<h1>Type of Goal and the Providers</h1>
<p>The columns we haven’t looked at yet are the <code>type</code> and <code>provider</code>.</p>
<pre><code>zlatan_tbl$data %>% 
  dplyr::filter(provider!=&quot;&quot;) %>% 
  group_by(provider) %>% 
  summarise(goals = n()) %>% 
  top_n(10,goals) %>% 
  ggplot(aes(x = reorder(provider,goals),y = goals))+
  geom_col(fill=&quot;#FF7F00&quot;)+
  scale_y_continuous(breaks=seq(0,12,3))+
  theme_goals(grid = &quot;x&quot;)+
  labs(x = &quot;&quot;, y = &quot;count&quot;,subtitle=&quot;Top ten providers&quot;)+
  coord_flip() -> p_prov

zlatan_tbl$data %>% 
  dplyr::filter(type!=&quot;&quot;) %>% 
  group_by(type) %>% 
  summarise(goals = n()) %>% 
  ggplot(aes(x = reorder(type,goals),y = goals))+
  geom_col(fill=&quot;#FF7F00&quot;)+
  theme_goals(grid = &quot;x&quot;)+
  labs(x = &quot;&quot;, y = &quot;count&quot;,subtitle=&quot;Type of goal&quot;)+
  coord_flip() -> p_type

p_prov + 
  p_type + 
  plot_layout(nrow = 1) + 
  plot_annotation(theme=theme(
    plot.background=element_rect(fill=&quot;#666666&quot;,colour=&quot;#666666&quot;),
    plot.title = element_text(family = &quot;Roboto Condensed&quot;,color=&quot;white&quot;,face = &quot;bold&quot;,size=rel(1.5))),
    title=zlatan_tbl$name)</code></pre>
<p><img src="https://i0.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/goals_provider_type-1.png?w=450" data-recalc-dims="1"></p>
<p>According to his transfermarkt <a rel="nofollow" href="https://www.transfermarkt.co.uk/zlatan-ibrahimovic/profil/spieler/3455" target="_blank">profile</a>,<br>
Zlatan is “both-footed”, but he seems to prefer the right foot for scoring.</p>
<p>For comparison, here are the plots for Messi and CR7.<br>
<img src="https://i0.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/messi_cr7_provider_type-1.png?w=450" data-recalc-dims="1"><img src="https://i0.wp.com/blog.schochastics.net/post/2018-10-12-analyzing-the-greatest-strikers-in-football-ii-visualizing-data_files/figure-html/messi_cr7_provider_type-2.png?w=450" data-recalc-dims="1"></p>
</div>
<div>
<h1>Scorecard</h1>
<p>You can neatly put all plots together to create an informative scorecard for Zlatan.</p>
<pre><code>p &lt;- {p_date + p_stand + plot_layout(ncol = 2)}+
  {p_min + p_prov + p_type + plot_layout(ncol = 3)}+
  plot_layout(ncol = 1) + 
  plot_annotation(theme = theme(
    plot.background = element_rect(fill=&quot;#666666&quot;,colour=&quot;#666666&quot;)))
p</code></pre>
<p><img src="https://i2.wp.com/blog.schochastics.net/img/zlatan_scorecard.png?w=456" data-recalc-dims="1"></p>
<p>The code for the plots is kept as general as possible, so that it is easy to produce them<br>
quickly for any other player found on transfermarkt. Below are two more scorecards that<br>
were created in the same way as for Zlatan.</p>
<p><img src="https://i0.wp.com/blog.schochastics.net/img/lewandowski_scorecard.png?w=456" data-recalc-dims="1"></p>
<p><img src="https://i2.wp.com/blog.schochastics.net/img/aubameyang_scorecard.png?w=456" data-recalc-dims="1"></p>
</div>
<div>
<hr>
<ol>
<li>
<p>according to <a rel="nofollow" href="https://www.reddit.com/r/soccer/comments/6rug3f/players_who_have_scored_in_every_minute_of_a/" target="_blank">this</a> reddit post, Zlatan has in fact scored in every minute if one includes national team matches.<a rel="nofollow" href="http://blog.schochastics.net/post/analyzing-the-greatest-strikers-in-football-ii-visualizing-data/#fnref1" target="_blank"><img alt="↩" src="https://s.w.org/images/core/emoji/2.4/72x72/21a9.png" class="wp-smiley"></a></p>
</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.schochastics.net/post/analyzing-the-greatest-strikers-in-football-ii-visualizing-data/"> schochastics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/38jn41O7sxk" height="1"></a></div>