|
1 | 1 | #' 2D contours of a 3D surface
|
2 | 2 | #'
|
| 3 | +#' @description |
3 | 4 | #' 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. 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.) |
| 5 | +#' `geom_contour_filled()`, and [geom_tile()] to visualise 3D surfaces in 2D. |
| 6 | +#' |
| 7 | +#' These functions require regular data, where the `x` and `y` coordinates |
| 8 | +#' form an equally spaced grid, and each combination of `x` and `y` appears |
| 9 | +#' once. Missing values of `z` are allowed, but contouring will only work for |
| 10 | +#' grid points where all four corners are non-missing. If you have irregular |
| 11 | +#' data, you'll need to first interpolate on to a grid before visualising, |
| 12 | +#' using [interp::interp()], [akima::bilinear()], or similar. |
14 | 13 | #'
|
15 | 14 | #' @eval rd_aesthetics("geom", "contour")
|
16 | 15 | #' @eval rd_aesthetics("geom", "contour_filled")
|
|
56 | 55 | #' v + geom_contour(colour = "red")
|
57 | 56 | #' v + geom_raster(aes(fill = density)) +
|
58 | 57 | #' geom_contour(colour = "white")
|
59 |
| -#' |
60 |
| -#' # Irregular data |
61 |
| -#' if (requireNamespace("interp")) { |
62 |
| -#' # Use a dataset from the interp package |
63 |
| -#' data(franke, package = "interp") |
64 |
| -#' origdata <- as.data.frame(interp::franke.data(1, 1, franke)) |
65 |
| -#' grid <- with(origdata, interp::interp(x, y, z)) |
66 |
| -#' griddf <- subset(data.frame(x = rep(grid$x, nrow(grid$z)), |
67 |
| -#' y = rep(grid$y, each = ncol(grid$z)), |
68 |
| -#' z = as.numeric(grid$z)), |
69 |
| -#' !is.na(z)) |
70 |
| -#' ggplot(griddf, aes(x, y, z = z)) + |
71 |
| -#' geom_contour_filled() + |
72 |
| -#' geom_point(data = origdata) |
73 |
| -#' } else |
74 |
| -#' message("Irregular data requires the 'interp' package") |
75 | 58 | #' }
|
76 | 59 | geom_contour <- function(mapping = NULL, data = NULL,
|
77 | 60 | stat = "contour", position = "identity",
|
|
0 commit comments