Using plotdap

2019-05-02

plotdap

The R package plotdap makes it easy to visualize ‘tabledap’ and ‘griddap’ objects obtained via the rerddap package. Maps can be made using either base or ggplot2 graphics, and the user does not need to know the intricacies of obtaining continental outlines, projections, and combining those with the data to form maps. Animations of the data are also readily obtained. plotdap works in a similar fashion with either tables or grids return by rerddap further simplifying the mapping process.

A user who desires fine control of their maps should learn how to map the data themselves - some examples are give in the rerddap vignette. But plotdap provides a simplified workflow of obtaining data using rerddap and quickly and simply mapping the data. In what follows we go over how to install plotdap and it’s basic usage, as well as how to utilize some of the more important options in the package in order to improve the map.

Installation

plotdap can be installed from CRAN with:

and the development version can be installed with:

Getting started with plotdap()

The plotdap() function makes it easy to visualize data acquired via rerddap::tabledap() or rerddap::griddap(). Regardless of the data you want to visualize, you’ll always want to start a plot via plotdap(), where you may specify some “global” plotting options. Subsequent sections will demonstrate how to add tables/grids via add_tabledap()/add_griddap(), but for now we’ll focus on options provided by plotdap(). Most importantly, the first argument decides whether base or ggplot2 graphics should be used for the actual plotting.

In addition to choosing a plotting method, plotdap() is where you can define properties of the background map, including the target projection using a valid coordinate reference system (CRS) definition. Projection is performed using the PROJ.4 library, and spatialreference.org is a great resource for finding PROJ.4 CRS descriptions. Using the search utility, you can for example, search for “South Pole” and pick from a number of options. Here I’ve chosen the MODIS South Pole Stereographic option and copy-pasted the Proj4 page with the CRS definition:

You might notice that some projections aren’t “well-defined” on a global scale, and thus, may result in an error, or a “broken” looking map. For instance, this Albers projection centered on Alaska:

That does not mean we can’t use this (or similar) projections – we just have to be careful that they are sensible given the lat/lon limits. By default, those limits span the entire world, but as we’ll see later, the limits are shrunk to the given data (i.e., griddap() / tabledap()) limits. In other words, we should expect this projection to work once we “add” some data located near Alaska to the visualization. However, in case you want to make a map without any data, or want to customize the background map in some special way, you can supply an sf object (or something coercable to an sf object) to the mapData argument.

With the odd exception of window sizing and projections, the options in plotdap() should just work in a similar way for either plotting method. However, there are some useful options that are deliberately left out, since they work differently based on the plotting method.

The mapData argument can be used to change the resolution of the continental outlines or to limit the outline to a pre-selected area, which can speed up processing because fewer unused polygons need to be clipped. This is particularly important for maps that will cross the dateline. So for example to use the hi-res outlines for VIIIRS SST off the coast of North America:

Maps that cross the dateline work better using the “world2” or “world2Hires” databases frpm the mapdata package. There is a known problem with that continental database in that polygons from certain countries cause artificial lines in the map, and must removed, as done below.

Since the result of plotdap() is always a map, it always forces a fixed aspect ratio (i.e., \(\frac{height}{width}\) of graph equals \(r=\frac{latitude}{longitude}\)). For this reason, the current size of your graphics device may not be sensible for the value of \(r\) (for instance, if \(r\) is high, but the height of the graphics device is small, you may see an error such as: polygon edge not found since the device cannot possibly render the result under the conditions). For a number of reasons, plotdap() will not automatically resize your graphics device; instead, it’s recommended that you use a reliable graphics device such as Cairo, and use a height/width ratio close to \(r\).

More advanced users that know some base/ggplot2 plotting may want more control of certain aspects of the plot (a later section – Customizing plotdap() objects – covers this topic).

Adding tabledap() layers

The add_tabledap() function allows you to add markers that encode variable(s) obtained via tabledap() to an existing plotdap() object. For example, suppose we have the following sardines data, and wish to understand the frequency of subsample counts:

At the very least, add_tabledap() needs a base map (i.e., a plotdap() object), the tabledap() data, and a formula defining the variable of interest (for encoding the color of the markers). In R, you can create a formula by prefixing ~ to some expression. This formula can simply reference a variable already residing in the dataset (e.g., ~subsample_count) or it can be a function of some variables (e.g. ~log2(subsample_count)):

Modifying tabledap() layers

It is also easy to alter the color scale as well as the symbol type and size in add_tabledap() via the color, shape and size arguments.

For further details about these arguments, please refer to the documentation on help(add_tabledap).

Adding griddap() layers

Similar to add_tabledap(), the add_griddap() function makes it easy to add rasters (i.e., rectangular tiles) to a plotdap() object. To demonstrate, lets obtain some of the latest sea surface temperatures along the western coast of the US.

Again, similar to add_tabledap(), add_griddap() needs a base map (i.e., a plotdap() object), the griddap() data, and a formula defining the variable of interest (for encoding the fill of the rectangles). The add_griddap() function also has a maxpixels argument which sets a maximum threshold for the number of cells (i.e., pixels) to use before projection and plotting occurs. Compared to ggplot2, base plotting is much more efficient at rendering raster objects, so it might be worth increasing the threshold in that case:

The murSST_west grid has a single time point (i.e., length(unique(murSST_west$data$time)) == 1), but what do we do when there are multiple time points? In addition to animating multiple grids (a la add_tabledap()), you also have the option to summarize multiple grids into a single grid. To demonstrate, lets grab some wind speeds measured along the west coast of the US.

When faced with multiple time periods, and animate = FALSE (the default), the time argument is used to reduce multiple grids (i.e., raster bricks) to a single grid (i.e., a single raster layer). You can pass any R function to the time argument, but when animate = FALSE, you should take care to ensure the function returns a single value. The default uses the mean(na.rm = TRUE) function so that each cell represents the average (in this case amongst three time points), but we could easily set this to var() to get the variance for each cell:

Changing the layer order

By default, add_griddap plots the land first and then the grid on top of that. For many uses that is desirable, but at other times it is more desirable to have the land mask the grid. This can now be done in the print method, by saving the plotdap object and printing with the option “landmask = TRUE”.

and compare when land is plotted over the grid:

maxpixels

Images of satellite data can contain a large number of pixels. add_griddap() allows for a differing number of pixels to be used (default is 10,000) by setting the parameter “maxpixels”. When the actual number pixels is larger than the value of “maxpixels”, the image is sub-sampled. This can greatly affect the how the image looks.