R Programming Tutorial - contour()

contour()

A Simple Example

# Data
x <- -10:10
y <- -10:10
z <- sqrt(outer(x ^ 2, y ^ 2, "+"))

contour(x, y, z)

# Both produce the same plot
contour(z)

Example with Data

Using base volcano data set

tibble::as.tibble(volcano)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## Please use `as_tibble()` instead.
## The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
## Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
## # A tibble: 87 × 61
##       V1    V2    V3    V4    V5    V6    V7    V8    V9   V10   V11   V12   V13
##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1   100   100   101   101   101   101   101   100   100   100   101   101   102
##  2   101   101   102   102   102   102   102   101   101   101   102   102   103
##  3   102   102   103   103   103   103   103   102   102   102   103   103   104
##  4   103   103   104   104   104   104   104   103   103   103   103   104   104
##  5   104   104   105   105   105   105   105   104   104   103   104   104   105
##  6   105   105   105   106   106   106   106   105   105   104   104   105   105
##  7   105   106   106   107   107   107   107   106   106   105   105   106   106
##  8   106   107   107   108   108   108   108   107   107   106   106   107   108
##  9   107   108   108   109   109   109   109   108   108   107   108   108   110
## 10   108   109   109   110   110   110   110   109   109   108   110   110   113
## # … with 77 more rows, and 48 more variables: V14 <dbl>, V15 <dbl>, V16 <dbl>,
## #   V17 <dbl>, V18 <dbl>, V19 <dbl>, V20 <dbl>, V21 <dbl>, V22 <dbl>,
## #   V23 <dbl>, V24 <dbl>, V25 <dbl>, V26 <dbl>, V27 <dbl>, V28 <dbl>,
## #   V29 <dbl>, V30 <dbl>, V31 <dbl>, V32 <dbl>, V33 <dbl>, V34 <dbl>,
## #   V35 <dbl>, V36 <dbl>, V37 <dbl>, V38 <dbl>, V39 <dbl>, V40 <dbl>,
## #   V41 <dbl>, V42 <dbl>, V43 <dbl>, V44 <dbl>, V45 <dbl>, V46 <dbl>,
## #   V47 <dbl>, V48 <dbl>, V49 <dbl>, V50 <dbl>, V51 <dbl>, V52 <dbl>, …

The basic plot

contour(volcano)

Adjusting size and style of contour lines

contour(volcano,
        lwd = 2,
        lty = 8)

contour(volcano,
        lwd = 2,
        lty = 8,
        labels = c("A","B","C","D"))

Changing contour label text size

contour(volcano,
        lwd = 3,
        lty = 5,
        labels = c("A","B","C","D"),
        labcex = 1)

Or removing the labels altogether

contour(volcano,
        lwd = 3,
        lty = 5,
        drawlabels = F)

Can also create a filled contour mapping

filled.contour(volcano)

Defining the number of levels

filled.contour(volcano,
               nlevels = 30)

Modifying color schemes

filled.contour(volcano,
               nlevels = 30,
               color.palette = terrain.colors)

Adding contour over the filled contour

filled.contour(volcano,
               nlevels = 30,
               color.palette = terrain.colors,
               plot.axes = {
                   axis(1)
                   axis(2)
                   contour(volcano, add = TRUE, lwd = 2)
               })