Skip to contents

contourPlot2 plots a contour map of a given quantity, such as the ground concentration of an airborne pollutant or odour, defined on a regular grid.


  x = "x",
  y = "y",
  z = "z",
  domain = NULL,
  background = NULL,
  underlayer = NULL,
  overlayer = NULL,
  legend = NULL,
  levels = NULL,
  size = 0,
  fill = TRUE,
  tile = FALSE,
  transparency = 0.75,
  colors = NULL,
  mask = NULL,
  inverse = FALSE,
  bare = FALSE



dataframe in long format, with three columns for Easting, Northing and values to be plotted.


name of the column with Easting data (default "x").


name of the column with Northing data (default "y").


name of the column with the values to be plotted (default "z").


optional list with six numeric values defining the boundaries of the domain to be plotted: minimum X, maximum X, minimum Y, maximum Y, number of ticks on X axis, number of ticks on Y axis.


optional path to a png file to be plotted as the base map.


optional list of layers to be plotted between base map and contour plot.


optional list of layers to be plotted on top of the contour plot.


optional title of the legend.


numeric vector of levels for contour plot. If not set, automatic pretty levels are computed. If -Inf and Inf are used as the lowest and highest limits of the array, the lowest and highest bands are unbounded and the legend shows < and >= symbols.


thickness of the contour line.


boolean (default TRUE). If TRUE the contour plot is filled with colour.


boolean (default FALSE). If TRUE rectangular tiles are plotted.


transparency level of the contour plot between 0.0 (fully transparent) and 1.0 (fully opaque). Default = 0.75).


colour palette for contour plot, as an array of colours.


path to shp file used as a mask. It must be a closed polygon.


logical. If TRUE areas on mask are masked. Default is to mask areas outside the polygon defined in the shp file.


boolean (default FALSE). If TRUE only the bare plot is shown: axis, legend, titles and any other graphical element of the plot are removed.


A ggplot2 object.


This is a convenience function to plot contour levels of a scalar quantity such as pollutants computed by a dispersion model, with ggplot2 version >= 3.3.0.

Data are required to be on a regular grid, typically (but not necessarily) in UTM coordinates. The input dataframe has to be in long format, i.e. one line per value to be plotted. The names of the columns corresponding to x, y and z can be specified in the input parameters.

If tile = TRUE data are shown as they are, without any graphical interpolation required for contour plots. This is helpful when you want to visualise the raw data. Since version 2.4.0, when tile = TRUE the intervals include the lowest bound and exclude the highest bound: [min, max). Note: In previous version it was the opposite.

underlayer and overlayer layers are ggplot2 objects to be shown at different levels of the vertical stack of the plot. These are useful to show topographical information related to the plot, such as sources or receptors locations.

When a shp file is given to the mask argument the plot is drawn only inside the polygon. In order to avoid boundary artifacts due to reduced resolution, original data are resampled to higher resolution (currently set to 10x the original one.) Ifinverse is set to TRUE, the plot is drawn outside the polygon. The mask feature is based on the same name function of the terra package. The CRS of the shp file is applied to the data in the data.frame. Please, keep in mind this feature is still experimental.


# Load example data in long format
volcano3d <- reshape2::melt(volcano)
names(volcano3d) <- c("x", "y", "z")
# Contour plot with default options
v <- contourPlot2(volcano3d)

# Set levels, and properly format the legend title:
             levels = c(-Inf, seq(100, 200, 20), Inf),
             legend = expression("PM"[10]~"["*mu*"g m"^-3*"]"))

# Sometimes, instead of a contour plot it is better to plot the original
# raster data, without any interpolation:
             levels = c(-Inf, seq(100, 200, 20), Inf),
             tile = TRUE)

# Since contourPlot2 returns a `ggplot2` object, you can add instructions as:
v + ggtitle("Example volcano data") +
    labs(x = NULL, y = NULL)