Skip to content

Commit 2b261d0

Browse files
authored
Update docstring changes to ggplot()
Changes per conversation with @teunbrand re: PR #5132
1 parent a18e92a commit 2b261d0

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

R/plot.r

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#' `ggplot()` is used to construct the initial plot object,
99
#' and is almost always followed by a plus sign (`+`) to add
1010
#' components to the plot.
11+
#'
1112
#' There are three common patterns used to invoke `ggplot()`:
1213
#'
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)`
1516
#' * `ggplot()`
1617
#'
1718
#' The first pattern is recommended if all layers use the same
@@ -22,13 +23,18 @@
2223
#' The second pattern specifies the default data frame to use
2324
#' for the plot, but no aesthetics are defined up front. This
2425
#' 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.
2627
#'
2728
#' The third pattern initializes a skeleton `ggplot` object, which
2829
#' is fleshed out as layers are added. This is useful when
2930
#' multiple data frames are used to produce different layers, as
3031
#' is often the case in complex graphics.
3132
#'
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+
#'
3238
#' @param data Default dataset to use for plot. If not already a data.frame,
3339
#' will be converted to one by [fortify()]. If not specified,
3440
#' must be supplied in each layer added to the plot.
@@ -40,39 +46,55 @@
4046
#' @export
4147
#' @examples
4248
#' # 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.
4450
#' set.seed(1)
51+
#'
4552
#' sample_df <- data.frame(
4653
#' group = factor(rep(letters[1:3], each = 10)),
4754
#' value = rnorm(30)
4855
#' )
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.
5265
#'
5366
#' # 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)) +
5972
#' 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+
#' )
6176
#'
6277
#' # 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+
#' )
6987
#'
7088
#' # 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.
7393
#' 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+
#' )
7698
ggplot <- function(data = NULL, mapping = aes(), ...,
7799
environment = parent.frame()) {
78100
UseMethod("ggplot")

0 commit comments

Comments
 (0)