Skip to content

Commit b771c50

Browse files
authored
Use the isoband package to calculate contour lines and bands (#3439, fixes #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
1 parent 125cc5f commit b771c50

13 files changed

+412
-237
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Suggests:
3939
ggplot2movies,
4040
hexbin,
4141
Hmisc,
42+
isoband,
4243
knitr,
4344
lattice,
4445
mapproj,

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export(StatBindot)
205205
export(StatBinhex)
206206
export(StatBoxplot)
207207
export(StatContour)
208+
export(StatContourFilled)
208209
export(StatCount)
209210
export(StatDensity)
210211
export(StatDensity2d)
@@ -306,6 +307,7 @@ export(geom_blank)
306307
export(geom_boxplot)
307308
export(geom_col)
308309
export(geom_contour)
310+
export(geom_contour_filled)
309311
export(geom_count)
310312
export(geom_crossbar)
311313
export(geom_curve)
@@ -520,6 +522,7 @@ export(stat_bin_hex)
520522
export(stat_binhex)
521523
export(stat_boxplot)
522524
export(stat_contour)
525+
export(stat_contour_filled)
523526
export(stat_count)
524527
export(stat_density)
525528
export(stat_density2d)

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# ggplot2 (development version)
22

3+
* Added `stat_contour_filled()` and `geom_contour_filled()`, which compute
4+
and draw filled contours of gridded data (@paleolimbot, #3044).
5+
6+
* `geom_contour()` and `stat_contour()` now use the isoband package
7+
to compute contour lines. The `complete` parameter (which was undocumented
8+
and has been unused for at least four years) was removed (@paleolimbot, #3044).
9+
310
* `stat_smooth()` user `REML` by default, if `method = "gam"` and
411
`gam`'s method is not specified (@ikosmidis, #2630).
512

R/geom-contour.r

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#' @inheritParams layer
1313
#' @inheritParams geom_point
1414
#' @inheritParams geom_path
15+
#' @param bins,binwidth Calculate `breaks` using evenly-spaced values. Use `NULL`
16+
#' to use the default [pretty()] breaks.
17+
#' @param breaks A vector of breaks (overrides `bins` and `bindwidth`).
1518
#' @seealso [geom_density_2d()]: 2d density contours
1619
#' @export
1720
#' @export
@@ -25,6 +28,9 @@
2528
#' geom_density_2d()
2629
#'
2730
#' \donttest{
31+
#' # use geom_contour_filled() for filled contours
32+
#' v + geom_contour_filled()
33+
#'
2834
#' # Setting bins creates evenly spaced contours in the range of the data
2935
#' v + geom_contour(bins = 2)
3036
#' v + geom_contour(bins = 10)
@@ -46,6 +52,9 @@ geom_contour <- function(mapping = NULL, data = NULL,
4652
lineend = "butt",
4753
linejoin = "round",
4854
linemitre = 10,
55+
bins = NULL,
56+
binwidth = NULL,
57+
breaks = NULL,
4958
na.rm = FALSE,
5059
show.legend = NA,
5160
inherit.aes = TRUE) {
@@ -61,6 +70,38 @@ geom_contour <- function(mapping = NULL, data = NULL,
6170
lineend = lineend,
6271
linejoin = linejoin,
6372
linemitre = linemitre,
73+
bins = bins,
74+
binwidth = binwidth,
75+
breaks = breaks,
76+
na.rm = na.rm,
77+
...
78+
)
79+
)
80+
}
81+
82+
#' @rdname geom_contour
83+
#' @export
84+
geom_contour_filled <- function(mapping = NULL, data = NULL,
85+
stat = "contour_filled", position = "identity",
86+
...,
87+
bins = NULL,
88+
binwidth = NULL,
89+
breaks = NULL,
90+
na.rm = FALSE,
91+
show.legend = NA,
92+
inherit.aes = TRUE) {
93+
layer(
94+
data = data,
95+
mapping = mapping,
96+
stat = stat,
97+
geom = GeomPolygon,
98+
position = position,
99+
show.legend = show.legend,
100+
inherit.aes = inherit.aes,
101+
params = list(
102+
bins = bins,
103+
binwidth = binwidth,
104+
breaks = breaks,
64105
na.rm = na.rm,
65106
...
66107
)
@@ -73,6 +114,11 @@ geom_contour <- function(mapping = NULL, data = NULL,
73114
#' @export
74115
#' @include geom-path.r
75116
GeomContour <- ggproto("GeomContour", GeomPath,
76-
default_aes = aes(weight = 1, colour = "#3366FF", size = 0.5, linetype = 1,
77-
alpha = NA)
117+
default_aes = aes(
118+
weight = 1,
119+
colour = "#3366FF",
120+
size = 0.5,
121+
linetype = 1,
122+
alpha = NA
123+
)
78124
)

0 commit comments

Comments
 (0)