Skip to content

Commit

Permalink
Improve docs for geom_contour as discussed in issue #4324 (#4325)
Browse files Browse the repository at this point in the history
* Improve docs for geom_contour as discussed in issue #4324

* Stop default testing of new example.

* Recommend interp instead of akima
  • Loading branch information
dmurdoch authored Jan 30, 2021
1 parent 6e987ed commit dbd7d79
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Suggests:
ggplot2movies,
hexbin,
Hmisc,
interp,
knitr,
lattice,
mapproj,
Expand Down
38 changes: 29 additions & 9 deletions R/geom-contour.r
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#' 2D contours of a 3D surface
#'
#' ggplot2 can not draw true 3D surfaces, but you can use `geom_contour()`,
#' `geom_contour_filled()`, and [geom_tile()] to visualise 3D surfaces in 2D.
#' To specify a valid surface, the data must contain `x`, `y`, and `z` coordinates,
#' and each unique combination of `x` and `y` can appear exactly once. Contouring
#' tends to work best when `x` and `y` form a (roughly) evenly
#' spaced grid. If your data is not evenly spaced, you may want to interpolate
#' to a grid before visualising, see [geom_density_2d()].
#' `geom_contour_filled()`, and [geom_tile()] to visualise 3D surfaces in 2D. To
#' specify a valid surface, the data must contain `x`, `y`, and `z` coordinates,
#' and each unique combination of `x` and `y` can appear at most once.
#' Contouring requires that the points can be rearranged so that the `z` values
#' form a matrix, with rows corresponding to unique `x` values, and columns
#' corresponding to unique `y` values. Missing entries are allowed, but contouring
#' will only be done on cells of the grid with all four `z` values present. If
#' your data is irregular, you can interpolate to a grid before visualising
#' using the [interp::interp()] function from the `interp` package
#' (or one of the interpolating functions from the `akima` package.)
#'
#' @eval rd_aesthetics("geom", "contour")
#' @eval rd_aesthetics("geom", "contour_filled")
Expand All @@ -15,9 +19,9 @@
#' @inheritParams geom_path
#' @param bins Number of contour bins. Overridden by `binwidth`.
#' @param binwidth The width of the contour bins. Overridden by `breaks`.
#' @param breaks Numeric vector to set the contour breaks.
#' Overrides `binwidth` and `bins`. By default, this is a vector of
#' length ten with [pretty()] breaks.
#' @param breaks Numeric vector to set the contour breaks. Overrides `binwidth`
#' and `bins`. By default, this is a vector of length ten with [pretty()]
#' breaks.
#' @seealso [geom_density_2d()]: 2d density contours
#' @export
#' @examples
Expand Down Expand Up @@ -47,6 +51,22 @@
#' v + geom_contour(colour = "red")
#' v + geom_raster(aes(fill = density)) +
#' geom_contour(colour = "white")
#'
#' # Irregular data
#' if (requireNamespace("interp")) {
#' # Use a dataset from the interp package
#' data(franke, package = "interp")
#' origdata <- as.data.frame(interp::franke.data(1, 1, franke))
#' grid <- with(origdata, interp::interp(x, y, z))
#' griddf <- subset(data.frame(x = rep(grid$x, nrow(grid$z)),
#' y = rep(grid$y, each = ncol(grid$z)),
#' z = as.numeric(grid$z)),
#' !is.na(z))
#' ggplot(griddf, aes(x, y, z = z)) +
#' geom_contour_filled() +
#' geom_point(data = origdata)
#' } else
#' message("Irregular data requires the 'interp' package")
#' }
geom_contour <- function(mapping = NULL, data = NULL,
stat = "contour", position = "identity",
Expand Down
38 changes: 29 additions & 9 deletions man/geom_contour.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/geom_density_2d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dbd7d79

Please sign in to comment.