Skip to content

Commit 9806e27

Browse files
authored
Examples of plotting automation (#5745)
* write docpage for plotting automation * crosslink docs * reoxygenate * add pkgdown entry
1 parent 91a1dcf commit 9806e27

File tree

8 files changed

+243
-4
lines changed

8 files changed

+243
-4
lines changed

R/autolayer.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @param ... other arguments passed to specific methods
99
#' @return a ggplot layer
1010
#' @export
11-
#' @seealso [autoplot()], [ggplot()] and [fortify()]
11+
#' @family plotting automation topics
1212
autolayer <- function(object, ...) {
1313
UseMethod("autolayer")
1414
}

R/autoplot.R

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,115 @@
1+
#' Tailoring plots to particular data types
2+
#'
3+
#' @description
4+
#' There are three functions to make plotting particular data types easier:
5+
#' `autoplot()`, `autolayer()` and `fortify()`. These are S3 generics for which
6+
#' other packages can write methods to display classes of data. The three
7+
#' functions are complementary and allow different levels of customisation.
8+
#' Below we'll explore implementing this series of methods to automate plotting
9+
#' of some class.
10+
#'
11+
#' Let's suppose we are writing a packages that has a class called 'my_heatmap',
12+
#' that wraps a matrix and we'd like users to easily plot this heatmap.
13+
#'
14+
#' ```r
15+
#' my_heatmap <- function(...) {
16+
#' m <- matrix(...)
17+
#' class(m) <- c("my_heatmap", class(m))
18+
#' m
19+
#' }
20+
#'
21+
#' my_data <- my_heatmap(volcano)
22+
#' ```
23+
#'
24+
#' # Automatic data shaping
25+
#'
26+
#' One of the things we have to do is ensure that the data is shaped in the long
27+
#' format so that it is compatible with ggplot2. This is the job of the
28+
#' `fortify()` function. Because 'my_heatmap' wraps a matrix, we can let the
29+
#' fortify method 'melt' the matrix to a long format. If your data is already
30+
#' based on a long-format `<data.frame>`, you can skip implementing a
31+
#' `fortify()` method.
32+
#'
33+
#' ```r
34+
#' fortify.my_heatmap <- function(model, ...) {
35+
#' data.frame(
36+
#' row = as.vector(row(model)),
37+
#' col = as.vector(col(model)),
38+
#' value = as.vector(model)
39+
#' )
40+
#' }
41+
#'
42+
#' fortify(my_data)
43+
#' ```
44+
#'
45+
#' When you have implemented the `fortify()` method, it should be easier to
46+
#' construct a plot with the data than with the matrix.
47+
#'
48+
#' ```r
49+
#' ggplot(my_data, aes(x = col, y = row, fill = value)) +
50+
#' geom_raster()
51+
#' ```
52+
#'
53+
#' # Automatic layers
54+
#'
55+
#' A next step in automating plotting of your data type is to write an
56+
#' `autolayer()` method. These are typically wrappers around geoms or stats
57+
#' that automatically set aesthetics or other parameters. If you haven't
58+
#' implemented a `fortify()` method for your data type, you might have to
59+
#' reshape the data in `autolayer()`.
60+
#'
61+
#' If you require multiple layers to display your data type, you can use an
62+
#' `autolayer()` method that constructs a list of layers, which can be added
63+
#' to a plot.
64+
#'
65+
#' ```r
66+
#' autolayer.my_heatmap <- function(object, ...) {
67+
#' geom_raster(
68+
#' mapping = aes(x = col, y = row, fill = value),
69+
#' data = object,
70+
#' ...,
71+
#' inherit.aes = FALSE
72+
#' )
73+
#' }
74+
#'
75+
#' ggplot() + autolayer(my_data)
76+
#' ```
77+
#'
78+
#' As a quick tip: if you define a mapping in `autolayer()`, you might want
79+
#' to set `inherit.aes = FALSE` to not have aesthetics set in other layers
80+
#' interfere with your layer.
81+
#'
82+
#' # Automatic plots
83+
#'
84+
#' The last step in automating plotting is to write an `autoplot()` method
85+
#' for your data type. The expectation is that these return a complete plot.
86+
#' In the example below, we're exploiting the `autolayer()` method that we
87+
#' have already written to make a complete plot.
88+
#'
89+
#' ```r
90+
#' autoplot.my_heatmap <- function(object, ..., option = "magma") {
91+
#' ggplot() +
92+
#' autolayer(my_data) +
93+
#' scale_fill_viridis_c(option = option) +
94+
#' theme_void()
95+
#' }
96+
#'
97+
#' autoplot(my_data)
98+
#' ```
99+
#'
100+
#' If you don't have a wish to implement a base R plotting method, you
101+
#' can set the plot method for your class to the autoplot method.
102+
#'
103+
#' ```r
104+
#' plot.my_heatmap <- autoplot.my_heatmap
105+
#' plot(my_data)
106+
#' ```
107+
#'
108+
#' @family plotting automation topics
109+
#'
110+
#' @name automatic_plotting
111+
NULL
112+
1113
#' Create a complete ggplot appropriate to a particular data type
2114
#'
3115
#' `autoplot()` uses ggplot2 to draw a particular plot for an object of a
@@ -8,7 +120,7 @@
8120
#' @param ... other arguments passed to specific methods
9121
#' @return a ggplot object
10122
#' @export
11-
#' @seealso [autolayer()], [ggplot()] and [fortify()]
123+
#' @family plotting automation topics
12124
autoplot <- function(object, ...) {
13125
UseMethod("autoplot")
14126
}

R/fortify.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#' package, which implements a much wider range of methods. `fortify()`
55
#' may be deprecated in the future.
66
#'
7+
#' @family plotting automation topics
78
#' @seealso [fortify.lm()]
89
#' @param model model or other R object to convert to data frame
910
#' @param data original dataset, if needed

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ reference:
244244
- autolayer
245245
- fortify
246246
- map_data
247+
- automatic_plotting
247248

248249

249250
articles:

man/autolayer.Rd

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

man/automatic_plotting.Rd

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

man/autoplot.Rd

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

man/fortify.Rd

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

0 commit comments

Comments
 (0)