Skip to content

Commit 8ef6862

Browse files
committed
Doc tweaks for themes
1 parent 5cbce9b commit 8ef6862

16 files changed

+155
-181
lines changed

R/geom-.r

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ Geom <- ggproto("Geom",
154154
#' that grid uses internally for \code{lwd} and \code{fontsize}.
155155
#'
156156
#' @name graphical-units
157+
#' @keywords internal
158+
#' @aliases NULL
157159
NULL
158160

159161
#' @export

R/margins.R

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
#' Define margins.
2-
#'
3-
#' This is a convenient function that creates a grid unit object of the
4-
#' correct length to use for setting margins.
5-
#'
6-
#' @export
71
#' @param t,r,b,l Dimensions of each margin. (To remember order, think trouble).
82
#' @param unit Default units of dimensions. Defaults to "pt" so it
93
#' can be most easily scaled with the text.
4+
#' @rdname element
105
#' @export
11-
#' @examples
12-
#' margin(4)
13-
#' margin(4, 2)
14-
#' margin(4, 3, 2, 1)
156
margin <- function(t = 0, r = 0, b = 0, l = 0, unit = "pt") {
167
structure(unit(c(t, r, b, l), unit), class = c("margin", "unit"))
178
}

R/theme-current.R

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
theme_env <- new.env(parent = emptyenv())
44
theme_env$current <- theme_gray()
55

6-
#' Get, set and update themes
6+
#' Get, set, and modify the active theme
77
#'
8+
#' The current/active theme is automatically applied to every plot you draw.
89
#' Use \code{theme_get} to get the current theme, and \code{theme_set} to
910
#' completely override it. \code{theme_update} and \code{theme_replace} are
10-
#' shorthands for changing individual elements in the current theme.
11+
#' shorthands for changing individual elements.
1112
#'
1213
#' @section Adding on to a theme:
1314
#'
@@ -29,49 +30,41 @@ theme_env$current <- theme_gray()
2930
#'
3031
#' @param ... named list of theme settings
3132
#' @param e1,e2 Theme and element to combine
33+
#' @return \code{theme_set}, \code{theme_update}, and \code{theme_replace}
34+
#' invisibly return the previous theme so you can easily save it, then
35+
#' later restore it.
3236
#' @seealso \code{\link{+.gg}}
3337
#' @export
3438
#' @examples
3539
#' p <- ggplot(mtcars, aes(mpg, wt)) +
3640
#' geom_point()
3741
#' p
38-
#' old <- theme_set(theme_bw())
39-
#' p
40-
#' theme_set(old)
41-
#' p
4242
#'
43-
#' #theme_replace NULLs out the fill attribute of panel.background,
44-
#' #resulting in a white background:
45-
#' theme_get()$panel.background
46-
#' old <- theme_replace(panel.background = element_rect(colour = "pink"))
47-
#' theme_get()$panel.background
43+
#' # Use theme_set() to completely override the current theme.
44+
#' # Here we have the old theme so we can later restore it.
45+
#' # Note that the theme is applied when the plot is drawn, not
46+
#' # when it is created.
47+
#' old <- theme_set(theme_bw())
4848
#' p
4949
#' theme_set(old)
50-
#'
51-
#' #theme_update only changes the colour attribute, leaving the others intact:
52-
#' old <- theme_update(panel.background = element_rect(colour = "pink"))
53-
#' theme_get()$panel.background
5450
#' p
55-
#' theme_set(old)
56-
#'
57-
#' theme_get()
58-
#'
5951
#'
60-
#' ggplot(mtcars, aes(mpg, wt)) +
61-
#' geom_point(aes(color = mpg)) +
62-
#' theme(legend.position = c(0.95, 0.95),
63-
#' legend.justification = c(1, 1))
64-
#' last_plot() +
65-
#' theme(legend.background = element_rect(fill = "white", colour = "white", size = 3))
6652
#'
67-
#' # Adding on to a theme ----------------------------------------------
68-
#'
69-
#' # Compare these results of adding theme objects to other theme objects
70-
#' add_el <- theme_grey() + theme(text = element_text(family = "Times"))
71-
#' rep_el <- theme_grey() %+replace% theme(text = element_text(family = "Times"))
53+
#' # Modifying theme objects -----------------------------------------
54+
#' # You can use + and %+replace% to modify a theme object.
55+
#' # They differ in how they deal with missing arguments in
56+
#' # the theme elements.
7257
#'
58+
#' add_el <- theme_grey() +
59+
#' theme(text = element_text(family = "Times"))
7360
#' add_el$text
61+
#'
62+
#' rep_el <- theme_grey() %+replace%
63+
#' theme(text = element_text(family = "Times"))
7464
#' rep_el$text
65+
#'
66+
#' # theme_update() and theme_replace() are similar except they
67+
#' # apply directly to the current/active theme.
7568
theme_get <- function() {
7669
theme_env$current
7770
}

R/theme-defaults.r

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
#' ggplot2 themes
1+
#' Complete themes
22
#'
3-
#' Themes set the general aspect of the plot such as the colour of the
4-
#' background, gridlines, the size and colour of fonts.
3+
#' These are complete themes which control all non-data display. Use
4+
#' \code{\link{theme}} if you just need to tweak the display of an existing
5+
#' theme.
56
#'
67
#' @param base_size base font size
78
#' @param base_family base font family
89
#'
9-
#' @details \describe{
10+
#' @details
11+
#' \describe{
1012
#'
1113
#' \item{\code{theme_gray}}{
1214
#' The signature ggplot2 theme with a grey background and white gridlines,
@@ -43,18 +45,16 @@
4345
#' @examples
4446
#' p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg,
4547
#' colour = factor(gear))) + facet_wrap(~am)
46-
#'
47-
#' p
48+
#' p + theme_gray() # the default
4849
#' p + theme_bw()
4950
#' p + theme_linedraw()
5051
#' p + theme_light()
5152
#' p + theme_dark()
5253
#' p + theme_minimal()
5354
#' p + theme_classic()
5455
#' p + theme_void()
55-
#' p + theme_gray() # default theme
56-
#'
5756
#' @name ggtheme
57+
#' @aliases NULL
5858
NULL
5959

6060
#' @include theme.r

R/theme-elements.r

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#' Theme elements
22
#'
3-
#' In conjunction with the \link{theme} system, these objects specify the display of
4-
#' how non-data components of the plot are a drawn.
3+
#' @description
4+
#' In conjunction with the \link{theme} system, the \code{element_} functions
5+
#' specify the display of how non-data components of the plot are a drawn.
56
#'
67
#' \itemize{
78
#' \item \code{element_blank}: draws nothing, and assigns no space.
@@ -10,16 +11,18 @@
1011
#' \item \code{element_text}: text.
1112
#' }
1213
#'
14+
#' \code{rel()} is used to specify sizes relative to the parent,
15+
#' \code{margins()} is used to specify the margins of elements.
16+
#'
1317
#' @param fill Fill colour.
1418
#' @param colour,color Line/border colour. Color is an alias for colour.
1519
#' @param size Line/border size in mm; text size in pts.
1620
#' @param inherit.blank Should this element inherit the existence of an
17-
#' element_blank among its parents? If \code{TRUE} the existence of a blank
18-
#' element among its parents will cause this element to be blank as well. If
19-
#' \code{FALSE} any blank parent element will be ignored when calculating final
20-
#' element state.
21-
#' @name element
22-
#' @return An S3 object of class \code{element}.
21+
#' \code{element_blank} among its parents? If \code{TRUE} the existence of
22+
#' a blank element among its parents will cause this element to be blank as
23+
#' well. If \code{FALSE} any blank parent element will be ignored when
24+
#' calculating final element state.
25+
#' @return An S3 object of class \code{element}, \code{rel}, or \code{margin}.
2326
#' @examples
2427
#' plot <- ggplot(mpg, aes(displ, hwy)) + geom_point()
2528
#'
@@ -29,17 +32,24 @@
2932
#' )
3033
#'
3134
#' plot + theme(
32-
#' axis.text = element_text(colour = "red", size = 12)
35+
#' axis.text = element_text(colour = "red", size = rel(1.5))
3336
#' )
3437
#'
3538
#' plot + theme(
36-
#' axis.line.x = element_line()
39+
#' axis.line = element_line(arrow = arrow())
3740
#' )
3841
#'
3942
#' plot + theme(
40-
#' panel.background = element_rect(fill = "grey95"),
41-
#' panel.border = element_rect(colour = "grey70", fill = NA, size = 1)
43+
#' panel.background = element_rect(fill = "white"),
44+
#' plot.margin = margin(2, 2, 2, 2, "cm"),
45+
#' plot.background = element_rect(
46+
#' fill = "grey90",
47+
#' colour = "black",
48+
#' size = 1
49+
#' )
4250
#' )
51+
#' @name element
52+
#' @aliases NULL
4353
NULL
4454

4555
#' @export
@@ -117,14 +127,7 @@ element_text <- function(family = NULL, face = NULL, colour = NULL,
117127
print.element <- function(x, ...) utils::str(x)
118128

119129

120-
#' Relative sizing for theme elements
121-
#'
122-
#' @param x A number representing the relative size
123-
#' @examples
124-
#' df <- data.frame(x = 1:3, y = 1:3)
125-
#' ggplot(df, aes(x, y)) +
126-
#' geom_point() +
127-
#' theme(axis.title.x = element_text(size = rel(2.5)))
130+
#' @rdname element
128131
#' @export
129132
rel <- function(x) {
130133
structure(x, class = "rel")
@@ -135,6 +138,7 @@ print.rel <- function(x, ...) print(noquote(paste(x, " *", sep = "")))
135138

136139
#' Reports whether x is a rel object
137140
#' @param x An object to test
141+
#' @keywords internal
138142
is.rel <- function(x) inherits(x, "rel")
139143

140144
# Given a theme object and element name, return a grob for the element

R/theme.r

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
#' Set theme elements
1+
#' Modify components of a theme
22
#'
3-
#' Theme elements can inherit properties from other theme elements.
3+
#' Use \code{theme()} to modify individual components of a theme, allowing
4+
#' you to control the appearance of all non-data components of the plot.
5+
#' \code{theme()} only affects a single plot: see \code{\link{theme_update}} if
6+
#' you want modify the active theme, to affect all subsequent plots.
7+
#'
8+
#' @section Theme inheritance:
9+
#' Theme elements inherit properties from other theme elements.
410
#' For example, \code{axis.title.x} inherits from \code{axis.title},
511
#' which in turn inherits from \code{text}. All text elements inherit
612
#' directly or indirectly from \code{text}; all lines inherit from
713
#' \code{line}, and all rectangular objects inherit from \code{rect}.
14+
#' This means that you can modify the appearance of multiple elements by
15+
#' setting a single high-level component.
816
#'
917
#' @param line all line elements (\code{element_line})
1018
#' @param rect all rectangular elements (\code{element_rect})
@@ -155,7 +163,6 @@
155163
#'
156164
#' @seealso
157165
#' \code{\link{+.gg}} and \code{\link{\%+replace\%}},
158-
#' \code{\link{rel}} for details of relative sizing,
159166
#' \code{\link{element_blank}}, \code{\link{element_line}},
160167
#' \code{\link{element_rect}}, and \code{\link{element_text}} for
161168
#' details of the specific theme elements.
@@ -564,6 +571,7 @@ combine_elements <- function(e1, e2) {
564571
#' Reports whether x is a theme object
565572
#' @param x An object to test
566573
#' @export
574+
#' @keywords internal
567575
is.theme <- function(x) inherits(x, "theme")
568576

569577
#' @export

_pkgdown.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ reference:
110110
- label_bquote
111111

112112
- title: Coordinate systems
113-
desc:
113+
desc: >
114114
The coordinate system determines how the `x` and `y` aesthetics combine
115115
to position elements in the plot. The default coordinate system is
116116
Cartesian (`coord_cartesian()`), which can be tweaked with `coord_map()`,
@@ -125,16 +125,18 @@ reference:
125125
- coord_trans
126126

127127
- title: Themes
128+
desc: >
129+
Themes control the display of all non-data elements of the plot. You
130+
can override all settings with a complete theme like `theme_bw()`, or
131+
choose to tweak individual settings by using `theme()` and the `element_`
132+
functions. Use `theme_set()` to modify the active theme, affecting all
133+
future plots.
128134
contents:
129-
- ggtheme
130135
- theme
136+
- theme_bw
131137
- theme_update
132-
- element
133-
- rel
134-
- is.rel
135-
- is.theme
138+
- element_line
136139
- margin
137-
- graphical-units
138140

139141
- title: Programming with ggplot2
140142
contents:

0 commit comments

Comments
 (0)