Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 133 lines (122 sloc) 3.619 kB
d365c9a @hadley Add lims() to parallel labs()
authored
1 #' Convenience functions to set the axis limits.
8f1a58c @joranE Specify that data are dropped, link to coord_cartesian
joranE authored
2 #'
3 #' Observations not in this range will be dropped completely and
ea3b3ce @jimhester Update news and update documentation
jimhester authored
4 #' not passed to any other layers. If a NA value is substituted for one of the
5 #' limits that limit is automatically calculated.
1244716 @hadley Trim whitespace
authored
6 #'
d365c9a @hadley Add lims() to parallel labs()
authored
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.
1244716 @hadley Trim whitespace
authored
10 #' @seealso For changing x or y axis limits \strong{without} dropping data
8f1a58c @joranE Specify that data are dropped, link to coord_cartesian
joranE authored
11 #' observations, see \code{\link{coord_cartesian}}.
f8ecfe2 @hadley Update limits convenience functions.
authored
12 #' @export
13 #' @examples
72a4d79 @joranE Clean up formatting in examples
joranE authored
14 #' # xlim
f8ecfe2 @hadley Update limits convenience functions.
authored
15 #' xlim(15, 20)
16 #' xlim(20, 15)
17 #' xlim(c(10, 20))
1244716 @hadley Trim whitespace
authored
18 #' xlim("a", "b", "c")
d365c9a @hadley Add lims() to parallel labs()
authored
19 #'
bbfc5be @hrbrmstr finished removing qplot from remaining functions except for the expli…
hrbrmstr authored
20 #' ggplot(mtcars, aes(mpg, wt)) +
21 #' geom_point() +
22 #' xlim(15, 20)
ea3b3ce @jimhester Update news and update documentation
jimhester authored
23 #' # with automatic lower limit
bbfc5be @hrbrmstr finished removing qplot from remaining functions except for the expli…
hrbrmstr authored
24 #' ggplot(mtcars, aes(mpg, wt)) +
25 #' geom_point() +
26 #' xlim(NA, 20)
1244716 @hadley Trim whitespace
authored
27 #'
d365c9a @hadley Add lims() to parallel labs()
authored
28 #' # Change both xlim and ylim
bbfc5be @hrbrmstr finished removing qplot from remaining functions except for the expli…
hrbrmstr authored
29 #' ggplot(mtcars, aes(mpg, wt)) +
30 #' geom_point() +
d365c9a @hadley Add lims() to parallel labs()
authored
31 #' lims(x = c(10, 20), y = c(3, 5))
32 lims <- function(...) {
33 args <- list(...)
34
35 if (any(!has_name(args))) {
36 stop("All arguments must be named", call. = FALSE)
37 }
38
39 Map(limits, args, names(args))
40 }
41
42 #' @export
43 #' @rdname lims
373fcfb @hadley Make xlim and ylim more flexible
authored
44 xlim <- function(...) {
c701b82 @hadley Make xlim and ylim generic
authored
45 limits(c(...), "x")
373fcfb @hadley Make xlim and ylim more flexible
authored
46 }
47
f8ecfe2 @hadley Update limits convenience functions.
authored
48 #' @export
d365c9a @hadley Add lims() to parallel labs()
authored
49 #' @rdname lims
373fcfb @hadley Make xlim and ylim more flexible
authored
50 ylim <- function(...) {
c701b82 @hadley Make xlim and ylim generic
authored
51 limits(c(...), "y")
52 }
53
f8ecfe2 @hadley Update limits convenience functions.
authored
54 #' Generate correct scale type for specified limits
1244716 @hadley Trim whitespace
authored
55 #'
1e28092 @bbolker various doc/comment typos
bbolker authored
56 #' @param limits vector of limits
f8ecfe2 @hadley Update limits convenience functions.
authored
57 #' @param var name of variable
58 #' @keywords internal
59 #' @examples
6f74b6f @hadley Use ::: in limits example
authored
60 #' ggplot2:::limits(c(1, 5), "x")
61 #' ggplot2:::limits(c(5, 1), "x")
62 #' ggplot2:::limits(c("A", "b", "c"), "x")
63 #' ggplot2:::limits(c("A", "b", "c"), "fill")
64 #' ggplot2:::limits(as.Date(c("2008-01-01", "2009-01-01")), "x")
3e7d9b3 @hadley Documentation fixes for R CMD check
authored
65 limits <- function(lims, var) UseMethod("limits")
3c63ff7 @hadley Convert from @S3method to @export
authored
66 #' @export
c701b82 @hadley Make xlim and ylim generic
authored
67 limits.numeric <- function(lims, var) {
68 stopifnot(length(lims) == 2)
6744184 @jimhester Style fixes suggested by winston
jimhester authored
69 if (!any(is.na(lims)) && lims[1] > lims[2]) {
c701b82 @hadley Make xlim and ylim generic
authored
70 trans <- "reverse"
373fcfb @hadley Make xlim and ylim more flexible
authored
71 } else {
c701b82 @hadley Make xlim and ylim generic
authored
72 trans <- "identity"
373fcfb @hadley Make xlim and ylim more flexible
authored
73 }
1244716 @hadley Trim whitespace
authored
74
f8ecfe2 @hadley Update limits convenience functions.
authored
75 make_scale("continuous", var, limits = lims, trans = trans)
76 }
77
78 make_scale <- function(type, var, ...) {
79 scale <- match.fun(paste("scale_", var, "_", type, sep = ""))
1244716 @hadley Trim whitespace
authored
80 scale(...)
c701b82 @hadley Make xlim and ylim generic
authored
81 }
f8ecfe2 @hadley Update limits convenience functions.
authored
82
3c63ff7 @hadley Convert from @S3method to @export
authored
83 #' @export
c701b82 @hadley Make xlim and ylim generic
authored
84 limits.character <- function(lims, var) {
f8ecfe2 @hadley Update limits convenience functions.
authored
85 make_scale("discrete", var, limits = lims)
c701b82 @hadley Make xlim and ylim generic
authored
86 }
3c63ff7 @hadley Convert from @S3method to @export
authored
87 #' @export
c701b82 @hadley Make xlim and ylim generic
authored
88 limits.factor <- function(lims, var) {
f8ecfe2 @hadley Update limits convenience functions.
authored
89 make_scale("discrete", var, limits = as.character(lims))
c701b82 @hadley Make xlim and ylim generic
authored
90 }
3c63ff7 @hadley Convert from @S3method to @export
authored
91 #' @export
c701b82 @hadley Make xlim and ylim generic
authored
92 limits.Date <- function(lims, var) {
93 stopifnot(length(lims) == 2)
f8ecfe2 @hadley Update limits convenience functions.
authored
94 make_scale("date", var, limits = lims)
c701b82 @hadley Make xlim and ylim generic
authored
95 }
3c63ff7 @hadley Convert from @S3method to @export
authored
96 #' @export
c701b82 @hadley Make xlim and ylim generic
authored
97 limits.POSIXct <- function(lims, var) {
98 stopifnot(length(lims) == 2)
f8ecfe2 @hadley Update limits convenience functions.
authored
99 make_scale("datetime", var, limits = lims)
c701b82 @hadley Make xlim and ylim generic
authored
100 }
3c63ff7 @hadley Convert from @S3method to @export
authored
101 #' @export
c701b82 @hadley Make xlim and ylim generic
authored
102 limits.POSIXlt <- function(lims, var) {
103 stopifnot(length(lims) == 2)
f8ecfe2 @hadley Update limits convenience functions.
authored
104 make_scale("datetime", var, limits = as.POSIXct(lims))
8bca15f @hadley set_limits() - makes it possible to force inclusion of values in scal…
authored
105 }
106
f8ecfe2 @hadley Update limits convenience functions.
authored
107 #' Expand the plot limits with data.
108 #'
109 #. Sometimes you may want to ensure limits include a single value, for all
110 #' panels or all plots. This function is a thin wrapper around
111 #' \code{\link{geom_blank}} that makes it easy to add such values.
1244716 @hadley Trim whitespace
authored
112 #'
113 #' @param ... named list of aesthetics specifying the value (or values) that
a0394ef @hadley Document missing arguments for functions a-e*
authored
114 #' should be included in each scale.
f8ecfe2 @hadley Update limits convenience functions.
authored
115 #' @export
116 #' @examples
bbfc5be @hrbrmstr finished removing qplot from remaining functions except for the expli…
hrbrmstr authored
117 #' p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
f8ecfe2 @hadley Update limits convenience functions.
authored
118 #' p + expand_limits(x = 0)
119 #' p + expand_limits(y = c(1, 9))
120 #' p + expand_limits(x = 0, y = 0)
121 #'
bbfc5be @hrbrmstr finished removing qplot from remaining functions except for the expli…
hrbrmstr authored
122 #' ggplot(mtcars, aes(mpg, wt)) +
123 #' geom_point(aes(colour = cyl)) +
124 #' expand_limits(colour = seq(2, 10, by = 2))
125 #' ggplot(mtcars, aes(mpg, wt)) +
126 #' geom_point(aes(colour = factor(cyl))) +
127 #' expand_limits(colour = factor(seq(2, 10, by = 2)))
8bca15f @hadley set_limits() - makes it possible to force inclusion of values in scal…
authored
128 expand_limits <- function(...) {
4791dd2 @hadley Use stringsAsFactors in case of discrete limits
authored
129 data <- data.frame(..., stringsAsFactors = FALSE)
1244716 @hadley Trim whitespace
authored
130
c2c9417 @hadley geom_blank defaults to inherit.aes = FALSE
authored
131 geom_blank(aes_all(names(data)), data)
4bcc38a @hadley Add missing newlines
authored
132 }
Something went wrong with that request. Please try again.