|
3 | 3 | #' @name aes_group_order |
4 | 4 | #' @aliases group |
5 | 5 | #' |
6 | | -#' @examples |
7 | | -#' \donttest{ |
| 6 | +#' @description |
| 7 | +#' The `group` aesthetic is by default set to the interaction of all discrete variables |
| 8 | +#' in the plot. This choice often partitions the data correctly, but when it does not, |
| 9 | +#' or when no discrete variable is used in the plot, you will need to explicitly define the |
| 10 | +#' grouping structure by mapping `group` to a variable that has a different value |
| 11 | +#' for each group. |
| 12 | +#' |
| 13 | +#' @details |
| 14 | +#' For most applications the grouping is set implicitly by mapping one or more |
| 15 | +#' discrete variables to `x`, `y`, `colour`, `fill`, `alpha`, `shape`, `size`, |
| 16 | +#' and/or `linetype`. This is demonstrated in the examples below. |
| 17 | +#' |
| 18 | +#' There are three common cases where the default does not display the data correctly. |
| 19 | +#' The examples below use a longitudinal dataset, `Oxboys`, from the nlme package to demonstrate |
| 20 | +#' these cases. `Oxboys` records the heights (height) and centered ages (age) of 26 boys (Subject), |
| 21 | +#' measured on nine occasions (Occasion). |
8 | 22 | #' |
9 | | -#' # By default, the group is set to the interaction of all discrete variables in the |
10 | | -#' # plot. This often partitions the data correctly, but when it does not, or when |
11 | | -#' # no discrete variable is used in the plot, you will need to explicitly define the |
12 | | -#' # grouping structure, by mapping group to a variable that has a different value |
13 | | -#' # for each group. |
| 23 | +#' @seealso |
| 24 | +#' * Geoms commonly used with groups: [geom_bar()], [geom_histogram()], [geom_line()] |
| 25 | +#' * Run `vignette("ggplot2-specs")` to see an overview of other aesthestics that |
| 26 | +#' can be modified. |
14 | 27 | #' |
15 | | -#' # For most applications you can simply specify the grouping with |
16 | | -#' # various aesthetics (colour, shape, fill, linetype) or with facets. |
| 28 | +#' @examples |
| 29 | +#' \donttest{ |
17 | 30 | #' |
18 | 31 | #' p <- ggplot(mtcars, aes(wt, mpg)) |
19 | 32 | #' # A basic scatter plot |
20 | 33 | #' p + geom_point(size = 4) |
21 | | -#' # The colour aesthetic |
| 34 | +#' # Using the colour aesthetic |
22 | 35 | #' p + geom_point(aes(colour = factor(cyl)), size = 4) |
23 | | -#' # Or you can use shape to distinguish the data |
| 36 | +#' # Using the shape aesthetic |
24 | 37 | #' p + geom_point(aes(shape = factor(cyl)), size = 4) |
25 | 38 | #' |
26 | 39 | #' # Using fill |
27 | | -#' a <- ggplot(mtcars, aes(factor(cyl))) |
28 | | -#' a + geom_bar() |
29 | | -#' a + geom_bar(aes(fill = factor(cyl))) |
30 | | -#' a + geom_bar(aes(fill = factor(vs))) |
| 40 | +#' p <- ggplot(mtcars, aes(factor(cyl))) |
| 41 | +#' p + geom_bar() |
| 42 | +#' p + geom_bar(aes(fill = factor(cyl))) |
| 43 | +#' p + geom_bar(aes(fill = factor(vs))) |
31 | 44 | #' |
32 | 45 | #' # Using linetypes |
33 | | -#' f <- ggplot(economics_long, aes(date, value01)) |
34 | | -#' f + geom_line(aes(linetype = variable)) |
35 | | -#' |
36 | | -#' # Using facets |
37 | | -#' k <- ggplot(diamonds, aes(carat, after_stat(density))) + |
38 | | -#' geom_histogram(binwidth = 0.2) |
39 | | -#' k + facet_grid(. ~ cut) |
40 | | -#' |
41 | | -#' # There are three common cases where the default is not enough, and we |
42 | | -#' # will consider each one below. In the following examples, we will use a simple |
43 | | -#' # longitudinal dataset, Oxboys, from the nlme package. It records the heights |
44 | | -#' # (height) and centered ages (age) of 26 boys (Subject), measured on nine |
45 | | -#' # occasions (Occasion). |
| 46 | +#' ggplot(economics_long, aes(date, value01)) + |
| 47 | +#' geom_line(aes(linetype = variable)) |
46 | 48 | #' |
47 | 49 | #' # Multiple groups with one aesthetic |
48 | | -#' h <- ggplot(nlme::Oxboys, aes(age, height)) |
49 | | -#' # A single line tries to connect all the observations |
50 | | -#' h + geom_line() |
51 | | -#' # The group aesthetic maps a different line for each subject |
52 | | -#' h + geom_line(aes(group = Subject)) |
| 50 | +#' p <- ggplot(nlme::Oxboys, aes(age, height)) |
| 51 | +#' # The default is not sufficient here. A single line tries to connect all |
| 52 | +#' # the observations. |
| 53 | +#' p + geom_line() |
| 54 | +#' # To fix this, use the group aesthetic to map a different line for each |
| 55 | +#' # subject. |
| 56 | +#' p + geom_line(aes(group = Subject)) |
53 | 57 | #' |
54 | 58 | #' # Different groups on different layers |
55 | | -#' h <- h + geom_line(aes(group = Subject)) |
| 59 | +#' p <- p + geom_line(aes(group = Subject)) |
56 | 60 | #' # Using the group aesthetic with both geom_line() and geom_smooth() |
57 | 61 | #' # groups the data the same way for both layers |
58 | | -#' h + geom_smooth(aes(group = Subject), method = "lm", se = FALSE) |
| 62 | +#' p + geom_smooth(aes(group = Subject), method = "lm", se = FALSE) |
59 | 63 | #' # Changing the group aesthetic for the smoother layer |
60 | 64 | #' # fits a single line of best fit across all boys |
61 | | -#' h + geom_smooth(aes(group = 1), size = 2, method = "lm", se = FALSE) |
| 65 | +#' p + geom_smooth(aes(group = 1), size = 2, method = "lm", se = FALSE) |
62 | 66 | #' |
63 | 67 | #' # Overriding the default grouping |
64 | | -#' # The plot has a discrete scale but you want to draw lines that connect across |
65 | | -#' # groups. This is the strategy used in interaction plots, profile plots, and parallel |
66 | | -#' # coordinate plots, among others. For example, we draw boxplots of height at |
67 | | -#' # each measurement occasion |
68 | | -#' boysbox <- ggplot(nlme::Oxboys, aes(Occasion, height)) |
69 | | -#' boysbox + geom_boxplot() |
| 68 | +#' # Sometimes the plot has a discrete scale but you want to draw lines |
| 69 | +#' # that connect across groups. This is the strategy used in interaction |
| 70 | +#' # plots, profile plots, and parallel coordinate plots, among others. |
| 71 | +#' # For example, we draw boxplots of height at each measurement occasion. |
| 72 | +#' p <- ggplot(nlme::Oxboys, aes(Occasion, height)) + geom_boxplot() |
| 73 | +#' p |
70 | 74 | #' # There is no need to specify the group aesthetic here; the default grouping |
71 | | -#' # works because occasion is a discrete variable. To overlay individual trajectories |
72 | | -#' # we again need to override the default grouping for that layer with aes(group = Subject) |
73 | | -#' boysbox <- boysbox + geom_boxplot() |
74 | | -#' boysbox + geom_line(aes(group = Subject), colour = "blue") |
| 75 | +#' # works because occasion is a discrete variable. To overlay individual |
| 76 | +#' # trajectories, we again need to override the default grouping for that layer |
| 77 | +#' # with aes(group = Subject) |
| 78 | +#' p + geom_line(aes(group = Subject), colour = "blue") |
75 | 79 | #' } |
76 | 80 | NULL |
0 commit comments