|
1 |
| -#' Create a new ggplot plot. |
| 1 | +#' Create a new ggplot |
2 | 2 | #'
|
3 | 3 | #' \code{ggplot()} initializes a ggplot object. It can be used to
|
4 | 4 | #' declare the input data frame for a graphic and to specify the
|
5 | 5 | #' set of plot aesthetics intended to be common throughout all
|
6 | 6 | #' subsequent layers unless specifically overridden.
|
7 | 7 | #'
|
8 |
| -#' \code{ggplot()} is typically used to construct a plot |
9 |
| -#' incrementally, using the + operator to add layers to the |
10 |
| -#' existing ggplot object. This is advantageous in that the |
11 |
| -#' code is explicit about which layers are added and the order |
12 |
| -#' in which they are added. For complex graphics with multiple |
13 |
| -#' layers, initialization with \code{ggplot} is recommended. |
| 8 | +#' \code{ggplot()} is used to construct the initial plot object, |
| 9 | +#' and is almost always followed by \code{+} to add component to the |
| 10 | +#' plot. There are three common ways to invoke \code{ggplot}: |
| 11 | +#' |
| 12 | +#' \enumerate{ |
| 13 | +#' \item \code{ggplot(df, aes(x, y, <other aesthetics>))} |
| 14 | +#' \item \code{ggplot(df)} |
| 15 | +#' \item \code{ggplot()} |
| 16 | +#' } |
14 | 17 | #'
|
15 |
| -#' There are three common ways to invoke \code{ggplot}: |
16 |
| -#' \itemize{ |
17 |
| -#' \item \code{ggplot(df, aes(x, y, <other aesthetics>))} |
18 |
| -#' \item \code{ggplot(df)} |
19 |
| -#' \item \code{ggplot()} |
20 |
| -#' } |
21 | 18 | #' The first method is recommended if all layers use the same
|
22 | 19 | #' data and the same set of aesthetics, although this method
|
23 | 20 | #' can also be used to add a layer using data from another
|
|
42 | 39 | #' to using the environment in which \code{ggplot()} is called.
|
43 | 40 | #' @export
|
44 | 41 | #' @examples
|
45 |
| -#' df <- data.frame(gp = factor(rep(letters[1:3], each = 10)), |
46 |
| -#' y = rnorm(30)) |
47 |
| -#' # Compute sample mean and standard deviation in each group |
| 42 | +#' # Generate some sample data, then compute mean and standard deviation |
| 43 | +#' # in each group |
| 44 | +#' df <- data.frame( |
| 45 | +#' gp = factor(rep(letters[1:3], each = 10)), |
| 46 | +#' y = rnorm(30) |
| 47 | +#' ) |
48 | 48 | #' ds <- plyr::ddply(df, "gp", plyr::summarise, mean = mean(y), sd = sd(y))
|
49 | 49 | #'
|
50 |
| -#' # Declare the data frame and common aesthetics. |
51 |
| -#' # The summary data frame ds is used to plot |
52 |
| -#' # larger red points in a second geom_point() layer. |
53 |
| -#' # If the data = argument is not specified, it uses the |
54 |
| -#' # declared data frame from ggplot(); ditto for the aesthetics. |
55 |
| -#' ggplot(df, aes(x = gp, y = y)) + |
56 |
| -#' geom_point() + |
57 |
| -#' geom_point(data = ds, aes(y = mean), |
58 |
| -#' colour = 'red', size = 3) |
| 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)) + |
| 54 | +#' geom_point() + |
| 55 | +#' geom_point(data = ds, aes(y = mean), colour = 'red', size = 3) |
| 56 | +#' |
59 | 57 | #' # Same plot as above, declaring only the data frame in ggplot().
|
60 | 58 | #' # Note how the x and y aesthetics must now be declared in
|
61 | 59 | #' # each geom_point() layer.
|
62 | 60 | #' ggplot(df) +
|
63 |
| -#' geom_point(aes(x = gp, y = y)) + |
64 |
| -#' geom_point(data = ds, aes(x = gp, y = mean), |
65 |
| -#' colour = 'red', size = 3) |
66 |
| -#' # Set up a skeleton ggplot object and add layers: |
| 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 |
67 | 67 | #' ggplot() +
|
68 |
| -#' geom_point(data = df, aes(x = gp, y = y)) + |
69 |
| -#' geom_point(data = ds, aes(x = gp, y = mean), |
70 |
| -#' colour = 'red', size = 3) + |
71 |
| -#' geom_errorbar(data = ds, aes(x = gp, y = mean, |
72 |
| -#' ymin = mean - sd, ymax = mean + sd), |
73 |
| -#' colour = 'red', width = 0.4) |
| 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 | 76 | ggplot <- function(data = NULL, mapping = aes(), ...,
|
75 | 77 | environment = parent.frame()) {
|
76 | 78 | UseMethod("ggplot")
|
77 | 79 | }
|
78 | 80 |
|
79 | 81 | #' @export
|
80 |
| -#' @rdname ggplot |
81 |
| -#' @usage NULL |
82 | 82 | ggplot.default <- function(data = NULL, mapping = aes(), ...,
|
83 | 83 | environment = parent.frame()) {
|
84 | 84 | ggplot.data.frame(fortify(data, ...), mapping, environment = environment)
|
85 | 85 | }
|
86 | 86 |
|
87 | 87 | #' @export
|
88 |
| -#' @rdname ggplot |
89 |
| -#' @usage NULL |
90 | 88 | ggplot.data.frame <- function(data, mapping = aes(), ...,
|
91 | 89 | environment = parent.frame()) {
|
92 | 90 | if (!missing(mapping) && !inherits(mapping, "uneval")) {
|
|
0 commit comments