|
8 | 8 | #' `ggplot()` is used to construct the initial plot object,
|
9 | 9 | #' and is almost always followed by a plus sign (`+`) to add
|
10 | 10 | #' components to the plot.
|
| 11 | +#' |
11 | 12 | #' There are three common patterns used to invoke `ggplot()`:
|
12 | 13 | #'
|
13 |
| -#' * `ggplot(df, aes(x, y, other aesthetics))` |
14 |
| -#' * `ggplot(df)` |
| 14 | +#' * `ggplot(data = df, mapping = aes(x, y, other aesthetics))` |
| 15 | +#' * `ggplot(data = df)` |
15 | 16 | #' * `ggplot()`
|
16 | 17 | #'
|
17 | 18 | #' The first pattern is recommended if all layers use the same
|
|
22 | 23 | #' The second pattern specifies the default data frame to use
|
23 | 24 | #' for the plot, but no aesthetics are defined up front. This
|
24 | 25 | #' is useful when one data frame is used predominantly for the
|
25 |
| -#' plot, but the aesthetics may vary from one layer to another. |
| 26 | +#' plot, but the aesthetics vary from one layer to another. |
26 | 27 | #'
|
27 | 28 | #' The third pattern initializes a skeleton `ggplot` object, which
|
28 | 29 | #' is fleshed out as layers are added. This is useful when
|
29 | 30 | #' multiple data frames are used to produce different layers, as
|
30 | 31 | #' is often the case in complex graphics.
|
31 | 32 | #'
|
| 33 | +#' The `data =` and `mapping =` specifications in the arguments are optional |
| 34 | +#' (and are often omitted in practice), so long as the data and the mapping |
| 35 | +#' values are passed into the function in the right order. In the examples |
| 36 | +#' below, however, they are left in place for clarity. |
| 37 | +#' |
32 | 38 | #' @param data Default dataset to use for plot. If not already a data.frame,
|
33 | 39 | #' will be converted to one by [fortify()]. If not specified,
|
34 | 40 | #' must be supplied in each layer added to the plot.
|
|
40 | 46 | #' @export
|
41 | 47 | #' @examples
|
42 | 48 | #' # Create a data frame with some sample data, then create a data frame
|
43 |
| -#' # containing the mean for each group in the sample data. |
| 49 | +#' # containing the mean value for each group in the sample data. |
44 | 50 | #' set.seed(1)
|
| 51 | +#' |
45 | 52 | #' sample_df <- data.frame(
|
46 | 53 | #' group = factor(rep(letters[1:3], each = 10)),
|
47 | 54 | #' value = rnorm(30)
|
48 | 55 | #' )
|
49 |
| -#' group_means_df <- do.call(rbind, lapply(split(sample_df, sample_df$group), function(d) { |
50 |
| -#' data.frame(group_mean = mean(d$value), group = d$group) |
51 |
| -#' })) |
| 56 | +#' |
| 57 | +#' group_means_df <- setNames( |
| 58 | +#' aggregate(value ~ group, sample_df, mean), |
| 59 | +#' c("group", "group_mean") |
| 60 | +#' ) |
| 61 | +#' |
| 62 | +#' # The following three code blocks create the same graphic, each using one |
| 63 | +#' # of the three patterns specified above. In each graphic, the group means |
| 64 | +#' # data frame is used to plot larger red points on top of the sample data. |
52 | 65 | #'
|
53 | 66 | #' # Pattern 1
|
54 |
| -#' # The group means data frame is used to plot larger red points on top |
55 |
| -#' # of the sample data. Note that we don't need to supply `data =` or `mapping =` |
56 |
| -#' # in each layer because the arguments are passed into ggplot() in the default |
57 |
| -#' # positions. |
58 |
| -#' ggplot(sample_df, aes(x = group, y = value)) + |
| 67 | +#' # Both the `data` and the `mapping` arguments are passed into the `ggplot()` |
| 68 | +#' # call. Note that we don't need to supply `mapping` or `data` arguments in |
| 69 | +#' # the first `geom_point()` layer because those arguments get passed along |
| 70 | +#' # from the `ggplot()` call. |
| 71 | +#' ggplot(data = sample_df, mapping = aes(x = group, y = value)) + |
59 | 72 | #' geom_point() +
|
60 |
| -#' geom_point(group_means_df, aes(y = group_mean), colour = 'red', size = 3) |
| 73 | +#' geom_point(mapping = aes(x = group, y = group_mean), data = group_means_df, |
| 74 | +#' colour = 'red', size = 3 |
| 75 | +#' ) |
61 | 76 | #'
|
62 | 77 | #' # Pattern 2
|
63 |
| -#' # Same plot as above, declaring only the data frame in ggplot(). |
64 |
| -#' # Note how the x and y aesthetics must now be declared in |
65 |
| -#' # each geom_point() layer. |
66 |
| -#' ggplot(sample_df) + |
67 |
| -#' geom_point(aes(x = group, y = value)) + |
68 |
| -#' geom_point(group_means_df, aes(x = group, y = group_mean), colour = 'red', size = 3) |
| 78 | +#' # Same plot as above, passing in only the `data` argument in the `ggplot()` |
| 79 | +#' # call. Note how the `mapping` arguments must now be passed in for each |
| 80 | +#' # `geom_point()` layer because there are no `mapping` arguments passed |
| 81 | +#' # along from the `ggplot()` call. |
| 82 | +#' ggplot(data = sample_df) + |
| 83 | +#' geom_point(mapping = aes(x = group, y = value)) + |
| 84 | +#' geom_point(mapping = aes(x = group, y = group_mean), data = group_means_df, |
| 85 | +#' colour = 'red', size = 3 |
| 86 | +#' ) |
69 | 87 | #'
|
70 | 88 | #' # Pattern 3
|
71 |
| -#' # Alternatively, we can fully specify the plot in each layer. This |
72 |
| -#' # can be particularly useful when working with complex, multi-dataset graphics. |
| 89 | +#' # Same plot as above, passing in neither the `data` or `mapping` arguments |
| 90 | +#' # to the `ggplot()` call. Both those arguments must then be fully specified |
| 91 | +#' # in each layer. This pattern can be particularly useful when creating more |
| 92 | +#' # complex graphics. |
73 | 93 | #' ggplot() +
|
74 |
| -#' geom_point(sample_df, aes(x = group, y = value)) + |
75 |
| -#' geom_point(group_means_df, aes(x = group, y = group_mean), colour = 'red', size = 3) |
| 94 | +#' geom_point(mapping = aes(x = group, y = value), data = sample_df) + |
| 95 | +#' geom_point(mapping = aes(x = group, y = group_mean), data = group_means_df, |
| 96 | +#' colour = 'red', size = 3 |
| 97 | +#' ) |
76 | 98 | ggplot <- function(data = NULL, mapping = aes(), ...,
|
77 | 99 | environment = parent.frame()) {
|
78 | 100 | UseMethod("ggplot")
|
|
0 commit comments