Skip to content

Commit

Permalink
Use the isoband package to calculate contour lines and bands (#3439, f…
Browse files Browse the repository at this point in the history
…ixes #3044)

* use isoband instead of contourLines in stat_contour()

* separate contouring functions into testable functions

* implement filled_ variants of stat_ and geom_ contour

* add tests and examples for the _contour_ functions

* adopt rasterization method from GeomRaster (rather than use tapply()) to create z_matrix
  • Loading branch information
paleolimbot authored Jul 16, 2019
1 parent 125cc5f commit b771c50
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 237 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Suggests:
ggplot2movies,
hexbin,
Hmisc,
isoband,
knitr,
lattice,
mapproj,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export(StatBindot)
export(StatBinhex)
export(StatBoxplot)
export(StatContour)
export(StatContourFilled)
export(StatCount)
export(StatDensity)
export(StatDensity2d)
Expand Down Expand Up @@ -306,6 +307,7 @@ export(geom_blank)
export(geom_boxplot)
export(geom_col)
export(geom_contour)
export(geom_contour_filled)
export(geom_count)
export(geom_crossbar)
export(geom_curve)
Expand Down Expand Up @@ -520,6 +522,7 @@ export(stat_bin_hex)
export(stat_binhex)
export(stat_boxplot)
export(stat_contour)
export(stat_contour_filled)
export(stat_count)
export(stat_density)
export(stat_density2d)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ggplot2 (development version)

* Added `stat_contour_filled()` and `geom_contour_filled()`, which compute
and draw filled contours of gridded data (@paleolimbot, #3044).

* `geom_contour()` and `stat_contour()` now use the isoband package
to compute contour lines. The `complete` parameter (which was undocumented
and has been unused for at least four years) was removed (@paleolimbot, #3044).

* `stat_smooth()` user `REML` by default, if `method = "gam"` and
`gam`'s method is not specified (@ikosmidis, #2630).

Expand Down
50 changes: 48 additions & 2 deletions R/geom-contour.r
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#' @inheritParams layer
#' @inheritParams geom_point
#' @inheritParams geom_path
#' @param bins,binwidth Calculate `breaks` using evenly-spaced values. Use `NULL`
#' to use the default [pretty()] breaks.
#' @param breaks A vector of breaks (overrides `bins` and `bindwidth`).
#' @seealso [geom_density_2d()]: 2d density contours
#' @export
#' @export
Expand All @@ -25,6 +28,9 @@
#' geom_density_2d()
#'
#' \donttest{
#' # use geom_contour_filled() for filled contours
#' v + geom_contour_filled()
#'
#' # Setting bins creates evenly spaced contours in the range of the data
#' v + geom_contour(bins = 2)
#' v + geom_contour(bins = 10)
Expand All @@ -46,6 +52,9 @@ geom_contour <- function(mapping = NULL, data = NULL,
lineend = "butt",
linejoin = "round",
linemitre = 10,
bins = NULL,
binwidth = NULL,
breaks = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
Expand All @@ -61,6 +70,38 @@ geom_contour <- function(mapping = NULL, data = NULL,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre,
bins = bins,
binwidth = binwidth,
breaks = breaks,
na.rm = na.rm,
...
)
)
}

#' @rdname geom_contour
#' @export
geom_contour_filled <- function(mapping = NULL, data = NULL,
stat = "contour_filled", position = "identity",
...,
bins = NULL,
binwidth = NULL,
breaks = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomPolygon,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
bins = bins,
binwidth = binwidth,
breaks = breaks,
na.rm = na.rm,
...
)
Expand All @@ -73,6 +114,11 @@ geom_contour <- function(mapping = NULL, data = NULL,
#' @export
#' @include geom-path.r
GeomContour <- ggproto("GeomContour", GeomPath,
default_aes = aes(weight = 1, colour = "#3366FF", size = 0.5, linetype = 1,
alpha = NA)
default_aes = aes(
weight = 1,
colour = "#3366FF",
size = 0.5,
linetype = 1,
alpha = NA
)
)
Loading

0 comments on commit b771c50

Please sign in to comment.