Skip to content

Draw a circle on a filled.contour() plot

7 messages · Webmaster, Duncan Murdoch, Renaud Lancelot +2 more

#
Hi all,

I'm trying to use symbols() to draw a circle of a given radius at a  
given position onto a filled.contour() plot. The commands I issue are:
As can be seen on the resulting PDF file (http://www.eleves.ens.fr/ 
home/coudert/hist_2d.pdf), the circle appears to have the right  
radius but its center is not the correct one [0.62, 0]. What am I  
missing here?

Thanks for your help,
FX

PS: complete input (5 lines), data file for the contour plot and PDF  
result on my system can be found at:
Data file: http://www.eleves.ens.fr/home/coudert/hist_2d
R script: http://www.eleves.ens.fr/home/coudert/hist_2d.R
PDF result: http://www.eleves.ens.fr/home/coudert/hist_2d.pdf
#
On 12/17/2006 7:06 AM, Webmaster wrote:
There's a note on the man page about this:

The output produced by 'filled.contour' is actually a combination
      of two plots; one is the filled contour and one is the legend.
      Two separate coordinate systems are set up for these two plots,
      but they are only used internally - once the function has returned
      these coordinate systems are lost.  If you want to annotate the
      main contour plot, for example to add points, you can specify
      graphics commands in the 'plot.axes' argument.  An example is
      given below.

Duncan Murdoch
#
Thanks!

I now have another question (hopefully the last) for which I couldn't  
find an answer in the mailing-list archives: how can I get rid of the  
color key on the contour.filled() plot? I that question, unanswered,  
in a post one year ago on this same list, so I hope it's by any mean  
possible... (I think it's possible to change the code completely and  
use image(), but it's much less pretty).

FX
#
On 12/17/2006 9:31 AM, Webmaster wrote:
I don't see any option in there, so you might have to write your own 
version to do it.  The source for the regular function is in

http://svn.r-project.org/R/trunk/src/library/graphics/R/filled.contour.R

and the part that plots the key is

     ## Plot the 'plot key' (scale):
     mar <- mar.orig
     mar[4] <- mar[2]
     mar[2] <- 1
     par(mar = mar)
     plot.new()
     plot.window(xlim=c(0,1), ylim=range(levels), xaxs="i", yaxs="i")
     rect(0, levels[-length(levels)], 1, levels[-1], col = col)
     if (missing(key.axes)) {
         if (axes)
             axis(4)
     }
     else key.axes
     box()
     if (!missing(key.title))
	key.title

You'd probably also want to change the layout, because you won't want 
the blank space there.  Or possibly you could get the results you want 
from contour(), which doesn't plot the scale.

Duncan Murdoch
#
Try levelplot in package lattice instead of filled.contour: you will
be able to annotate the plot using the grid package. Moreover, you can
remove the key using the colorkey argument.

library(lattice)
library(grid)
z <- as.matrix(read.table("hist_2d"))
dimnames(z) <- NULL
Data <- expand.grid(x = seq(-4.45, 4.45, len = 90),
                    y = seq(-4.45, 4.45, len = 90))
Data$z <- c(z)
rm(z)
Cols <- colorRampPalette(c("white", "blue","yellow","red"))
levelplot(z ~ x * y, data = Data,
  aspect = 1, colorkey = FALSE, col.regions = Cols(6),
    panel = function(...){
      panel.levelplot(...)
      grid.circle(x = 0.62, y = 0, r = 2.5, default.units = "native")
      })

Best,

Renaud

2006/12/17, Webmaster <webmaster at citedesjeunes.com>:

  
    
#
Use my function filled.next:

filled.next <- function(fun) {
         mar.orig <- mar <- par("mar")
         w <- (3 + mar[2]) * par("csi") * 2.54
         layout(matrix(c(2, 1), nc = 2), widths = c(1, lcm(w)))
         mar[4] <- 1
         par(mar = mar)
         par(mfg=c(1,2))
         par(new=TRUE)
         fun
         par(mar = mar.orig)
}

# then

filled.contour(y,x,z,levels=seq(0.02,1.0,len=50),
     color.palette=colorRampPalette(c("blue","yellow","red")),
     title=title(main="",xlab="",ylab=""))
filled.next(symbols(0.62,0.0,circles=c(2.5),add=TRUE,inches=FALSE))



With plot function in filled.next, don't forget to add xaxs='i', yaxs='i'...

Jean Coursol

----------------------------------------------

Quoting Renaud Lancelot <renaud.lancelot at gmail.com>:
#
Hi
On 17 Dec 2006 at 10:04, Duncan Murdoch wrote:
Date sent:      	Sun, 17 Dec 2006 10:04:37 -0500
From:           	Duncan Murdoch <murdoch at stats.uwo.ca>
To:             	Webmaster <webmaster at citedesjeunes.com>
Copies to:      	r-help at stat.math.ethz.ch
Subject:        	Re: [R] Draw a circle on a filled.contour() plot
You possibly can use

image(...)
contour(..., add=T)

to get contours overlayed e.g.

x <- y <- seq(-4*pi, 4*pi, len=27)
r <- sqrt(outer(x^2, y^2, "+"))
image(z = z <- cos(r^2)*exp(-r/6), col=gray((0:32)/32))
image(z, axes = FALSE, main = "Math can be beautiful ...",
      xlab = expression(cos(r^2) * e^{-r/6}))
contour(z, add = TRUE, drawlabels = FALSE)

HTH
Petr
Petr Pikal
petr.pikal at precheza.cz