Skip to content

Commit aab9f2a

Browse files
authored
Improve clarity of docstring for ggplot()
changed "component" to "components" used "pattern" instead of "way" and "method" broke each pattern description in to separate paragraph used more explicit names in example code added more explicit comments in example code removed error bars in last pattern of example code (and removed standard deviations from the summary stats calculated)
1 parent d85eb62 commit aab9f2a

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

R/plot.r

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@
66
#' subsequent layers unless specifically overridden.
77
#'
88
#' `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()`:
1112
#'
1213
#' * `ggplot(df, aes(x, y, other aesthetics))`
1314
#' * `ggplot(df)`
1415
#' * `ggplot()`
1516
#'
16-
#' The first method is recommended if all layers use the same
17+
#' The first pattern is recommended if all layers use the same
1718
#' 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
2629
#' multiple data frames are used to produce different layers, as
2730
#' is often the case in complex graphics.
2831
#'
@@ -36,43 +39,41 @@
3639
#' evaluation.
3740
#' @export
3841
#' @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.
4144
#' 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)
4548
#' )
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)
4851
#' }))
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)) +
5459
#' 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
5763
#' # Same plot as above, declaring only the data frame in ggplot().
5864
#' # Note how the x and y aesthetics must now be declared in
5965
#' # 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.
6773
#' 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+
#' }
7677
ggplot <- function(data = NULL, mapping = aes(), ...,
7778
environment = parent.frame()) {
7879
UseMethod("ggplot")

0 commit comments

Comments
 (0)