R Graphics · Contour Plot

Visualizing Surfaces with Contour Plots in R

Create basic, filled, labeled, custom-level, overlaid, and perspective-linked contours.

6 examplesReproducible R code
Before you begin

Choose the variation that serves the analysis

Contour plots connect locations with equal values on a two-dimensional surface. They translate three-dimensional structure into readable levels, gradients, peaks, and valleys.

Examples
06
Levels
Beginner + Intermediate
01
Example 01Base RBeginner

Create a Basic Contour Plot

Contour lines connect combinations of x and y that share the same z value.

Create a Basic Contour Plot example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x<-seq(-3,3,length=60);y<-x;z<-outer(x,y,function(a,b) exp(-(a^2+b^2)/2));contour(x,y,z,col="#2563eb",lwd=2)
02
Example 02Base RBeginner

Use Filled Contours

Filled bands make ranges of surface height visually immediate.

Use Filled Contours example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x<-seq(-3,3,length=60);y<-x;z<-outer(x,y,function(a,b) sin(a)*cos(b));filled.contour(x,y,z,color.palette=function(n) hcl.colors(n,"Blues 3"))
03
Example 03Base RBeginner

Label Contour Levels

Inline labels state the numeric value represented by each isoline.

Label Contour Levels example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x<-seq(-3,3,length=60);y<-x;z<-outer(x,y,function(a,b) exp(-(a^2+b^2)/2));contour(x,y,z,drawlabels=TRUE,col="#2563eb")
04
Example 04Base RIntermediate

Choose Meaningful Levels

Custom levels focus attention on analytically important thresholds.

Choose Meaningful Levels example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x<-seq(-3,3,length=60);y<-x;z<-outer(x,y,function(a,b) exp(-(a^2+b^2)/2));contour(x,y,z,levels=c(.1,.25,.5,.75,.9),lwd=2,col=hcl.colors(5,"Blues 3"))
05
Example 05Base RIntermediate

Overlay Observations

Points can show sampled locations on top of the interpolated or theoretical surface.

Overlay Observations example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x<-seq(-3,3,length=60);y<-x;z<-outer(x,y,function(a,b) exp(-(a^2+b^2)/2));contour(x,y,z,col="#94a3b8");points(rnorm(30),rnorm(30),pch=19,col="#dc2626")
06
Example 06Base RIntermediate

Connect Contours to a 3D Surface

A perspective plot provides a complementary three-dimensional view of the same matrix.

Connect Contours to a 3D Surface example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x<-seq(-3,3,length=40);y<-x;z<-outer(x,y,function(a,b) exp(-(a^2+b^2)/2));persp(x,y,z,theta=35,phi=25,col="#93c5fd",shade=.4,xlab="X",ylab="Y",zlab="Z")