[R] adding contour lines to a filled.contour

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Dec 15 11:20:03 CET 2002


Andy Bunn wrote:
> 
> 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?

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





> 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)
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list