Skip to content

Commit 3d3892e

Browse files
committed
Doc tweaks for scales
1 parent 2d5f6d3 commit 3d3892e

37 files changed

+387
-358
lines changed

R/geom-linerange.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' @seealso
1313
#' \code{\link{stat_summary}} for examples of these guys in use,
1414
#' \code{\link{geom_smooth}} for continuous analog,
15-
#' \code{\link{geom_errorh}} for a horizontal error bar.
15+
#' \code{\link{geom_errorbarh}} for a horizontal error bar.
1616
#' @export
1717
#' @inheritParams layer
1818
#' @inheritParams geom_point

R/guide-colorbar.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Continuous colour bar guide.
1+
#' Continuous colour bar guide
22
#'
33
#' Colour bar guide shows continuous color scales mapped onto values.
44
#' Colour bar is available with \code{scale_fill} and \code{scale_colour}.

R/labels.r

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#'
33
#' @param p plot to modify
44
#' @param labels named list of new labels
5+
#' @keywords internal
56
#' @export
67
#' @examples
78
#' p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
@@ -15,42 +16,37 @@ update_labels <- function(p, labels) {
1516
p
1617
}
1718

18-
#' Change axis labels, legend titles, plot title/subtitle and below-plot
19-
#' caption.
19+
#' Modify axis, legend, and plot labels
20+
#'
21+
#' Good labels are critical for making your plots accessible to a wider
22+
#' audience. Ensure the axis and legend labels display the full variable name.
23+
#' Use the plot \code{title} and \code{subtitle} to explain the main findings.
24+
#' It's common to use the \code{caption} to provide information about the
25+
#' data source.
26+
#'
27+
#' You can also set axis and legend labels in the individual scales (using
28+
#' the first argument, the \code{name}. I recommend doing that if you're
29+
#' changing other scale options.
2030
#'
2131
#' @param label The text for the axis, plot title or caption below the plot.
2232
#' @param subtitle the text for the subtitle for the plot which will be
2333
#' displayed below the title. Leave \code{NULL} for no subtitle.
24-
#' @param ... a list of new names in the form aesthetic = "new name"
34+
#' @param ... A list of new name-value pairs. The name should either be
35+
#' an aesthetic, or one of "title", "subtitle", or "caption".
2536
#' @export
2637
#' @examples
27-
#' p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
28-
#' p + labs(title = "New plot title")
38+
#' p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
39+
#' p + labs(colour = "Cylinders")
2940
#' p + labs(x = "New x label")
30-
#' p + xlab("New x label")
31-
#' p + ylab("New y label")
32-
#' p + ggtitle("New plot title")
3341
#'
34-
#' # Can add a subtitle to plots with either of the following
35-
#' p + ggtitle("New plot title", subtitle = "A subtitle")
42+
#' # The plot title appears at the top-left, with the subtitle
43+
#' # display in smaller text underneath it
44+
#' p + labs(title = "New plot title")
3645
#' p + labs(title = "New plot title", subtitle = "A subtitle")
3746
#'
38-
#' # Can add a plot caption underneath the whole plot (for sources, notes or
39-
#' # copyright), similar to the \code{sub} parameter in base R, with the
40-
#' # following
47+
#' # The caption appears in the bottom-right, and is often used for
48+
#' # sources, notes or copyright
4149
#' p + labs(caption = "(based on data from ...)")
42-
#'
43-
#' # This should work independently of other functions that modify the
44-
#' # the scale names
45-
#' p + ylab("New y label") + ylim(2, 4)
46-
#' p + ylim(2, 4) + ylab("New y label")
47-
#'
48-
#' # The labs function also modifies legend labels
49-
#' p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
50-
#' p + labs(colour = "Cylinders")
51-
#'
52-
#' # Can also pass in a list, if that is more convenient
53-
#' p + labs(list(title = "Title", subtitle = "Subtitle", x = "X", y = "Y"))
5450
labs <- function(...) {
5551
args <- list(...)
5652
if (is.list(args[[1]])) args <- args[[1]]

R/limits.r

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
1-
#' Convenience functions to set the axis limits.
1+
#' Set scale limits
22
#'
3-
#' Observations not in this range will be dropped completely and
4-
#' not passed to any other layers. If a NA value is substituted for one of the
5-
#' limits that limit is automatically calculated.
3+
#' This is a shortcut for supplying the \code{limits} argument to the
4+
#' individual scales. Note that, by default, any values outside the limits
5+
#' will be replaced with \code{NA}.
66
#'
7-
#' @param ... If numeric, will create a continuous scale, if factor or
8-
#' character, will create a discrete scale. For \code{lims}, every
9-
#' argument must be named.
7+
#' @param ... A name-value pair. The name must be an aesthetic, and the value
8+
#' must be either a length-2 numeric, a character, a factor, or a date/time.
9+
#'
10+
#' A numeric value will create a continuous scale. If the larger value
11+
#' comes first, the scale will be reversed. You can leave one value as
12+
#' \code{NA} to compute from the range of the data.
13+
#'
14+
#' A character or factor value will create a discrete scale.
15+
#'
16+
#' A date-time value will create a continuous date/time scale.
1017
#' @seealso For changing x or y axis limits \strong{without} dropping data
11-
#' observations, see \code{\link{coord_cartesian}}.
18+
#' observations, see \code{\link{coord_cartesian}}. To expand the range of
19+
#' a plot to always include certain values, see \code{\link{expand_limits}}.
1220
#' @export
1321
#' @examples
14-
#' # xlim
15-
#' xlim(15, 20)
16-
#' xlim(20, 15)
17-
#' xlim(c(10, 20))
18-
#' xlim("a", "b", "c")
19-
#'
22+
#' # Zoom into a specified area
2023
#' ggplot(mtcars, aes(mpg, wt)) +
2124
#' geom_point() +
2225
#' xlim(15, 20)
26+
#'
27+
#' # reverse scale
28+
#' ggplot(mtcars, aes(mpg, wt)) +
29+
#' geom_point() +
30+
#' xlim(20, 15)
31+
#'
2332
#' # with automatic lower limit
2433
#' ggplot(mtcars, aes(mpg, wt)) +
2534
#' geom_point() +
2635
#' xlim(NA, 20)
2736
#'
28-
#' # Change both xlim and ylim
29-
#' ggplot(mtcars, aes(mpg, wt)) +
37+
#' # You can also supply limits that are larger than the data.
38+
#' # This is useful if you want to match scales across different plots
39+
#' small <- subset(mtcars, cyl == 4)
40+
#' big <- subset(mtcars, cyl > 4)
41+
#'
42+
#' ggplot(small, aes(mpg, wt, colour = factor(cyl))) +
43+
#' geom_point() +
44+
#' lims(colour = c("4", "6", "8"))
45+
#'
46+
#' ggplot(big, aes(mpg, wt, colour = factor(cyl))) +
3047
#' geom_point() +
31-
#' lims(x = c(10, 20), y = c(3, 5))
48+
#' lims(colour = c("4", "6", "8"))
3249
lims <- function(...) {
3350
args <- list(...)
3451

@@ -104,9 +121,9 @@ limits.POSIXlt <- function(lims, var) {
104121
make_scale("datetime", var, limits = as.POSIXct(lims))
105122
}
106123

107-
#' Expand the plot limits with data.
124+
#' Expand the plot limits, using data
108125
#'
109-
#. Sometimes you may want to ensure limits include a single value, for all
126+
#' Sometimes you may want to ensure limits include a single value, for all
110127
#' panels or all plots. This function is a thin wrapper around
111128
#' \code{\link{geom_blank}} that makes it easy to add such values.
112129
#'

R/scale-alpha.r

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
#' Alpha scales.
1+
#' Alpha transparency scales
22
#'
3+
#' Alpha-transparency scales are not tremendously useful, but can be a
4+
#' convenient way to visually down-weight less important observations.
35
#' \code{scale_alpha} is an alias for \code{scale_alpha_continuous} since
46
#' that is the most common use of alpha, and it saves a bit of typing.
57
#'
68
#' @param ... Other arguments passed on to \code{\link{continuous_scale}}
79
#' or \code{\link{discrete_scale}} as appropriate, to control name, limits,
810
#' breaks, labels and so forth.
9-
#' @param range range of output alpha values. Should lie between 0 and 1.
11+
#' @param range Output range of alpha values. Must lie between 0 and 1.
12+
#' @family colour scales
1013
#' @export
1114
#' @examples
12-
#' (p <- ggplot(mtcars, aes(mpg, cyl)) +
13-
#' geom_point(aes(alpha = cyl)))
14-
#' p + scale_alpha("cylinders")
15-
#' p + scale_alpha("number\nof\ncylinders")
15+
#' p <- ggplot(mpg, aes(displ, hwy)) +
16+
#' geom_point(aes(alpha = year))
1617
#'
18+
#' p
19+
#' p + scale_alpha("cylinders")
1720
#' p + scale_alpha(range = c(0.4, 0.8))
18-
#'
19-
#' (p <- ggplot(mtcars, aes(mpg, cyl)) +
20-
#' geom_point(aes(alpha = factor(cyl))))
21-
#' p + scale_alpha_discrete(range = c(0.4, 0.8))
2221
scale_alpha <- function(..., range = c(0.1, 1)) {
2322
continuous_scale("alpha", "alpha_c", rescale_pal(range), ...)
2423
}

R/scale-brewer.r

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#' Sequential, diverging and qualitative colour scales from colorbrewer.org
22
#'
3-
#' ColorBrewer provides sequential, diverging and qualitative colour schemes
4-
#' which are particularly suited and tested to display discrete values (levels
5-
#' of a factor) on a map. ggplot2 can use those colours in discrete scales. It
6-
#' also allows to smoothly interpolate 6 colours from any palette to a
7-
#' continuous scale (6 colours per palette gives nice gradients; more results in
8-
#' more saturated colours which do not look as good). However, the original
9-
#' colour schemes (particularly the qualitative ones) were not intended for this
10-
#' and the perceptual result is left to the appreciation of the user.
11-
#' See \url{http://colorbrewer2.org} for more information.
3+
#' @description
4+
#' The \code{brewer} scales provides sequential, diverging and qualitative
5+
#' colour schemes from ColorBrewer. These are particularly well suited to
6+
#' display discrete values on a map. See \url{http://colorbrewer2.org} for
7+
#' more information.
8+
#'
9+
#' @note
10+
#' The \code{distiller} scales extends brewer to continuous scales by smoothly
11+
#' interpolate 6 colours from any palette to a continuous scale.
12+
#'
13+
#' @details
14+
#' The \code{brewer} scales were carefully designed and tested on discrete data.
15+
#' They were not designed to be extended to continuous data, but results often
16+
#' look good. Your mileage may vary.
1217
#'
1318
#' @section Palettes:
1419
#' The following palettes are available for use with these scales:
@@ -23,19 +28,16 @@
2328
#' @inheritParams scale_colour_hue
2429
#' @inheritParams scale_colour_gradient
2530
#' @inheritParams scales::gradient_n_pal
26-
#' @seealso Other colour scales:
27-
#' \code{\link{scale_colour_gradient}},
28-
#' \code{\link{scale_colour_grey}},
29-
#' \code{\link{scale_colour_hue}}
31+
#' @family colour scales
3032
#' @rdname scale_brewer
3133
#' @export
3234
#' @examples
3335
#' dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
3436
#' (d <- ggplot(dsamp, aes(carat, price)) +
3537
#' geom_point(aes(colour = clarity)))
38+
#' d + scale_colour_brewer()
3639
#'
3740
#' # Change scale label
38-
#' d + scale_colour_brewer()
3941
#' d + scale_colour_brewer("Diamond\nclarity")
4042
#'
4143
#' # Select brewer palette to use, see ?scales::brewer_pal for more details

R/scale-continuous.r

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,70 @@
1-
#' Continuous position scales (x & y).
1+
#' Position scales for continuous data (x & y)
22
#'
3-
#' \code{scale_x_continuous} and \code{scale_y_continuous} are the key functions.
4-
#' The others, \code{scale_x_log10}, \code{scale_y_sqrt} etc, are aliases
5-
#' that set the \code{trans} argument to commonly used transformations.
3+
#' \code{scale_x_continuous} and \code{scale_y_continuous} are the default
4+
#' scales for continuous x and y aesthetics. There are three variants
5+
#' that set the \code{trans} argument for commonly used transformations:
6+
#' \code{scale_*_log10}, \code{scale_*_sqrt} and \code{scale_*_reverse}.
7+
#'
8+
#' For simple manipulation of labels and limits, you may wish to use
9+
#' \code{\link{labs}()} and \code{\link{lims}()} instead.
610
#'
711
#' @inheritParams continuous_scale
8-
#' @seealso \code{\link{scale_date}} for date/time position scales.
12+
#' @family position scales
913
#' @param ... Other arguments passed on to \code{scale_(x|y)_continuous}
1014
#' @examples
11-
#' \donttest{
12-
#' if (require(ggplot2movies)) {
13-
#' m <- ggplot(subset(movies, votes > 1000), aes(rating, votes)) +
14-
#' geom_point(na.rm = TRUE)
15-
#' m
15+
#' p1 <- ggplot(mpg, aes(displ, hwy)) +
16+
#' geom_point()
17+
#' p1
1618
#'
1719
#' # Manipulating the default position scales lets you:
18-
#'
1920
#' # * change the axis labels
20-
#' m + scale_y_continuous("number of votes")
21-
#' m + scale_y_continuous(quote(votes ^ alpha))
21+
#' p1 +
22+
#' scale_x_continuous("Engine displacement (L)") +
23+
#' scale_y_continuous("Highway MPG")
24+
#'
25+
#' # You can also use the short-cut labs().
26+
#' # Use NULL to suppress axis labels
27+
#' p1 + labs(x = NULL, y = NULL)
2228
#'
2329
#' # * modify the axis limits
24-
#' m + scale_y_continuous(limits = c(0, 5000))
25-
#' m + scale_y_continuous(limits = c(1000, 10000))
26-
#' m + scale_x_continuous(limits = c(7, 8))
30+
#' p1 + scale_x_continuous(limits = c(2, 6))
31+
#' p1 + scale_x_continuous(limits = c(0, 10))
2732
#'
28-
#' # you can also use the short hand functions xlim and ylim
29-
#' m + ylim(0, 5000)
30-
#' m + ylim(1000, 10000)
31-
#' m + xlim(7, 8)
33+
#' # you can also use the short hand functions `xlim()` and `ylim()`
34+
#' p1 + xlim(2, 6)
3235
#'
3336
#' # * choose where the ticks appear
34-
#' m + scale_x_continuous(breaks = 1:10)
35-
#' m + scale_x_continuous(breaks = c(1,3,7,9))
36-
#'
37-
#' # * manually label the ticks
38-
#' m + scale_x_continuous(breaks = c(2,5,8), labels = c("two", "five", "eight"))
39-
#' m + scale_x_continuous(breaks = c(2,5,8), labels = c("horrible", "ok", "awesome"))
40-
#' m + scale_x_continuous(breaks = c(2,5,8), labels = expression(Alpha, Beta, Omega))
37+
#' p1 + scale_x_continuous(breaks = c(2, 4, 6))
4138
#'
42-
#' # There are a few built in transformation that you can use:
43-
#' m + scale_y_log10()
44-
#' m + scale_y_sqrt()
45-
#' m + scale_y_reverse()
46-
#' # You can also create your own and supply them to the trans argument.
47-
#' # See ?scales::trans_new
39+
#' # * add what labels they have
40+
#' p1 + scale_x_continuous(
41+
#' breaks = c(2, 4, 6),
42+
#' label = c("two", "four", "six")
43+
#' )
4844
#'
49-
#' # You can control the formatting of the labels with the formatter
50-
#' # argument. Some common formats are built into the scales package:
45+
#' # Typically you'll pass a function to the `labels` argument.
46+
#' # Some common formats are built into the scales package:
5147
#' df <- data.frame(
5248
#' x = rnorm(10) * 100000,
5349
#' y = seq(0, 1, length.out = 10)
5450
#' )
55-
#' p <- ggplot(df, aes(x, y)) + geom_point()
56-
#' p + scale_y_continuous(labels = scales::percent)
57-
#' p + scale_y_continuous(labels = scales::dollar)
58-
#' p + scale_x_continuous(labels = scales::comma)
51+
#' p2 <- ggplot(df, aes(x, y)) + geom_point()
52+
#' p2 + scale_y_continuous(labels = scales::percent)
53+
#' p2 + scale_y_continuous(labels = scales::dollar)
54+
#' p2 + scale_x_continuous(labels = scales::comma)
55+
#'
56+
#' # You can also override the default linear mapping by using a
57+
#' # transformation. There are three shortcuts:
58+
#' p1 + scale_y_log10()
59+
#' p1 + scale_y_sqrt()
60+
#' p1 + scale_y_reverse()
61+
#'
62+
#' # Or you can supply a transformation in the `trans` argument:
63+
#' p1 + scale_y_continuous(trans = scales::reciprocal_trans())
5964
#'
60-
#' # Other shortcut functions
61-
#' ggplot(movies, aes(rating, votes)) +
62-
#' geom_point() +
63-
#' ylim(1e4, 5e4)
64-
#' # * axis labels
65-
#' ggplot(movies, aes(rating, votes)) +
66-
#' geom_point() +
67-
#' labs(x = "My x axis", y = "My y axis")
68-
#' # * log scaling
69-
#' ggplot(movies, aes(rating, votes)) +
70-
#' geom_point() +
71-
#' scale_x_log10() +
72-
#' scale_y_log10()
73-
#' }
74-
#' }
65+
#' # You can also create your own. See ?scales::trans_new
7566
#' @name scale_continuous
67+
#' @aliases NULL
7668
NULL
7769

7870
#' @rdname scale_continuous

0 commit comments

Comments
 (0)