Brian Lara’s runs in test cricket by Opponent

Data

This plot uses the lara_tests data frame of the gcubed package.

This data was obtained from ESPN Cricinfo.

library(gcubed)
head(lara_tests)
## # A tibble: 6 x 8
##    Runs Inning Notout DNB   Opp          Ground     `Start Date` MatchNum
##   <int> <fct>  <lgl>  <lgl> <chr>        <chr>      <chr>        <chr>   
## 1    44 1      FALSE  FALSE Pakistan     Lahore     6-Dec-90     1158    
## 2     5 2      FALSE  FALSE Pakistan     Lahore     6-Dec-90     1158    
## 3    17 1      FALSE  FALSE South Africa Bridgetown 18-Apr-92    1188    
## 4    64 2      FALSE  FALSE South Africa Bridgetown 18-Apr-92    1188    
## 5    58 1      FALSE  FALSE Australia    Brisbane   27-Nov-92    1202    
## 6     0 2      FALSE  FALSE Australia    Brisbane   27-Nov-92    1202

Code for plot

First, create a data frame aggregating runs by opponent. (Note that this can also be done using the aggregate function of base R):

library(dplyr)

df <- group_by(lara_tests, Opp) %>% 
  summarise(Runs = sum(Runs, na.rm = TRUE)) %>% 
  arrange(desc(Runs)) 

head(df)
## # A tibble: 6 x 2
##   Opp           Runs
##   <chr>        <int>
## 1 England       2983
## 2 Australia     2856
## 3 South Africa  1715
## 4 Pakistan      1173
## 5 Sri Lanka     1125
## 6 India         1002
library(ggplot2)
library(scales) #to get commas in formatting numerical values on the y-axis

df$Opp <- factor(df$Opp, levels =df$Opp) # gets the order of the bars right  

bcl_runs_plt <- ggplot(df, aes(x = Opp, y = Runs)) + 
  geom_bar(stat = "identity", fill = "mediumpurple", colour = "black") +
  xlab("Opponent") + 
  ggtitle("Brian Lara Test Career (1990-2006)")+
  scale_y_continuous(label = comma)+ 
  theme( plot.title = element_text(size = 16, face = "bold", hjust = 0.5))

bcl_runs_plt