Skip to content

adding contour lines to a filled.contour

3 messages · Andy Bunn, Uwe Ligges, Roger D. Peng

#
Hi all, 

Does anybody know how to add contour lines to a filled contour plot?

I want to draw a single contour around values that are above a certain
level (e.g., significant). The problem I'm having is that since the
filled.contour command actually draws two plots (data and the key),
adding contour lines paints them over both plots.

Any suggestions?

Thanks, Andy


#~~~~~~~~~~~~~~~~~~~
#example

junk.mat <- matrix(rnorm(1600), 16, 100)

filled.contour(junk.mat, 
               color = terrain.colors)

#set values < 2 to zero
contour.mat <- ifelse(junk.mat < 2, 0, junk.mat)

#add contours
contour(contour.mat,
        levels = 1,
        drawlabels = F,
        axes = F, 
        frame.plot = F,
        add = T)
#
Andy Bunn wrote:
Yes. Look into the code of filled.contour(), how things are set up for
those two parts of the plot. The following solution will become clear:

 # your code:
 junk.mat <- matrix(rnorm(1600), 16, 100)
 filled.contour(junk.mat, color = terrain.colors)
 contour.mat <- ifelse(junk.mat < 2, 0, junk.mat)

 # insert 3 lines of code, stolen from filled.contour():
 mar.orig <- par("mar")
 w <- (3 + mar.orig[2]) * par("csi") * 2.54
 layout(matrix(c(2, 1), nc = 2), widths = c(1, lcm(w)))

 # your code:
 contour(contour.mat, levels = 1, drawlabels = FALSE, 
     axes = FALSE, frame.plot = FFALSE, add = TRUE)
 

Uwe Ligges
#
There is an easier solution.  Try,

junk.mat <- matrix(rnorm(1600), 16, 100)
contour.mat <- ifelse(junk.mat < 2, 0, junk.mat)
filled.contour(junk.mat, color = terrain.colors, 
               plot.axes = contour(contour.mat, levels = 1, 
                                   drawlabels = FALSE, axes = FALSE, 
                                   frame.plot = FFALSE, add = TRUE))

The 'plot.axes' argument to filled.contour() gives you access to the
coordinate system in the actual plotting area.  However, you will notice
that the axes are missing.  You need to add them explicitly, as in:

filled.contour(junk.mat, color = terrain.colors, 
               plot.axes = { contour(contour.mat, levels = 1, 
                                     drawlabels = FALSE, axes = FALSE, 
                                     frame.plot = FFALSE, add = TRUE);
			     axis(1); axis(2) } )

This also useful for adding titles, text annotations, points, etc. 

-roger
_______________________________
UCLA Department of Statistics
rpeng at stat.ucla.edu
http://www.stat.ucla.edu/~rpeng
On Sun, 15 Dec 2002, Uwe Ligges wrote: