geom_contour breaks (greater than a value instead of a fixed value)

35 views Asked by At

I am trying to plot contour lines greater than a value. In the example below, if I use geom_contour() I get all the contours, if I use breaks = 0.00384 then I get just for that break, is there a way to say all breaks greater than 0.00384? I tried breaks > 0.00384 but I get an error Error in geom_contour(): ! mapping must be created by aes() Run rlang::last_trace() to see where the error occurred.

library(ggplot2)

(v <- ggplot(faithfuld, aes(waiting, eruptions, z = density)))
v + geom_contour()
v + geom_contour(breaks=0.00384)
1

There are 1 answers

0
stefan On

The breaks= argument of geom/stat_contour can be a numeric vector of breaks or a function which takes the range of the data and the bin width as arguments and returns a vector of breaks, e.g. by default the breaks are computed as pretty(z_range, 10).

Hence, you could pass any function to the breaks= argument, e.g. the code below uses a function which gives 4 breaks > 0.000384 using seq() and dropping the first aka the break 0.000384:

Note: When passing a function to the breaks= argument it's important to set binwidth= or bins= to a non NULL value, which looks like a bug to me.

library(ggplot2)

v <- ggplot(faithfuld, aes(waiting, eruptions, z = density))

v +
  geom_contour(
    color = "red"
  ) +
  geom_contour(
    binwidth = 1, 
    breaks = \(z_range, binwidth) {
      seq(.000384, z_range[2], length.out = 5)[-1]
    }
  )