|
1 | 1 | #' 2D contours of a 3D surface
|
2 | 2 | #'
|
3 | 3 | #' 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.) |
10 | 14 | #'
|
11 | 15 | #' @eval rd_aesthetics("geom", "contour")
|
12 | 16 | #' @eval rd_aesthetics("geom", "contour_filled")
|
|
15 | 19 | #' @inheritParams geom_path
|
16 | 20 | #' @param bins Number of contour bins. Overridden by `binwidth`.
|
17 | 21 | #' @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. |
21 | 25 | #' @seealso [geom_density_2d()]: 2d density contours
|
22 | 26 | #' @export
|
23 | 27 | #' @examples
|
|
47 | 51 | #' v + geom_contour(colour = "red")
|
48 | 52 | #' v + geom_raster(aes(fill = density)) +
|
49 | 53 | #' 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") |
50 | 70 | #' }
|
51 | 71 | geom_contour <- function(mapping = NULL, data = NULL,
|
52 | 72 | stat = "contour", position = "identity",
|
|
0 commit comments