forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_group_order.Rd
83 lines (76 loc) · 3.32 KB
/
aes_group_order.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/aes-group-order.r
\name{aes_group_order}
\alias{aes_group_order}
\alias{group}
\title{Aesthetics: grouping}
\description{
The \code{group} aesthetic is by default set to the interaction of all discrete variables
in the plot. This choice often partitions the data correctly, but when it does not,
or when no discrete variable is used in the plot, you will need to explicitly define the
grouping structure by mapping \code{group} to a variable that has a different value
for each group.
}
\details{
For most applications the grouping is set implicitly by mapping one or more
discrete variables to \code{x}, \code{y}, \code{colour}, \code{fill}, \code{alpha}, \code{shape}, \code{size},
and/or \code{linetype}. This is demonstrated in the examples below.
There are three common cases where the default does not display the data correctly.
The examples below use a longitudinal dataset, \code{Oxboys}, from the nlme package to demonstrate
these cases. \code{Oxboys} records the heights (height) and centered ages (age) of 26 boys (Subject),
measured on nine occasions (Occasion).
}
\examples{
\donttest{
p <- ggplot(mtcars, aes(wt, mpg))
# A basic scatter plot
p + geom_point(size = 4)
# Using the colour aesthetic
p + geom_point(aes(colour = factor(cyl)), size = 4)
# Using the shape aesthetic
p + geom_point(aes(shape = factor(cyl)), size = 4)
# Using fill
p <- ggplot(mtcars, aes(factor(cyl)))
p + geom_bar()
p + geom_bar(aes(fill = factor(cyl)))
p + geom_bar(aes(fill = factor(vs)))
# Using linetypes
ggplot(economics_long, aes(date, value01)) +
geom_line(aes(linetype = variable))
# Multiple groups with one aesthetic
p <- ggplot(nlme::Oxboys, aes(age, height))
# The default is not sufficient here. A single line tries to connect all
# the observations.
p + geom_line()
# To fix this, use the group aesthetic to map a different line for each
# subject.
p + geom_line(aes(group = Subject))
# Different groups on different layers
p <- p + geom_line(aes(group = Subject))
# Using the group aesthetic with both geom_line() and geom_smooth()
# groups the data the same way for both layers
p + geom_smooth(aes(group = Subject), method = "lm", se = FALSE)
# Changing the group aesthetic for the smoother layer
# fits a single line of best fit across all boys
p + geom_smooth(aes(group = 1), size = 2, method = "lm", se = FALSE)
# Overriding the default grouping
# Sometimes the plot has a discrete scale but you want to draw lines
# that connect across groups. This is the strategy used in interaction
# plots, profile plots, and parallel coordinate plots, among others.
# For example, we draw boxplots of height at each measurement occasion.
p <- ggplot(nlme::Oxboys, aes(Occasion, height)) + geom_boxplot()
p
# There is no need to specify the group aesthetic here; the default grouping
# works because occasion is a discrete variable. To overlay individual
# trajectories, we again need to override the default grouping for that layer
# with aes(group = Subject)
p + geom_line(aes(group = Subject), colour = "blue")
}
}
\seealso{
\itemize{
\item Geoms commonly used with groups: \code{\link[=geom_bar]{geom_bar()}}, \code{\link[=geom_histogram]{geom_histogram()}}, \code{\link[=geom_line]{geom_line()}}
\item Run \code{vignette("ggplot2-specs")} to see an overview of other aesthestics that
can be modified.
}
}