Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 73 lines (68 sloc) 1.874 kb
953fe47 @hadley Convert all documentation to roxygen
authored
1 #' Update axis/legend labels
1244716 @hadley Trim whitespace
authored
2 #'
953fe47 @hadley Convert all documentation to roxygen
authored
3 #' @param p plot to modify
4 #' @param labels named list of new labels
ef29cfa @hadley Export update_labels
authored
5 #' @export
953fe47 @hadley Convert all documentation to roxygen
authored
6 #' @examples
7 #' p <- qplot(mpg, wt, data = mtcars)
8 #' update_labels(p, list(x = "New x"))
9 #' update_labels(p, list(x = expression(x / y ^ 2)))
10 #' update_labels(p, list(x = "New x", y = "New Y"))
11 #' update_labels(p, list(colour = "Fail silently"))
cc278ae @hadley New labels convenience functions
authored
12 update_labels <- function(p, labels) {
13 p <- plot_clone(p)
15c48f5 @wch ove plot$options$labels to plot$labels
wch authored
14 p$labels <- defaults(labels, p$labels)
15 p
cc278ae @hadley New labels convenience functions
authored
16 }
17
953fe47 @hadley Convert all documentation to roxygen
authored
18 #' Change axis labels and legend titles
1244716 @hadley Trim whitespace
authored
19 #'
1eb435e @wch Doc fixes for R CMD check
wch authored
20 #' @param label The text for the axis or plot title.
953fe47 @hadley Convert all documentation to roxygen
authored
21 #' @param ... a list of new names in the form aesthetic = "new name"
7cbd6c2 @wch Add bad export for labs()
wch authored
22 #' @export
953fe47 @hadley Convert all documentation to roxygen
authored
23 #' @examples
24 #' p <- qplot(mpg, wt, data = mtcars)
4f5b5f6 @wch Change theme(title=...) to labs(title=...)
wch authored
25 #' p + labs(title = "New plot title")
953fe47 @hadley Convert all documentation to roxygen
authored
26 #' p + labs(x = "New x label")
27 #' p + xlab("New x label")
28 #' p + ylab("New y label")
4f5b5f6 @wch Change theme(title=...) to labs(title=...)
wch authored
29 #' p + ggtitle("New plot title")
953fe47 @hadley Convert all documentation to roxygen
authored
30 #'
1244716 @hadley Trim whitespace
authored
31 #' # This should work indepdendently of other functions that modify the
953fe47 @hadley Convert all documentation to roxygen
authored
32 #' # the scale names
33 #' p + ylab("New y label") + ylim(2, 4)
34 #' p + ylim(2, 4) + ylab("New y label")
35 #'
36 #' # The labs function also modifies legend labels
37 #' p <- qplot(mpg, wt, data = mtcars, colour = cyl)
38 #' p + labs(colour = "Cylinders")
39 #'
40 #' # Can also pass in a list, if that is more convenient
1244716 @hadley Trim whitespace
authored
41 #' p + labs(list(title = "Title", x = "X", y = "Y"))
cc278ae @hadley New labels convenience functions
authored
42 labs <- function(...) {
43 args <- list(...)
44 if (is.list(args[[1]])) args <- args[[1]]
cc6e7b2 @wch American spellings work with labs()
wch authored
45 args <- rename_aes(args)
cc278ae @hadley New labels convenience functions
authored
46 structure(args, class = "labels")
47 }
48
abfcd2f @hadley Documentation improvements for website
authored
49 #' @rdname labs
50 #' @export
cc278ae @hadley New labels convenience functions
authored
51 xlab <- function(label) {
52 labs(x = label)
53 }
abfcd2f @hadley Documentation improvements for website
authored
54 #' @rdname labs
55 #' @export
cc278ae @hadley New labels convenience functions
authored
56 ylab <- function(label) {
57 labs(y = label)
2d558d4 @hadley Better conversion from aesthetics to lables
authored
58 }
4f5b5f6 @wch Change theme(title=...) to labs(title=...)
wch authored
59 #' @rdname labs
60 #' @export
61 ggtitle <- function(label) {
62 labs(title = label)
63 }
2d558d4 @hadley Better conversion from aesthetics to lables
authored
64
65 # Convert aesthetic mapping into text labels
66 make_labels <- function(mapping) {
67 remove_dots <- function(x) {
5871957 @hadley Better strategy for detecting calculated aes.
authored
68 gsub(match_calculated_aes, "\\1", x)
2d558d4 @hadley Better conversion from aesthetics to lables
authored
69 }
1244716 @hadley Trim whitespace
authored
70
2d558d4 @hadley Better conversion from aesthetics to lables
authored
71 lapply(mapping, function(x) remove_dots(deparse(x)))
4bcc38a @hadley Add missing newlines
authored
72 }
Something went wrong with that request. Please try again.