Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 197 lines (178 sloc) 6.52 kb
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
1 #' Label facets with their value.
2 #' This is the default labelling scheme.
1244716 Hadley Wickham Trim whitespace
authored
3 #'
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
4 #' @param variable variable name passed in by facetter
5 #' @param value variable value passed in by facetter
9d3d8e8 Hadley Wickham Better documentation for facets
authored
6 #' @family facet labellers
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
7 #' @export
8 #' @examples
9 #' p <- qplot(wt, mpg, data = mtcars)
fc462e9 Hadley Wickham Documentation bug fixes
authored
10 #' p + facet_grid(. ~ cyl)
11 #' p + facet_grid(. ~ cyl, labeller = label_value)
b107a4f Hadley Wickham Labels might now be factors - make sure to coerce to character
authored
12 label_value <- function(variable, value) as.character(value)
3d9925a Hadley Wickham Documentation improvements
authored
13
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
14 #' Label facets with value and variable.
1244716 Hadley Wickham Trim whitespace
authored
15 #'
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
16 #' @param variable variable name passed in by facetter
17 #' @param value variable value passed in by facetter
9d3d8e8 Hadley Wickham Better documentation for facets
authored
18 #' @family facet labellers
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
19 #' @export
20 #' @examples
21 #' p <- qplot(wt, mpg, data = mtcars)
fc462e9 Hadley Wickham Documentation bug fixes
authored
22 #' p + facet_grid(. ~ cyl)
23 #' p + facet_grid(. ~ cyl, labeller = label_both)
063c816 Hadley Wickham More tools to display expressions in facet labels
authored
24 label_both <- function(variable, value) paste(variable, value, sep = ": ")
25
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
26 #' Label facets with parsed label.
1244716 Hadley Wickham Trim whitespace
authored
27 #'
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
28 #' @seealso \code{\link{plotmath}}
29 #' @param variable variable name passed in by facetter
30 #' @param value variable value passed in by facetter
9d3d8e8 Hadley Wickham Better documentation for facets
authored
31 #' @family facet labellers
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
32 #' @export
33 #' @examples
34 #' mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "gamma"))
35 #' qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2)
1244716 Hadley Wickham Trim whitespace
authored
36 #' qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2,
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
37 #' labeller = label_parsed)
f90c778 Hadley Wickham Create some examples and make label_parsed work
authored
38 label_parsed <- function(variable, value) {
e79c075 Hadley Wickham Import plyr into namespace
authored
39 llply(as.character(value), function(x) parse(text = x))
f90c778 Hadley Wickham Create some examples and make label_parsed work
authored
40 }
41
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
42 #' Label facet with 'bquoted' expressions
1244716 Hadley Wickham Trim whitespace
authored
43 #'
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
44 #' See \code{\link{bquote}} for details on the syntax of the argument. The
1244716 Hadley Wickham Trim whitespace
authored
45 #' label value is x.
46 #'
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
47 #' @param expr labelling expression to use
9d3d8e8 Hadley Wickham Better documentation for facets
authored
48 #' @family facet labellers
953fe47 Hadley Wickham Convert all documentation to roxygen
authored
49 #' @seealso \code{\link{plotmath}}
50 #' @export
51 #' @examples
52 #' p <- qplot(wt, mpg, data = mtcars)
fc462e9 Hadley Wickham Documentation bug fixes
authored
53 #' p + facet_grid(. ~ vs, labeller = label_bquote(alpha ^ .(x)))
54 #' p + facet_grid(. ~ vs, labeller = label_bquote(.(x) ^ .(x)))
063c816 Hadley Wickham More tools to display expressions in facet labels
authored
55 label_bquote <- function(expr = beta ^ .(x)) {
56 quoted <- substitute(expr)
1244716 Hadley Wickham Trim whitespace
authored
57
fb24811 Hadley Wickham Cast value to character in label_bquote
authored
58 function(variable, value) {
59 value <- as.character(value)
063c816 Hadley Wickham More tools to display expressions in facet labels
authored
60 lapply(value, function(x)
61 eval(substitute(bquote(expr, list(x = x)), list(expr = quoted))))
fb24811 Hadley Wickham Cast value to character in label_bquote
authored
62 }
063c816 Hadley Wickham More tools to display expressions in facet labels
authored
63 }
efcc69e Hadley Wickham Supress globalVar note
authored
64 globalVariables("x")
65
063c816 Hadley Wickham More tools to display expressions in facet labels
authored
66
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
67 #' Label facets with a word wrapped label.
efcc69e Hadley Wickham Supress globalVar note
authored
68 #'
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
69 #' Uses \code{\link[base]{strwrap}} for line wrapping.
70 #' @param width integer, target column width for output.
71 #' @export
72 #' @seealso , \code{\link{labeller}}
73 label_wrap_gen <- function(width = 25) {
74 function(variable, values) {
efcc69e Hadley Wickham Supress globalVar note
authored
75 vapply(strwrap(as.character(values), width = width, simplify = FALSE),
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
76 paste, vector('character', 1), collapse = "\n")
77 }
78 }
79
80 #' Generic labeller function for facets
efcc69e Hadley Wickham Supress globalVar note
authored
81 #'
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
82 #' One-step function for providing methods or named character vectors
83 #' for displaying labels in facets.
efcc69e Hadley Wickham Supress globalVar note
authored
84 #'
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
85 #' The provided methods are checked for number of arguments.
efcc69e Hadley Wickham Supress globalVar note
authored
86 #' If the provided method takes less than two
87 #' (e.g. \code{\link[Hmisc]{capitalize}}),
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
88 #' the method is passed \code{values}.
efcc69e Hadley Wickham Supress globalVar note
authored
89 #' Else (e.g. \code{\link{label_both}}),
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
90 #' it is passed \code{variable} and \code{values} (in that order).
91 #' If you want to be certain, use e.g. an anonymous function.
efcc69e Hadley Wickham Supress globalVar note
authored
92 #' If errors are returned such as ``argument ".." is missing, with no default''
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
93 #' or ``unused argument (variable)'', matching the method's arguments does not
94 #' work as expected; make a wrapper function.
95 #'
efcc69e Hadley Wickham Supress globalVar note
authored
96 #'
97 #' @param ... Named arguments of the form \code{variable=values},
98 #' where \code{values} could be a vector or method.
99 #' @param keep.as.numeric logical, default TRUE. When FALSE, converts numeric
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
100 #' values supplied as margins to the facet to characters.
101 #' @family facet labeller
efcc69e Hadley Wickham Supress globalVar note
authored
102 #' @return Function to supply to
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
103 #' \code{\link{facet_grid}} for the argument \code{labeller}.
efcc69e Hadley Wickham Supress globalVar note
authored
104 #' @export
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
105 #' @examples
5551e1b Hadley Wickham Fixes for examples
authored
106 #' \donttest{
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
107 #' p1 <- ggplot(mpg, aes(cty, hwy)) + geom_point()
108 #' p1 + facet_grid(cyl ~ class, labeller=label_both)
109 #' p1 + facet_grid(cyl ~ class, labeller=labeller(cyl=label_both))
efcc69e Hadley Wickham Supress globalVar note
authored
110 #'
111 #' ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() +
112 #' facet_grid(vs + am ~ gear, margins=TRUE,
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
113 #' labeller=labeller(vs=label_both, am=label_both))
efcc69e Hadley Wickham Supress globalVar note
authored
114 #'
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
115 #' capitalize <- function(string) {
116 #' substr(string, 1, 1) <- toupper(substr(string, 1, 1))
5551e1b Hadley Wickham Fixes for examples
authored
117 #' string
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
118 #' }
119 #' conservation_status <- c('cd'='Conservation Dependent',
efcc69e Hadley Wickham Supress globalVar note
authored
120 #' 'en'='Endangered',
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
121 #' 'lc'='Least concern',
efcc69e Hadley Wickham Supress globalVar note
authored
122 #' 'nt'='Near Threatened',
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
123 #' 'vu'='Vulnerable',
124 #' 'domesticated'='Domesticated')
efcc69e Hadley Wickham Supress globalVar note
authored
125 #' ## Source: http://en.wikipedia.org/wiki/Wikipedia:Conservation_status
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
126 #'
5551e1b Hadley Wickham Fixes for examples
authored
127 #' p2 <- ggplot(msleep, aes(x=sleep_total, y=awake)) + geom_point()
128 #' p2 + facet_grid(vore ~ conservation, labeller = labeller(vore = capitalize))
efcc69e Hadley Wickham Supress globalVar note
authored
129 #'
130 #' p2 + facet_grid(vore ~ conservation,
5551e1b Hadley Wickham Fixes for examples
authored
131 #' labeller=labeller(vore = capitalize, conservation = conservation_status ))
efcc69e Hadley Wickham Supress globalVar note
authored
132 #'
133 #' # We could of course have renamed the levels;
0b91dbd Hadley Wickham Fix tests
authored
134 #' # then we can apply another nifty function
5551e1b Hadley Wickham Fixes for examples
authored
135 #' msleep$conservation2 <- plyr::revalue(msleep$conservation, conservation_status)
efcc69e Hadley Wickham Supress globalVar note
authored
136 #'
5551e1b Hadley Wickham Fixes for examples
authored
137 #' p2 %+% msleep +
138 #' facet_grid(vore ~ conservation2, labeller = labeller(vore = capitalize))
139 #' p2 %+% msleep +
140 #' facet_grid(vore ~ conservation2, labeller = labeller(conservation2 =
141 #' label_wrap_gen(10)))
142 #' }
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
143 labeller <- function(..., keep.as.numeric=FALSE) {
144 args <- list(...)
efcc69e Hadley Wickham Supress globalVar note
authored
145
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
146 function(variable, values) {
147 if (is.logical(values)) {
148 values <- as.integer(values) + 1
149 } else if (is.factor(values)) {
150 values <- as.character(values)
151 } else if (is.numeric(values) & !keep.as.numeric) {
152 values <- as.character(values)
153 }
efcc69e Hadley Wickham Supress globalVar note
authored
154
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
155 res <- args[[variable]]
efcc69e Hadley Wickham Supress globalVar note
authored
156
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
157 if (is.null(res)) {
158 # If the facetting margin (i.e. `variable`) was not specified when calling
159 # labeller, default to use the actual values.
160 result <- values
efcc69e Hadley Wickham Supress globalVar note
authored
161
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
162 } else if (is.function(res)) {
163 # How should `variable` and `values` be passed to a function? ------------
efcc69e Hadley Wickham Supress globalVar note
authored
164 arguments <- length(formals(res))
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
165 if (arguments < 2) {
166 result <- res(values)
167 } else {
168 result <- res(variable, values)
efcc69e Hadley Wickham Supress globalVar note
authored
169 }
170
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
171 } else {
172 result <- res[values]
173 }
efcc69e Hadley Wickham Supress globalVar note
authored
174
193d843 Hadley Wickham New labeller functions from @stefanedwards.
authored
175 return(result)
176 }
177 }
178
179
180
063c816 Hadley Wickham More tools to display expressions in facet labels
authored
181 # Grob for strip labels
182 ggstrip <- function(text, horizontal=TRUE, theme) {
183 text_theme <- if (horizontal) "strip.text.x" else "strip.text.y"
184 if (is.list(text)) text <- text[[1]]
185
5b48cb1 Winston Chang Rename theme_render to element_render
wch authored
186 label <- element_render(theme, text_theme, text)
063c816 Hadley Wickham More tools to display expressions in facet labels
authored
187
188 ggname("strip", absoluteGrob(
189 gList(
5b48cb1 Winston Chang Rename theme_render to element_render
wch authored
190 element_render(theme, "strip.background"),
063c816 Hadley Wickham More tools to display expressions in facet labels
authored
191 label
192 ),
193 width = grobWidth(label) + unit(0.5, "lines"),
194 height = grobHeight(label) + unit(0.5, "lines")
195 ))
196 }
Something went wrong with that request. Please try again.