Skip to content

6. Visualization and animation

abdelkaderm edited this page Aug 4, 2025 · 5 revisions

Basic Visualization

Standard Plots for esd Objects

The main data visualization in the esd package is provided through the plot and map methods. Additionally, there are more sophisticated methods for producing advanced graphs. For esd objects such as station, field, eof, pca, and ds, standard visualization utilizes S3 methods. For example, for a station object x, the command plot(x) internally calls plot.station(x).

Basic Commands:

  • Plotting:
    plot(x, ...)
  • Mapping:
    map(x, FUN, ...)

In the map() function:

  • If the argument FUN is not null, map(x, FUN) produces a colored map by applying the function FUN to x.
  • For example, to produce a map of standard deviations:
    map(x, FUN = 'sd')
    This applies to other esd objects like field.

Example 1: Visualizing Daily Precipitation Data at Bjornholt

Load daily precipitation data recorded at Bjornholt:

data(bjornholt)
x <- bjornholt
  • Plot the time series:

    plot(x)
  • Produce a map centered on the location:

    map(x, xlim = c(lon(x) - 10, lon(x) + 10), cex = 2, add.text = TRUE, full.names = TRUE)

Example 2: Mapping Mean Values at Weather Stations

To produce a map of mean values at weather stations from the NACD dataset:

X <- station(src = 'NACD', param = 't2m')
map(X, FUN = 'mean', add.text = TRUE, xlim = c(-20, 40), ylim = c(40, 90))

Other Types of Graphics

The esd package provides several additional visualization tools:

Function Description
diagnose Gives an overview of station records and data quality or post-processed results.
vis Displays a data matrix for a single station (x-axis: day of the year, y-axis: each year).
graph Presents downscaled results from a multi-model ensemble.
wheel Creates a wheel visualization for precipitation and temperature (angle: day of the year).
balls Generates fancy symbols for visualization.
climvar Shows seasonal variations in year-to-year variance on a daily basis.
corfield Displays correlation maps.
cumugram Shows accumulated mean or sum for a given year compared to previous years.
scatter.hexbin Produces scatterplots with hexagonal binning.
scatter.sunflower Creates sunflower scatterplots.
windrose Plots windroses for wind data at a station.
vec Draws arrows, e.g., on top of a contour plot.

Using the esd Package in Shiny Apps

The esd package can be used as an engine in R-Shiny apps. An example is the OpenClimateDataPrototype, a test site for an R-Shiny app that presents daily climate data from a network of stations.

The code for the app is available on GitHub.


Using esd to Create Animations

To create animations, you can integrate the esd package with animation libraries such as animation. Below is an example of creating a GIF of sea surface temperatures (SSTs) with wind arrows superimposed:

Example: Creating an Animation of SSTs with Wind Arrows

This example demonstrates how to create an animation of Sea Surface Temperatures (SSTs) with wind arrows superimposed, using the esd package in combination with the animation package.


Code Example

Below is the R code for generating the animation:

library(esd)
library(animation)

# Define a function to create frames for the animation
frame <- function(x1, x2, x3, it) {
  cat('.')
  par(bg = 'black', col.axis = 'white', col.lab = 'white', col.main = 'white')
  
  # Subset data for the specific time index
  y1 <- subset(x1, it = it)
  y2 <- subset(x2, it = it)
  y3 <- subset(x3, it = it)
  
  # Plot the SST map
  map(y1, 
      projection = 'sphere', 
      main = paste(month.abb[month(y1)], year(y1)), 
      lonR = -140, 
      style = 'night',
      colbar = list(breaks = seq(-5, 35, by = 1)), 
      new = FALSE)
  
  # Overlay wind vectors
  vec(y2, y3, 
      projection = 'sphere', 
      lonR = 120, 
      r = 1.1, 
      a = 0.7, 
      length = 0.03, 
      nx = 70, 
      ny = 70, 
      new = FALSE)
}

# Retrieve data for SSTs and wind components
print('Get data')
sst <- retrieve('~/data/ERA5/ERA5_sst_mon.nc', it = c(2015, 2018))
u10 <- retrieve('~/data/ERA5/ERA5_u+v_mon.nc', param = 'u10', it = c(2015, 2018))
v10 <- retrieve('~/data/ERA5/ERA5_u+v_mon.nc', param = 'v10', it = c(2015, 2018))

# Determine the number of time steps in the dataset
n <- length(index(sst))
print(n)

# Generate the GIF animation
print('GIF:')
saveGIF({
  for (i in 1:n) frame(sst, u10, v10, i)
}, movie.name = "elninoanim.gif", interval = 0.5)

Explanation of Code

  1. Defining the frame Function:

    • This function creates individual frames for the animation by:
      • Plotting the Sea Surface Temperature (SST) map using the map() function.
      • Overlaying wind vectors using the vec() function.
    • Customizations include:
      • projection = 'sphere': Renders the map in a spherical projection.
      • style = 'night': Applies a dark background for better visibility.
      • colbar: Sets the color bar with specific breaks for temperature ranges.
      • nx and ny: Adjust the density of wind arrows on the map.
  2. Data Preparation:

    • The retrieve() function is used to load SST data (sst) and wind component data (u10 and v10) for the specified time period (2015–2018).
  3. Animation Creation:

    • The saveGIF() function generates a GIF animation by iterating through time steps (n) and calling the frame function for each time step.
    • The resulting animation is saved as elninoanim.gif with an interval of 0.5 seconds between frames.

Example Output

You can view the resulting animation here.


Notes:

  • Dependencies: Ensure the esd and animation R packages are installed before running the script.
  • Customization: You can adjust parameters such as lonR, nx, ny, or the color bar to suit your dataset and visualization preferences.
  • Data Requirement: The input NetCDF files (ERA5_sst_mon.nc, ERA5_u+v_mon.nc) must be downloaded and accessible at the specified file paths.

This example highlights the flexibility of the esd package in creating dynamic visualizations of climate data, making it a powerful tool for climate analysis and communication.

Clone this wiki locally