R Warning: stat_contour(): Zero contours were generated

I am working with the R programming language. I am trying to follow the tutorial on "contour maps" posted over here: 2D contours of a 3D surface — geom_contour • ggplot2

I generated some data for this example:

#generate data
a <- rnorm(100,10,10)
b <- rnorm(100,5,5)
c <- rnorm(100,1,1)
d - data.frame(a,b,c)

Then, I tried to make these plots:

v <- ggplot(d, aes(a, b, z = density))
v + geom_contour_filled()

But this produces the following warning messages:

Warning messages:
1: stat_contour(): Zero contours were generated 
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

And produces an empty plot instead of the desired plot:

The Desired Plot

Can someone please show me what I am doing wrong?


#avoid small margins error

#load library

#create plot

fld <- with(DF, interp(x = X1, y = X2, z = total))

filled.contour(x = fld$x,
               y = fld$y,
               z = fld$z,
               color.palette =
                   colorRampPalette(c("white", "blue")),
               xlab = "X1",
               ylab = "X2",
               main = "Contour Plot",
               key.title = title(main = "total ", cex.main = 1))

enter image description here

@swaheera, you need binned data. If you look at the "faithfuld" data use in the example, e.g., plot(faithfuld$eruptions, faithfuld$waiting), it's a regular grid that is being contoured.

If you generate data at regular intervals it will give you what you want.

#generate data
a <- seq(1,10,1)
b <- seq(1,10,1)
d <- expand.grid(a,b)
c <- rnorm(100,1,1)
d$c <- c

ggplot(data = d, aes(Var1, Var2, z = c)) + geom_contour_filled()
1 Like

@Ach: thank you so much for your answer!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.