tags:

views:

56

answers:

1

Hi,

I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. Here's what my code looks like:

dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))

Does anyone know how I can plot those lines within the filled.contour function? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right.

Thanks!

+2  A: 

The manual page for filled.contour explains the problem (and gives a solution)

This function currently uses the ‘layout’ function and so is restricted to a full page display. As an alternative consider the ‘levelplot’ and ‘contourplot’ functions from the ‘lattice’ package which work in multipanel displays.

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.

So essentially you pass some instructions as the plot.axes parameters to override standard behaviour.

In your example:

filled.contour(x = seq(from=-1.5,to=1.5,length=20),
      y = seq(from=1,to=3.75,length=25), z = mypredict,
      col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
      plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})

Note that you have to recreate the two axes otherwise they will not be drawn. Also, no need to use the lines statement, when there is a rect function! :)

Hope this helps

nico
Thanks! That's a huge help! The rect function is also a good tip!
Think Blue Crew