Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 121 lines (115 sloc) 4.087 kb
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
1 #' Continuous position scales (x & y).
1244716 @hadley Trim whitespace
authored
2 #'
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
3 #' @param ... common continuous scale parameters: \code{name}, \code{breaks},
4 #' \code{labels}, \code{na.value}, \code{limits} and \code{trans}. See
5 #' \code{\link{continuous_scale}} for more details
1244716 @hadley Trim whitespace
authored
6 #' @param expand a numeric vector of length two giving multiplicative and
7 #' additive expansion constants. These constants ensure that the data is
07e5b81 @hadley Doc tweaks for continuous position scales
authored
8 #' placed some distance away from the axes.
7269691 @hadley Use new roxygen features
authored
9 #' @family position scales
e82e737 @hadley Use new roxygen features to better document scales
authored
10 #' @rdname scale_continuous
11 #' @export
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
12 #' @examples
c5b872e @hadley Don't test any example that takes over 5 seconds
authored
13 #' \donttest{
1244716 @hadley Trim whitespace
authored
14 #' (m <- qplot(rating, votes, data=subset(movies, votes > 1000),
16c0e55 @hadley More example fixes
authored
15 #' na.rm = TRUE))
1244716 @hadley Trim whitespace
authored
16 #'
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
17 #' # Manipulating the default position scales lets you:
18 #'
19 #' # * change the axis labels
20 #' m + scale_y_continuous("number of votes")
21 #' m + scale_y_continuous(expression(votes^alpha))
1244716 @hadley Trim whitespace
authored
22 #'
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
23 #' # * 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))
1244716 @hadley Trim whitespace
authored
27 #'
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
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)
32 #'
33 #' # * 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))
1244716 @hadley Trim whitespace
authored
41 #'
f81764b @hadley Improve trans documentation
authored
42 #' # There are a few built in transformation that you can use:
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
43 #' m + scale_y_log10()
44 #' m + scale_y_sqrt()
45 #' m + scale_y_reverse()
5f17e5b @BrianDiggs you -> your
BrianDiggs authored
46 #' # You can also create your own and supply them to the trans argument.
f81764b @hadley Improve trans documentation
authored
47 #' # See ?scale::trans_new
1244716 @hadley Trim whitespace
authored
48 #'
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
49 #' # You can control the formatting of the labels with the formatter
36ae141 @hadley Load scales package where needed
authored
50 #' # argument. Some common formats are built into the scales package:
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
51 #' x <- rnorm(10) * 100000
52 #' y <- seq(0, 1, length = 10)
53 #' p <- qplot(x, y)
36ae141 @hadley Load scales package where needed
authored
54 #' library(scales)
f330d00 @hadley Fixes to examples
authored
55 #' p + scale_y_continuous(labels = percent)
56 #' p + scale_y_continuous(labels = dollar)
57 #' p + scale_x_continuous(labels = comma)
1244716 @hadley Trim whitespace
authored
58 #'
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
59 #' # qplot allows you to do some of this with a little less typing:
60 #' # * axis limits
61 #' qplot(rating, votes, data=movies, ylim=c(1e4, 5e4))
62 #' # * axis labels
63 #' qplot(rating, votes, data=movies, xlab="My x axis", ylab="My y axis")
64 #' # * log scaling
65 #' qplot(rating, votes, data=movies, log="xy")
c5b872e @hadley Don't test any example that takes over 5 seconds
authored
66 #' }
8f040a9 @wch Change default expand to waiver() and fix polar spacing
wch authored
67 scale_x_continuous <- function(..., expand = waiver()) {
51381dc @BrianDiggs Add xintercept and yintercept to x and y scales
BrianDiggs authored
68 continuous_scale(c("x", "xmin", "xmax", "xend", "xintercept"), "position_c", identity,
5c31ba0 @kohske add warning if legend param is used in scale_XXX
kohske authored
69 ..., expand = expand, guide = "none")
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
70 }
71
e82e737 @hadley Use new roxygen features to better document scales
authored
72 #' @rdname scale_continuous
73 #' @export
8f040a9 @wch Change default expand to waiver() and fix polar spacing
wch authored
74 scale_y_continuous <- function(..., expand = waiver()) {
ba1a091 @kohske quick fix of incorrect automatic range for geom_box (#274)
kohske authored
75 continuous_scale(c("y", "ymin", "ymax", "yend", "yintercept", "ymin_final", "ymax_final"), "position_c", identity,
5c31ba0 @kohske add warning if legend param is used in scale_XXX
kohske authored
76 ..., expand = expand, guide = "none")
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
77 }
78
79
80 # Position aesthetics don't map, because the coordinate system takes
c423023 @hadley Ensure continuous scales make data numeric
authored
81 # care of it. But they do need to be made in to doubles, so stat methods
82 # can tell the difference between continuous and discrete data.
3c63ff7 @hadley Convert from @S3method to @export
authored
83 #' @export
19da6a8 @kohske accept ooh breaks (only by coord_cartesian), refs: #430, #422, #416,…
kohske authored
84 scale_map.position_c <- function(scale, x, limits = scale_limits(scale)) {
c718e2d @hadley Use na.value in scale_continuous.
authored
85 scaled <- as.numeric(scale$oob(x, limits))
86 ifelse(!is.na(scaled), scaled, scale$na.value)
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
87 }
88
e82e737 @hadley Use new roxygen features to better document scales
authored
89 # Transformed scales ---------------------------------------------------------
90
91 #' @rdname scale_continuous
92 #' @export
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
93 scale_x_log10 <- function(...) {
94 scale_x_continuous(..., trans = log10_trans())
95 }
e82e737 @hadley Use new roxygen features to better document scales
authored
96 #' @rdname scale_continuous
97 #' @export
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
98 scale_y_log10 <- function(...) {
99 scale_y_continuous(..., trans = log10_trans())
100 }
e82e737 @hadley Use new roxygen features to better document scales
authored
101 #' @rdname scale_continuous
102 #' @export
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
103 scale_x_reverse <- function(...) {
104 scale_x_continuous(..., trans = reverse_trans())
105 }
e82e737 @hadley Use new roxygen features to better document scales
authored
106 #' @rdname scale_continuous
107 #' @export
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
108 scale_y_reverse <- function(...) {
109 scale_y_continuous(..., trans = reverse_trans())
110 }
e82e737 @hadley Use new roxygen features to better document scales
authored
111 #' @rdname scale_continuous
112 #' @export
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
113 scale_x_sqrt <- function(...) {
114 scale_x_continuous(..., trans = sqrt_trans())
115 }
e82e737 @hadley Use new roxygen features to better document scales
authored
116 #' @rdname scale_continuous
117 #' @export
b7d7bfc @hadley Major revamp of scales to use new scales package.
authored
118 scale_y_sqrt <- function(...) {
119 scale_y_continuous(..., trans = sqrt_trans())
120 }
Something went wrong with that request. Please try again.