|
6 | 6 | #' subsequent layers unless specifically overridden.
|
7 | 7 | #'
|
8 | 8 | #' `ggplot()` is used to construct the initial plot object,
|
9 |
| -#' and is almost always followed by `+` to add component to the |
10 |
| -#' plot. There are three common ways to invoke `ggplot()`: |
| 9 | +#' and is almost always followed by a plus sign (`+`) to add |
| 10 | +#' components to the plot. |
| 11 | +#' There are three common patterns used to invoke `ggplot()`: |
11 | 12 | #'
|
12 | 13 | #' * `ggplot(df, aes(x, y, other aesthetics))`
|
13 | 14 | #' * `ggplot(df)`
|
14 | 15 | #' * `ggplot()`
|
15 | 16 | #'
|
16 |
| -#' The first method is recommended if all layers use the same |
| 17 | +#' The first pattern is recommended if all layers use the same |
17 | 18 | #' data and the same set of aesthetics, although this method
|
18 |
| -#' can also be used to add a layer using data from another |
19 |
| -#' data frame. See the first example below. The second |
20 |
| -#' method specifies the default data frame to use for the plot, |
21 |
| -#' but no aesthetics are defined up front. This is useful when |
22 |
| -#' one data frame is used predominantly as layers are added, |
23 |
| -#' but the aesthetics may vary from one layer to another. The |
24 |
| -#' third method initializes a skeleton `ggplot` object which |
25 |
| -#' is fleshed out as layers are added. This method is useful when |
| 19 | +#' can also be used when adding a layer using data from another |
| 20 | +#' data frame. |
| 21 | +#' |
| 22 | +#' The second pattern specifies the default data frame to use |
| 23 | +#' for the plot, but no aesthetics are defined up front. This |
| 24 | +#' is useful when one data frame is used predominantly for the |
| 25 | +#' plot, but the aesthetics may vary from one layer to another. |
| 26 | +#' |
| 27 | +#' The third pattern initializes a skeleton `ggplot` object, which |
| 28 | +#' is fleshed out as layers are added. This is useful when |
26 | 29 | #' multiple data frames are used to produce different layers, as
|
27 | 30 | #' is often the case in complex graphics.
|
28 | 31 | #'
|
|
36 | 39 | #' evaluation.
|
37 | 40 | #' @export
|
38 | 41 | #' @examples
|
39 |
| -#' # Generate some sample data, then compute mean and standard deviation |
40 |
| -#' # in each group |
| 42 | +#' # Create a data frame with some sample data, then create a data frame |
| 43 | +#' # containing the mean for each group in the sample data. |
41 | 44 | #' set.seed(1)
|
42 |
| -#' df <- data.frame( |
43 |
| -#' gp = factor(rep(letters[1:3], each = 10)), |
44 |
| -#' y = rnorm(30) |
| 45 | +#' sample_df <- data.frame( |
| 46 | +#' group = factor(rep(letters[1:3], each = 10)), |
| 47 | +#' value = rnorm(30) |
45 | 48 | #' )
|
46 |
| -#' ds <- do.call(rbind, lapply(split(df, df$gp), function(d) { |
47 |
| -#' data.frame(mean = mean(d$y), sd = sd(d$y), gp = d$gp) |
| 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) |
48 | 51 | #' }))
|
49 |
| -#' |
50 |
| -#' # The summary data frame ds is used to plot larger red points on top |
51 |
| -#' # of the raw data. Note that we don't need to supply `data` or `mapping` |
52 |
| -#' # in each layer because the defaults from ggplot() are used. |
53 |
| -#' ggplot(df, aes(gp, y)) + |
| 52 | +#' |
| 53 | +#' # 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)) + |
54 | 59 | #' geom_point() +
|
55 |
| -#' geom_point(data = ds, aes(y = mean), colour = 'red', size = 3) |
56 |
| -#' |
| 60 | +#' geom_point(group_means_df, aes(y = group_mean), colour = 'red', size = 3) |
| 61 | +#' |
| 62 | +#' # Pattern 2 |
57 | 63 | #' # Same plot as above, declaring only the data frame in ggplot().
|
58 | 64 | #' # Note how the x and y aesthetics must now be declared in
|
59 | 65 | #' # each geom_point() layer.
|
60 |
| -#' ggplot(df) + |
61 |
| -#' geom_point(aes(gp, y)) + |
62 |
| -#' geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3) |
63 |
| -#' |
64 |
| -#' # Alternatively we can fully specify the plot in each layer. This |
65 |
| -#' # is not useful here, but can be more clear when working with complex |
66 |
| -#' # mult-dataset graphics |
| 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) |
| 69 | +#' |
| 70 | +#' # 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. |
67 | 73 | #' ggplot() +
|
68 |
| -#' geom_point(data = df, aes(gp, y)) + |
69 |
| -#' geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3) + |
70 |
| -#' geom_errorbar( |
71 |
| -#' data = ds, |
72 |
| -#' aes(gp, mean, ymin = mean - sd, ymax = mean + sd), |
73 |
| -#' colour = 'red', |
74 |
| -#' width = 0.4 |
75 |
| -#' ) |
| 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) |
| 76 | +#' } |
76 | 77 | ggplot <- function(data = NULL, mapping = aes(), ...,
|
77 | 78 | environment = parent.frame()) {
|
78 | 79 | UseMethod("ggplot")
|
|
0 commit comments