Introduction

In this document, we will create a heatmap to visualize the number of wins and losses for different teams. The data has been processed to include the total number of wins and losses for each team, and we will use this information to generate a heatmap.

About ‘ggplot2’ Library

‘ggplot2’ is one of the most widely used packages for data visualization in R. Developed by “Hadley Wickham”, ‘ggplot2’ implements the grammer of graphics, a powerfull framework for creating complex and aesthetically pleasing visualizations. The package allows you to build plots layer by layer, starting with the data and mapping variables to visual properties, and then adding elements like geometries, scales, and themes.

Key features of ‘ggplot2’ include”

  • Aesthetic Mapping: ggplot2 allows you to map variables in your dataset to visual properties such as position, color, size, and shape.
  • Layered Approach: You can build a plot step by step, starting with the data and then adding layers like geometries (e.g., points, lines, bars), scales, and facets.
  • Extensibility: ggplot2 is highly extensible, allowing users to create custom plots and themes.
  • Themes: The package includes a variety of themes to change the overall look of your plot with minimal effort. ggplot2 is an essential tool for any R user looking to create data visualizations, offering flexibility and control over the appearance of your plots.

Load Necessary Libraries

library(ggplot2)
library(reshape2)
  • library(reshape2): This command loads the reshape2 package, which provides tools for reshaping data between wide and long formats. The melt() function from this package will be used to prepare our data for the heatmap.

  • library(ggplot2): This command loads the ggplot2 package, which will be used to create the heatmap. ggplot2 allows us to build plots layer by layer, offering extensive customization options.

Load Data

# Read the CSV file into a data frame
Icc_test_2024 <- read.csv("Icc_Test_2024.csv")
head(Icc_test_2024)
##   X.1 X       TEAMS  M W L T D N.R PT   PCT Series.Form             Next
## 1   1 1       India  9 6 2 0 1   0 74 68.51       LWWWW  vs BAN, BAN, NZ
## 2   2 2   Australia 12 8 3 0 1   0 90 62.50       WWLWW vs IND, IND, IND
## 3   3 3 New Zealand  6 3 3 0 0   0 36 50.00       WWWLL   vs SL, SL, IND
## 4   4 4   Sri Lanka  4 2 2 0 0   0 24 50.00        LLWW vs ENG, ENG, ENG
## 5   5 5    Pakistan  5 2 3 0 0   0 22 36.66       WWLLL vs BAN, BAN, ENG
## 6   6 6     England 13 6 6 0 1   0 57 36.54       LLWWW    vs SL, SL, SL
##   Total_wins Total_lose
## 1          4          1
## 2          4          1
## 3          3          2
## 4          2          2
## 5          2          3
## 6          3          2

Prepare Data for the Heatmap

# Melt the data for heatmap
melted_data <- melt(Icc_test_2024[, c("TEAMS", "Total_wins", "Total_lose")], id.vars = "TEAMS")

melt(Icc_test_2024[, c(“TEAMS”, “Total_wins”, “Total_lose”)], id.vars = “TEAMS”):

  • The melt() function transforms the data from wide format to long format.
  • Here, we are selecting the TEAMS, Total_wins, and Total_lose columns from the Icc_test_2024 data frame.
  • The id.vars = “TEAMS” argument keeps the TEAMS column as an identifier, while the other columns are melted into key-value pairs, making it easier to plot the heatmap.

Create the Heatmap

# Create the heatmap
ggplot(melted_data, aes(x = TEAMS, y = variable, fill = value)) +
  geom_tile() +
  labs(title = "Heatmap of Wins and Losses",
       x = "Teams", y = "Outcome") +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +  # Improved color scheme
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))  # Center the title

ggplot(melted_data, aes(x = TEAMS, y = variable, fill = value)):

  • This initializes the ggplot2 plotting system with melted_data as the dataset.
  • The aes() function sets up the aesthetic mappings: x = TEAMS places the teams on the x-axis, y = variable places the outcome types (“Total_wins” or “Total_lose”) on the y-axis, and fill = value maps the number of wins or losses to the fill color of the tiles.
geom_tile():
  • This function creates the heatmap by drawing a tile for each combination of TEAMS and variable with a fill color corresponding to the value.
labs(title = “Heatmap of Wins and Losses”, x = “Teams”, y = “Outcome”):
  • This adds labels to the plot. The title sets the main title of the plot, x sets the label for the x-axis, and y sets the label for the y-axis.

scale_fill_gradient(low = “lightblue”, high = “darkblue”):

  • This defines the color gradient for the heatmap, with low = “lightblue” for the minimum value and high = “darkblue” for the maximum value.

theme_minimal():

  • This function applies a minimalistic theme to the plot, removing grid lines and background color for a cleaner appearance.

theme(plot.title = element_text(hjust = 0.5)):

  • This centers the plot title using ‘hjust = 0.5’, where ‘hjust’ stands for horizontal justification.

Conclusion

This heatmap provides a clear visual representation of the number of wins and losses for each team. By using a gradient color scale, the differences in performance across teams are easily distinguishable, helping to identify which teams have the most consistent records. The ‘ggplot2’ package makes it easy to create such visualizations, offering a range of customization options to tailor the plot to your specific needs.