In this R Markdown document, we’ll demonstrate how to visualize ICC ODI rankings by country using various R packages. We’ll use geographic data and rankings from a CSV file to create a map showing the rankings.
library(sf) # For handling spatial data
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(rnaturalearth) # For accessing world map data
library(rnaturalearthdata) # Provides the data for rnaturalearth
##
## Attaching package: 'rnaturalearthdata'
## The following object is masked from 'package:rnaturalearth':
##
## countries110
library(ggplot2) # For creating the plot
In this code chunk, we load the libraries needed for our analysis:
#Load world map data
world <- ne_countries(scale="medium", returnclass = "sf")
In the above given code chunk , we load the world map data using the ‘ne_countries’ function from the ‘rnaturalearth’ package. The ‘scale’ argument determines the detail level of the map and ‘returnclass = “sf” specifies that the data should be returned as an ’sf’ (simple feature) object, which is sutitable for spatial plotting.
# Load ICC ODI data form csv file using read.csv function
icc_odi <- read.csv("icc_odi_ranking.csv", header = TRUE)
head(icc_odi)
## Position Team Matches Points Rating
## 1 1 India 42 5117 122
## 2 2 Australia 34 3936 116
## 3 3 South Africa 30 3357 112
## 4 4 Pakistan 26 2762 106
## 5 5 New Zealand 33 3349 101
## 6 6 England 28 2672 95
In the above given code we read the ICC ODI ranking data from a csv file using ‘read.csv’. The ‘header = True’ argument indicates that the first row of the csv file contains column names. We then use ‘head(icc_odi)’ to display the first few rows of the data, so we can check its structure,
# Reaname columns for clarity
colnames(icc_odi) <- c("Position","Team", "Matches","Points","Rating")
print(icc_odi)
## Position Team Matches Points Rating
## 1 1 India 42 5117 122
## 2 2 Australia 34 3936 116
## 3 3 South Africa 30 3357 112
## 4 4 Pakistan 26 2762 106
## 5 5 New Zealand 33 3349 101
## 6 6 England 28 2672 95
## 7 7 Sri Lanka 47 4363 93
## 8 8 Bangladesh 40 3453 86
## 9 9 Afghanistan 31 2477 80
## 10 10 West Indies 32 2205 69
## 11 11 Scotland 28 1450 52
## 12 12 Ireland 22 1091 50
## 13 13 Zimbabwe 24 1181 49
## 14 14 Netherlands 34 1482 44
## 15 15 Canada 9 320 36
## 16 16 Namibia 24 756 32
## 17 17 Nepal 35 1095 31
## 18 18 Oman 24 581 24
## 19 19 U.S.A. 20 410 21
## 20 20 U.A.E. 30 345 12
We rename or rearrange the columns of the ‘icc_odi’ data frame for clarity. The columns represent * ‘Position’ ; The ranking position of the team. * ‘Team’ : The name of the team. * ‘Mathces’m’Points’, and’Rating’ : Additional information about the team’s performance. We print the ‘icc_odi’ data frame to verify the changes.
# Merge the world data with the ICC data
world <- merge(world, icc_odi, by.x = "name", by.y = "Team", all.x = TRUE)
world$Position <- as.numeric(world$Position)
We merge the world map data with the ICC ODI data using the merge function. The by.x and by.y arguments specify the columns to match from each data frame (“name” from world and “Team” from icc_odi). After merging, we convert the Position column to numeric format to ensure it is suitable for plotting.
# Plot the data
ggplot(data = world) +
geom_sf(aes(fill = Position), color = "white") +
scale_fill_gradient(low = "darkgreen", high = "lightpink", na.value = "grey90") +
labs(title = "ICC ODI Rankings by Country 2024",
fill = "Position") +
theme_minimal() +
theme(legend.position = "right",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(hjust = 0.5))
In this final step, we use ggplot2 to create a map visualization:
##Explanation of Theme Elements theme_minimal(): Applies a minimal theme to the plot, which provides a clean and simple design without heavy gridlines or background colors.
theme(legend.position = “right”): Places the legend on the right side of the plot.
axis.text = element_blank(): Removes axis text labels to focus on the map and the color gradient.
axis.title = element_blank(): Removes axis titles to maintain a clean appearance.
panel.grid = element_blank(): Removes gridlines from the plot panel for a cleaner look.
plot.title = element_text(hjust = 0.5): Centers the plot title horizontally.
This R Markdown document demonstrates how to visualize ICC ODI rankings by country using R packages for spatial data and plotting. The resulting map provides a clear representation of team rankings globally.