Open In App

How to Draw Rainfall Runoff Graph in R Using ggplot?

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Rainfall-runoff analysis is an essential concept in hydrology, often used to study the relationship between rainfall and the resulting runoff from a watershed or catchment area. This helps hydrologists and environmental scientists understand water flow and predict flooding events. In this article, we will explore how to create a rainfall-runoff graph using R and the ggplot2 package, a powerful tool for creating high-quality visualizations using the R Programming Language.

Understanding Rainfall Runoff Graphs

A rainfall-runoff graph typically shows two key variables:

  • Rainfall: The quantity of water precipitated in a given area at a certain period is in millimeters.
  • Runoff: This is the amount of water flow (usually given in cubic meters per second, m³/s) caused by the rainfall depending on soil type, the slope, and the wetness of the catchment area previously.

The intended use of the graph is to represent the relationship between rainfall events and runoff within a given time frame. It is common practice to describe rainfall using a bar chart while runoff is described using a line chart on the same time axis, using separate scales on the y-axis since the two are in different units. Before we begin, make sure you have the following packages installed:

library(ggplot2)
library(scales)

Now we will discuss step by step How to Draw Rainfall Runoff Graph in R Using ggplot:

Step 1: Prepare the Data

We will start by creating a sample dataset that includes hourly rainfall and runoff measurements for a specific period. This data could represent a rainfall event and the corresponding runoff generated by a catchment.

R
# Sample data representing hourly rainfall (in mm) and runoff (in cubic meters per second)
rainfall_runoff_data <- data.frame(
  Time = 1:24,  # Time in hours
  Rainfall = c(0, 5, 12, 8, 20, 35, 50, 60, 80, 70, 50, 30, 15, 8, 
                          5, 2, 1, 0, 0, 0, 0, 0, 0, 0),  # Rainfall (mm)
  Runoff = c(0, 1, 3, 5, 10, 15, 25, 35, 50, 65, 70, 60, 45, 30, 20, 
             10, 5, 3, 1, 0, 0, 0, 0, 0)   # Runoff (cubic meters per second)
)

# Display the data
head(rainfall_runoff_data)

Output:

  Time Rainfall Runoff
1 1 0 0
2 2 5 1
3 3 12 3
4 4 8 5
5 5 20 10
6 6 35 15

Step 2: Create the Rainfall-Runoff Graph

The rainfall-runoff graph is a combination plot where we represent rainfall as a bar chart and runoff as a line graph. To achieve this, we'll use ggplot2 to create both layers in a single plot.

R
# Create the base ggplot object
rainfall_runoff_plot <- ggplot(rainfall_runoff_data, aes(x = Time)) +
  # Add bars for Rainfall data
  geom_bar(aes(y = Rainfall), stat = "identity", fill = "blue", alpha = 0.4, width = 0.8) +
  # Add a line for Runoff data
  geom_line(aes(y = Runoff), color = "red", size = 1.2) +
  # Add points to highlight Runoff data
  geom_point(aes(y = Runoff), color = "red", size = 2) +
  # Set up the scales and labels
  scale_y_continuous(
    name = "Rainfall (mm) / Runoff (cubic meters per second)",
    sec.axis = sec_axis(~., name = "Runoff (cubic meters per second)")
  ) +
  scale_x_continuous(breaks = seq(0, 24, by = 2)) +
  # Add titles and labels
  labs(
    title = "Rainfall-Runoff Graph",
    x = "Time (Hours)",
    y = "Rainfall (mm)"
  ) +
  # Customize the theme
  theme_minimal() +
  theme(
    axis.title.y.left = element_text(color = "blue", size = 12),
    axis.title.y.right = element_text(color = "red", size = 12),
    axis.text.y.left = element_text(color = "blue"),
    axis.text.y.right = element_text(color = "red"),
    plot.title = element_text(hjust = 0.5, face = "bold", size = 16)
  )

# Display the graph
print(rainfall_runoff_plot)

Output:

gh
Create the Rainfall-Runoff Graph
  • geom_bar(): Creates the bar plot for rainfall.
  • geom_line() and geom_point(): Adds a line and points to visualize runoff data.
  • scale_y_continuous() with sec.axis: Allows the addition of a secondary y-axis to differentiate between rainfall and runoff.
  • theme_minimal(): provides a clean background, while theme() customizes the text and labels.

Step 3: Enhancing the Graph

Let's enhance the graph further by adding more visual elements, such as labels and legends:

R
# Enhanced Rainfall-Runoff Plot
rainfall_runoff_plot_enhanced <- ggplot(rainfall_runoff_data, aes(x = Time)) +
  geom_bar(aes(y = Rainfall, fill = "Rainfall"), stat = "identity", alpha = 0.6, width = 0.8) +
  geom_line(aes(y = Runoff, color = "Runoff"), size = 1.2) +
  geom_point(aes(y = Runoff, color = "Runoff"), size = 2) +
  scale_fill_manual(values = c("blue")) +
  scale_color_manual(values = c("red")) +
  scale_y_continuous(
    name = "Rainfall (mm)",
    sec.axis = sec_axis(~., name = "Runoff (cubic meters per second)")
  ) +
  scale_x_continuous(breaks = seq(0, 24, by = 2)) +
  labs(
    title = "Enhanced Rainfall-Runoff Graph",
    x = "Time (Hours)",
    y = "Rainfall (mm)"
  ) +
  theme_minimal() +
  theme(
    axis.title.y.left = element_text(color = "blue", size = 12),
    axis.title.y.right = element_text(color = "red", size = 12),
    axis.text.y.left = element_text(color = "blue"),
    axis.text.y.right = element_text(color = "red"),
    plot.title = element_text(hjust = 0.5, face = "bold", size = 16),
    legend.position = "bottom",
    legend.title = element_blank()
  )

# Display the enhanced graph
print(rainfall_runoff_plot_enhanced)

Output:

gh
Draw Rainfall Runoff Graph in R Using ggplot

The rainfall-runoff graph created in R provides valuable insights:

  • Blue Bars: Represent rainfall intensity over time. High bars indicate heavy rainfall during specific hours.
  • Red Line and Points: Represent the corresponding runoff generated due to rainfall. The runoff typically increases as rainfall peaks and gradually decreases afterward.

Conclusion

Drawing a rainfall-runoff graph in R using ggplot2 provides a clear visualization of the relationship between rainfall events and the resulting runoff. By combining bar plots and line graphs, you can effectively communicate complex hydrological data. You can apply this technique to real-world datasets to analyze and predict runoff behavior, aiding in water resource management and flood risk assessment.


Next Article

Similar Reads