Skip to content

Commit dbd7d79

Browse files
authored
Improve docs for geom_contour as discussed in issue #4324 (#4325)
* Improve docs for geom_contour as discussed in issue #4324 * Stop default testing of new example. * Recommend interp instead of akima
1 parent 6e987ed commit dbd7d79

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Suggests:
4646
ggplot2movies,
4747
hexbin,
4848
Hmisc,
49+
interp,
4950
knitr,
5051
lattice,
5152
mapproj,

R/geom-contour.r

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#' 2D contours of a 3D surface
22
#'
33
#' ggplot2 can not draw true 3D surfaces, but you can use `geom_contour()`,
4-
#' `geom_contour_filled()`, and [geom_tile()] to visualise 3D surfaces in 2D.
5-
#' To specify a valid surface, the data must contain `x`, `y`, and `z` coordinates,
6-
#' and each unique combination of `x` and `y` can appear exactly once. Contouring
7-
#' tends to work best when `x` and `y` form a (roughly) evenly
8-
#' spaced grid. If your data is not evenly spaced, you may want to interpolate
9-
#' to a grid before visualising, see [geom_density_2d()].
4+
#' `geom_contour_filled()`, and [geom_tile()] to visualise 3D surfaces in 2D. To
5+
#' specify a valid surface, the data must contain `x`, `y`, and `z` coordinates,
6+
#' and each unique combination of `x` and `y` can appear at most once.
7+
#' Contouring requires that the points can be rearranged so that the `z` values
8+
#' form a matrix, with rows corresponding to unique `x` values, and columns
9+
#' corresponding to unique `y` values. Missing entries are allowed, but contouring
10+
#' will only be done on cells of the grid with all four `z` values present. If
11+
#' your data is irregular, you can interpolate to a grid before visualising
12+
#' using the [interp::interp()] function from the `interp` package
13+
#' (or one of the interpolating functions from the `akima` package.)
1014
#'
1115
#' @eval rd_aesthetics("geom", "contour")
1216
#' @eval rd_aesthetics("geom", "contour_filled")
@@ -15,9 +19,9 @@
1519
#' @inheritParams geom_path
1620
#' @param bins Number of contour bins. Overridden by `binwidth`.
1721
#' @param binwidth The width of the contour bins. Overridden by `breaks`.
18-
#' @param breaks Numeric vector to set the contour breaks.
19-
#' Overrides `binwidth` and `bins`. By default, this is a vector of
20-
#' length ten with [pretty()] breaks.
22+
#' @param breaks Numeric vector to set the contour breaks. Overrides `binwidth`
23+
#' and `bins`. By default, this is a vector of length ten with [pretty()]
24+
#' breaks.
2125
#' @seealso [geom_density_2d()]: 2d density contours
2226
#' @export
2327
#' @examples
@@ -47,6 +51,22 @@
4751
#' v + geom_contour(colour = "red")
4852
#' v + geom_raster(aes(fill = density)) +
4953
#' geom_contour(colour = "white")
54+
#'
55+
#' # Irregular data
56+
#' if (requireNamespace("interp")) {
57+
#' # Use a dataset from the interp package
58+
#' data(franke, package = "interp")
59+
#' origdata <- as.data.frame(interp::franke.data(1, 1, franke))
60+
#' grid <- with(origdata, interp::interp(x, y, z))
61+
#' griddf <- subset(data.frame(x = rep(grid$x, nrow(grid$z)),
62+
#' y = rep(grid$y, each = ncol(grid$z)),
63+
#' z = as.numeric(grid$z)),
64+
#' !is.na(z))
65+
#' ggplot(griddf, aes(x, y, z = z)) +
66+
#' geom_contour_filled() +
67+
#' geom_point(data = origdata)
68+
#' } else
69+
#' message("Irregular data requires the 'interp' package")
5070
#' }
5171
geom_contour <- function(mapping = NULL, data = NULL,
5272
stat = "contour", position = "identity",

man/geom_contour.Rd

Lines changed: 29 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_density_2d.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)