Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prasoonsharma/580533 to your computer and use it in GitHub Desktop.
Save prasoonsharma/580533 to your computer and use it in GitHub Desktop.
# ---------------- START: Who's the best allrounder in world cup cricket ----------------------
# read country and corresponding color info
country.color <- read.csv("country-colors.txt", header=T, sep=",")
# plot high runs/game and high wkts/game of players who've played at least N games in world cup cricket history
data <- world.cup.player.record[world.cup.player.record$ODI.played > 10 & !is.na(world.cup.player.record$Wickets.Per.Game) & !is.na(world.cup.player.record$Runs.Per.Game) & world.cup.player.record$High.Score.Probability > 0 & world.cup.player.record$High.Wicket.Probability > 0 & world.cup.player.record$Wickets.Per.Game > 0.8 & world.cup.player.record$Runs.Per.Game > 20,][,c("Player", "Country", "Runs.Per.Game", "Wickets.Per.Game", "High.Score.Probability", "High.Wicket.Probability", "Total.Batting.Runs.Scored", "Total.Wickets")]
# merge with country.color to get bubble color
data <- merge(data, country.color, by="Country")
max.wickets <- max(data$Total.Wickets)
max.runs.scored <- max(data$Total.Batting.Runs.Scored)
plot(data$Runs.Per.Game, data$Wickets.Per.Game, type="n", xlab="", ylab="", main="", axes=F)
# color the rectangular plot region to decrease the white space in the plot
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col = "gray")
# add contours
par(new=TRUE)
x <- 0:8
contour(outer(x, x), method = "edge", vfont = c("sans serif", "plain"), drawlabels = FALSE, axes = FALSE, frame.plot = F, ylim=c(0,1.1), lty=2)
# add bubble plot
par(new=TRUE)
symbols(data$Runs.Per.Game, data$Wickets.Per.Game, circles=((data$Total.Wickets/max.wickets) + (data$Total.Batting.Runs.Scored/max.runs.scored)), inches=1/5, bg=as.character(data$Color), xlab="Runs per game (average) in World Cup games", ylab="Wickets per game (strike rate) in World Cup games", main="Who's the best all rounder in World Cup Cricket history?")
# write player name next to the bubble
text(data$Runs.Per.Game, data$Wickets.Per.Game, data$Player, cex=0.75, col="black", adj=c(-0.3,0), xpd=NA, font=2)
legend(20, 2.15,country.color$Country, country.color$Color, cex=0.6, horiz=T, text.width=1.5, x.intersp=0.2)
text(26, 2, "Bubble size represents normalized total wickets and runs in Cricket World Cup", cex=0.6, col="black")
# add image of best all rounder
par(xpd=T, mar=c(0,0,0,0),fig=c(0.4,0.65,0.5,0.75), new=TRUE)
# install.packages("ReadImages")
library(ReadImages)
imran <- read.jpeg('imran.jpg')
plot(imran)
# ---------------- END: Who's the best allrounder in world cup cricket ----------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment