Skip to content

Commit 0f725e1

Browse files
improve aesthetic documentation (#3788)
* improve aesthetic documentation * rewording alpha description * clean up colour fill alpha docs * clean up aes group order * cleanup linetype size shape * clean up aes position * minor cleanup * copy edits * copy edit Co-authored-by: Claus Wilke <wilke@austin.utexas.edu>
1 parent e3fbccc commit 0f725e1

21 files changed

+412
-274
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,6 @@ Collate:
262262
'zxx.r'
263263
'zzz.r'
264264
VignetteBuilder: knitr
265-
RoxygenNote: 7.0.2
265+
RoxygenNote: 7.1.0
266266
Roxygen: list(markdown = TRUE)
267267
Encoding: UTF-8

R/aes-colour-fill-alpha.r

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,57 +39,62 @@
3939
#' [scale_fill_hue()], [scale_fill_identity()],
4040
#' [scale_fill_manual()], [scale_fill_viridis_d()]
4141
#' * Other options for modifying alpha: [scale_alpha()]
42-
#' * `vignette("ggplot2-specs")` provides an overview of other aesthestics that
43-
#' can be modified
42+
#' * Run `vignette("ggplot2-specs")` to see an overview of other aesthestics that
43+
#' can be modified.
4444
#'
4545
#' @name aes_colour_fill_alpha
4646
#' @aliases colour color fill
4747
#' @examples
4848
#' \donttest{
4949
#'
5050
#' # Bar chart example
51-
#' c <- ggplot(mtcars, aes(factor(cyl)))
51+
#' p <- ggplot(mtcars, aes(factor(cyl)))
5252
#' # Default plotting
53-
#' c + geom_bar()
53+
#' p + geom_bar()
5454
#' # To change the interior colouring use fill aesthetic
55-
#' c + geom_bar(fill = "red")
55+
#' p + geom_bar(fill = "red")
5656
#' # Compare with the colour aesthetic which changes just the bar outline
57-
#' c + geom_bar(colour = "red")
57+
#' p + geom_bar(colour = "red")
5858
#' # Combining both, you can see the changes more clearly
59-
#' c + geom_bar(fill = "white", colour = "red")
59+
#' p + geom_bar(fill = "white", colour = "red")
60+
#' # Both colour and fill can take an rgb specification.
61+
#' p + geom_bar(fill = "#00abff")
62+
#' # Use NA for a completely transparent colour.
63+
#' p + geom_bar(fill = NA, colour = "#00abff")
6064
#'
61-
#' # The aesthetic fill also takes different colouring scales
62-
#' # setting fill equal to a factor variable uses a discrete colour scale
63-
#' k <- ggplot(mtcars, aes(factor(cyl), fill = factor(vs)))
64-
#' k + geom_bar()
65+
#' # Colouring scales differ depending on whether a discrete or
66+
#' # continuous variable is being mapped. For example, when mapping
67+
#' # fill to a factor variable, a discrete colour scale is used.
68+
#' ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar()
6569
#'
66-
#' # Fill aesthetic can also be used with a continuous variable
67-
#' m <- ggplot(faithfuld, aes(waiting, eruptions))
68-
#' m + geom_raster()
69-
#' m + geom_raster(aes(fill = density))
70+
#' # When mapping fill to continuous variable a continuous colour
71+
#' # scale is used.
72+
#' ggplot(faithfuld, aes(waiting, eruptions)) +
73+
#' geom_raster(aes(fill = density))
7074
#'
71-
#' # Some geoms don't use both aesthetics (i.e. geom_point or geom_line)
72-
#' b <- ggplot(economics, aes(x = date, y = unemploy))
73-
#' b + geom_line()
74-
#' b + geom_line(colour = "green")
75-
#' b + geom_point()
76-
#' b + geom_point(colour = "red")
75+
#' # Some geoms only use the colour aesthetic but not the fill
76+
#' # aesthetic (e.g. geom_point() or geom_line()).
77+
#' p <- ggplot(economics, aes(x = date, y = unemploy))
78+
#' p + geom_line()
79+
#' p + geom_line(colour = "green")
80+
#' p + geom_point()
81+
#' p + geom_point(colour = "red")
7782
#'
7883
#' # For large datasets with overplotting the alpha
79-
#' # aesthetic will make the points more transparent
84+
#' # aesthetic will make the points more transparent.
8085
#' df <- data.frame(x = rnorm(5000), y = rnorm(5000))
81-
#' h <- ggplot(df, aes(x,y))
82-
#' h + geom_point()
83-
#' h + geom_point(alpha = 0.5)
84-
#' h + geom_point(alpha = 1/10)
86+
#' p <- ggplot(df, aes(x,y))
87+
#' p + geom_point()
88+
#' p + geom_point(alpha = 0.5)
89+
#' p + geom_point(alpha = 1/10)
8590
#'
86-
#' # Alpha can also be used to add shading
87-
#' j <- b + geom_line()
88-
#' j
91+
#' # Alpha can also be used to add shading.
92+
#' p <- ggplot(economics, aes(x = date, y = unemploy)) + geom_line()
93+
#' p
8994
#' yrng <- range(economics$unemploy)
90-
#' j <- j + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),
95+
#' p <- p + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),
9196
#' ymin = yrng[1], ymax = yrng[2], data = presidential)
92-
#' j
93-
#' j + scale_fill_manual(values = alpha(c("blue", "red"), .3))
97+
#' p
98+
#' p + scale_fill_manual(values = alpha(c("blue", "red"), .3))
9499
#' }
95100
NULL

R/aes-group-order.r

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,78 @@
33
#' @name aes_group_order
44
#' @aliases group
55
#'
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).
822
#'
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.
1427
#'
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{
1730
#'
1831
#' p <- ggplot(mtcars, aes(wt, mpg))
1932
#' # A basic scatter plot
2033
#' p + geom_point(size = 4)
21-
#' # The colour aesthetic
34+
#' # Using the colour aesthetic
2235
#' p + geom_point(aes(colour = factor(cyl)), size = 4)
23-
#' # Or you can use shape to distinguish the data
36+
#' # Using the shape aesthetic
2437
#' p + geom_point(aes(shape = factor(cyl)), size = 4)
2538
#'
2639
#' # 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)))
3144
#'
3245
#' # 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))
4648
#'
4749
#' # 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))
5357
#'
5458
#' # Different groups on different layers
55-
#' h <- h + geom_line(aes(group = Subject))
59+
#' p <- p + geom_line(aes(group = Subject))
5660
#' # Using the group aesthetic with both geom_line() and geom_smooth()
5761
#' # 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)
5963
#' # Changing the group aesthetic for the smoother layer
6064
#' # 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)
6266
#'
6367
#' # 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
7074
#' # 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")
7579
#' }
7680
NULL

R/aes-linetype-size-shape.r

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,74 @@
11
#' Differentiation related aesthetics: linetype, size, shape
22
#'
3-
#' This page demonstrates the usage of a sub-group
4-
#' of aesthetics; linetype, size and shape.
3+
#' @description
4+
#' The `linetype`, `size`, and `shape` aesthetics modify the appearance of
5+
#' lines and/or points. They also apply to the outlines of polygons (`linetype`
6+
#' and `size`) or to text (`size`).
57
#'
8+
#' The `linetype` aesthetic can be specified with either an integer (0-6), a
9+
#' name (0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash,
10+
#' 6 = twodash), a mapping to a discrete variable, or a string of an even number
11+
#' (up to eight) of hexadecimal digits which give the lengths in consecutive
12+
#' positions in the string. See examples for a hex string demonstration.
13+
#'
14+
#' The `size` aesthetic can be specified with a numerical value (in millimetres)
15+
#' or via a mapping to a continuous variable.
16+
#'
17+
#' The `shape` aesthetic can be specified with an integer (between 0 and 25),
18+
#' a single character (which uses that character as the plotting symbol),
19+
#' a `.` to draw the smallest rectangle that is visible (i.e., about one pixel),
20+
#' an `NA` to draw nothing, or a mapping to a discrete variable. Symbols and
21+
#' filled shapes are described in the examples below.
22+
#'
23+
#' @seealso
24+
#' * [geom_line()] and [geom_point()] for geoms commonly used
25+
#' with these aesthetics.
26+
#' * [aes_group_order()] for using `linetype`, `size`, or
27+
#' `shape` for grouping.
28+
#' * Run `vignette("ggplot2-specs")` to see an overview of other aesthestics that
29+
#' can be modified.
630
#' @name aes_linetype_size_shape
731
#' @aliases linetype size shape
832
#' @examples
933
#'
10-
#' # Line types should be specified with either an integer, a name, or with a string of
11-
#' # an even number (up to eight) of hexadecimal digits which give the lengths in
12-
#' # consecutive positions in the string.
13-
#' # 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash
14-
#'
15-
#' # Data
1634
#' df <- data.frame(x = 1:10 , y = 1:10)
17-
#' f <- ggplot(df, aes(x, y))
18-
#' f + geom_line(linetype = 2)
19-
#' f + geom_line(linetype = "dotdash")
35+
#' p <- ggplot(df, aes(x, y))
36+
#' p + geom_line(linetype = 2)
37+
#' p + geom_line(linetype = "dotdash")
2038
#'
21-
#' # An example with hex strings, the string "33" specifies three units on followed
39+
#' # An example with hex strings; the string "33" specifies three units on followed
2240
#' # by three off and "3313" specifies three units on followed by three off followed
2341
#' # by one on and finally three off.
24-
#' f + geom_line(linetype = "3313")
42+
#' p + geom_line(linetype = "3313")
2543
#'
26-
#' # Mapping line type from a variable
44+
#' # Mapping line type from a grouping variable
2745
#' ggplot(economics_long, aes(date, value01)) +
2846
#' geom_line(aes(linetype = variable))
2947
#'
3048
#' # Size examples
31-
#' # Should be specified with a numerical value (in millimetres),
32-
#' # or from a variable source
3349
#' p <- ggplot(mtcars, aes(wt, mpg))
3450
#' p + geom_point(size = 4)
3551
#' p + geom_point(aes(size = qsec))
3652
#' p + geom_point(size = 2.5) +
3753
#' geom_hline(yintercept = 25, size = 3.5)
3854
#'
3955
#' # Shape examples
40-
#' # Shape takes four types of values: an integer in [0, 25],
41-
#' # a single character-- which uses that character as the plotting symbol,
42-
#' # a . to draw the smallest rectangle that is visible (i.e., about one pixel)
43-
#' # an NA to draw nothing
4456
#' p + geom_point()
4557
#' p + geom_point(shape = 5)
4658
#' p + geom_point(shape = "k", size = 3)
4759
#' p + geom_point(shape = ".")
4860
#' p + geom_point(shape = NA)
49-
#'
50-
#' # Shape can also be mapped from a variable
5161
#' p + geom_point(aes(shape = factor(cyl)))
5262
#'
5363
#' # A look at all 25 symbols
5464
#' df2 <- data.frame(x = 1:5 , y = 1:25, z = 1:25)
55-
#' s <- ggplot(df2, aes(x, y))
56-
#' s + geom_point(aes(shape = z), size = 4) +
65+
#' p <- ggplot(df2, aes(x, y))
66+
#' p + geom_point(aes(shape = z), size = 4) +
5767
#' scale_shape_identity()
5868
#' # While all symbols have a foreground colour, symbols 19-25 also take a
5969
#' # background colour (fill)
60-
#' s + geom_point(aes(shape = z), size = 4, colour = "Red") +
70+
#' p + geom_point(aes(shape = z), size = 4, colour = "Red") +
6171
#' scale_shape_identity()
62-
#' s + geom_point(aes(shape = z), size = 4, colour = "Red", fill = "Black") +
72+
#' p + geom_point(aes(shape = z), size = 4, colour = "Red", fill = "Black") +
6373
#' scale_shape_identity()
6474
NULL

0 commit comments

Comments
 (0)