Thorn Thaler
2009-Aug-19 08:24 UTC
[Rd] Lattice: Drawing a single contour line with a negative value fails
Hi everybody, I want to add a single contourline to a levelplot. While everything works fine if the value at which the line should be drawn is positive, there is an error if the value is negative: library(lattice) my.panel <- function(..., at, contour=FALSE, labels=NULL) { panel.levelplot(..., at=at, contour=contour, labels=labels) panel.contourplot(..., contour=TRUE, labels="Contour", lwd=2, at=con) } x <- y <- 1:100 df <- expand.grid(x=x, y=y) df$z <- apply(df, 1, function(x) (x[1]^2+x[2]^2)*ifelse(x[1]^2+x[2]^2 < 5000, -1,1)) col.regions <- heat.colors(50) # Works con <- 1000 levelplot(df$z~df$x+df$y, col.regions=col.regions, panel=my.panel) # Does not work con <- -1000 levelplot(df$z~df$x+df$y, col.regions=col.regions, panel=my.panel) I've tracked down the error to the function "cut.default", which takes an argument "breaks", which should be "either a numeric vector of two or more cut points or a single number (greater than or equal to 2) giving the number of intervals into which 'x' is to be cut." So it seems to me that a single contourline at a positive value works because the value is interpreted as the number of intervals, even though there is just this single line in the resulting plot. What would be the correct way to add just a single contour line to a levelplot, or is it indeed a bug? Thanks, Thorn
Deepayan Sarkar
2009-Aug-22 08:06 UTC
[Rd] Lattice: Drawing a single contour line with a negative value fails
On 8/19/09, Thorn Thaler <thothal at sbox.tugraz.at> wrote:> Hi everybody, > > I want to add a single contourline to a levelplot. While everything works > fine if the value at which the line should be drawn is positive, there is an > error if the value is negative: > > library(lattice) > my.panel <- function(..., at, contour=FALSE, labels=NULL) { > panel.levelplot(..., at=at, contour=contour, labels=labels) > panel.contourplot(..., contour=TRUE, labels="Contour", lwd=2, > at=con) > } > > x <- y <- 1:100 > df <- expand.grid(x=x, y=y) > df$z <- apply(df, 1, function(x) > (x[1]^2+x[2]^2)*ifelse(x[1]^2+x[2]^2 < 5000, -1,1)) > col.regions <- heat.colors(50) > > # Works > con <- 1000 > levelplot(df$z~df$x+df$y, col.regions=col.regions, panel=my.panel) > > # Does not work > con <- -1000 > levelplot(df$z~df$x+df$y, col.regions=col.regions, panel=my.panel) > > I've tracked down the error to the function "cut.default", which takes an > argument "breaks", which should be "either a numeric vector of two or more > cut points or a single number (greater than or equal to 2) giving the number > of intervals into which 'x' is to be cut." > > So it seems to me that a single contourline at a positive value works > because the value is interpreted as the number of intervals, even though > there is just this single line in the resulting plot. > > What would be the correct way to add just a single contour line to a > levelplot, or is it indeed a bug?Definitely a bug. It should be easy to fix (but I haven't had time to get to it yet). Using something like con <- c(-1000, NA) seems to work though. -Deepayan