Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a box within filled.contour plots in R?

Tags:

r

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!

like image 979
Albert Lyu Avatar asked Aug 20 '10 08:08

Albert Lyu


People also ask

What is a filled contour plot?

Description. example. contourf( Z ) creates a filled contour plot containing the isolines of matrix Z , where Z contains height values on the x-y plane. MATLAB® automatically selects the contour lines to display. The column and row indices of Z are the x and y coordinates in the plane, respectively.

How do you plot in 3D contour?

To plot 3D contour we will use countour3() to plot different types of 3D modules. Syntax: contour3(X,Y,Z): Specifies the x and y coordinates for the values in Z. contour3(Z): Creates a 3-D contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane.


1 Answers

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

like image 125
nico Avatar answered Sep 30 '22 18:09

nico