@@ -61,94 +61,96 @@ label_bquote <- function(expr = beta ^ .(x)) {
61
61
eval(substitute(bquote(expr , list (x = x )), list (expr = quoted ))))
62
62
}
63
63
}
64
+ globalVariables(" x" )
65
+
64
66
65
67
# ' Label facets with a word wrapped label.
66
- # '
68
+ # '
67
69
# ' Uses \code{\link[base]{strwrap}} for line wrapping.
68
70
# ' @param width integer, target column width for output.
69
71
# ' @export
70
72
# ' @seealso , \code{\link{labeller}}
71
73
label_wrap_gen <- function (width = 25 ) {
72
74
function (variable , values ) {
73
- vapply(strwrap(as.character(values ), width = width , simplify = FALSE ),
75
+ vapply(strwrap(as.character(values ), width = width , simplify = FALSE ),
74
76
paste , vector(' character' , 1 ), collapse = " \n " )
75
77
}
76
78
}
77
79
78
80
# ' Generic labeller function for facets
79
- # '
81
+ # '
80
82
# ' One-step function for providing methods or named character vectors
81
83
# ' for displaying labels in facets.
82
- # '
84
+ # '
83
85
# ' The provided methods are checked for number of arguments.
84
- # ' If the provided method takes less than two
85
- # ' (e.g. \code{\link[Hmisc]{capitalize}}),
86
+ # ' If the provided method takes less than two
87
+ # ' (e.g. \code{\link[Hmisc]{capitalize}}),
86
88
# ' the method is passed \code{values}.
87
- # ' Else (e.g. \code{\link{label_both}}),
89
+ # ' Else (e.g. \code{\link{label_both}}),
88
90
# ' it is passed \code{variable} and \code{values} (in that order).
89
91
# ' If you want to be certain, use e.g. an anonymous function.
90
- # ' If errors are returned such as ``argument ".." is missing, with no default''
92
+ # ' If errors are returned such as ``argument ".." is missing, with no default''
91
93
# ' or ``unused argument (variable)'', matching the method's arguments does not
92
94
# ' work as expected; make a wrapper function.
93
- # '
94
95
# '
95
- # ' @param ... Named arguments of the form \code{variable=values},
96
- # ' where \code{values} could be a vector or method.
97
- # ' @param keep.as.numeric logical, default TRUE. When FALSE, converts numeric
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
98
100
# ' values supplied as margins to the facet to characters.
99
101
# ' @family facet labeller
100
- # ' @return Function to supply to
102
+ # ' @return Function to supply to
101
103
# ' \code{\link{facet_grid}} for the argument \code{labeller}.
102
- # ' @export
104
+ # ' @export
103
105
# ' @examples
104
- # '
106
+ # '
105
107
# ' data(mpg)
106
- # '
108
+ # '
107
109
# ' p1 <- ggplot(mpg, aes(cty, hwy)) + geom_point()
108
- # '
109
- # '
110
+ # '
111
+ # '
110
112
# ' p1 + facet_grid(cyl ~ class, labeller=label_both)
111
- # '
113
+ # '
112
114
# ' p1 + facet_grid(cyl ~ class, labeller=labeller(cyl=label_both))
113
- # '
114
- # ' ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() +
115
- # ' facet_grid(vs + am ~ gear, margins=TRUE,
115
+ # '
116
+ # ' ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() +
117
+ # ' facet_grid(vs + am ~ gear, margins=TRUE,
116
118
# ' labeller=labeller(vs=label_both, am=label_both))
117
- # '
118
- # '
119
- # '
119
+ # '
120
+ # '
121
+ # '
120
122
# ' data(msleep)
121
123
# ' capitalize <- function(string) {
122
124
# ' substr(string, 1, 1) <- toupper(substr(string, 1, 1))
123
- # ' string
125
+ # ' string
124
126
# ' }
125
127
# ' conservation_status <- c('cd'='Conservation Dependent',
126
- # ' 'en'='Endangered',
128
+ # ' 'en'='Endangered',
127
129
# ' 'lc'='Least concern',
128
- # ' 'nt'='Near Threatened',
130
+ # ' 'nt'='Near Threatened',
129
131
# ' 'vu'='Vulnerable',
130
132
# ' 'domesticated'='Domesticated')
131
- # ' ## Source: http://en.wikipedia.org/wiki/Uncyclopedia:Conservation_status
133
+ # ' ## Source: http://en.wikipedia.org/wiki/Uncyclopedia:Conservation_status
132
134
# '
133
- # ' p2 <- ggplot(msleep, aes(x=sleep_total, y=awake)) + geom_point() +
135
+ # ' p2 <- ggplot(msleep, aes(x=sleep_total, y=awake)) + geom_point() +
134
136
# ' p2 + facet_grid(vore ~ conservation, labeller=labeller(vore=capitalize))
135
- # '
136
- # ' p2 + facet_grid(vore ~ conservation,
137
+ # '
138
+ # ' p2 + facet_grid(vore ~ conservation,
137
139
# ' labeller=labeller(vore=capitalize, conservation=conservation_status ))
138
- # '
139
- # ' # We could of course have renamed the levels;
140
+ # '
141
+ # ' # We could of course have renamed the levels;
140
142
# ' # then we can apply another nifty function:
141
143
# ' library(plyr)
142
- # ' msleep$conservation2 <- revalue(msleep$conservation, conservation_status)
144
+ # ' msleep$conservation2 <- revalue(msleep$conservation, conservation_status)
143
145
# '
144
146
# ' p2 + facet_grid(vore ~ conservation2, labeller=labeller(vore=capitalize))
145
- # '
146
- # ' p2 + facet_grid(vore ~ conservation2,
147
+ # '
148
+ # ' p2 + facet_grid(vore ~ conservation2,
147
149
# ' labeller=labeller(conservation2=label_wrap_gen(10) ))
148
- # '
150
+ # '
149
151
labeller <- function (... , keep.as.numeric = FALSE ) {
150
152
args <- list (... )
151
-
153
+
152
154
function (variable , values ) {
153
155
if (is.logical(values )) {
154
156
values <- as.integer(values ) + 1
@@ -157,27 +159,27 @@ labeller <- function(..., keep.as.numeric=FALSE) {
157
159
} else if (is.numeric(values ) & ! keep.as.numeric ) {
158
160
values <- as.character(values )
159
161
}
160
-
162
+
161
163
res <- args [[variable ]]
162
-
164
+
163
165
if (is.null(res )) {
164
166
# If the facetting margin (i.e. `variable`) was not specified when calling
165
167
# labeller, default to use the actual values.
166
168
result <- values
167
-
169
+
168
170
} else if (is.function(res )) {
169
171
# How should `variable` and `values` be passed to a function? ------------
170
- arguments <- length(formals(res ))
172
+ arguments <- length(formals(res ))
171
173
if (arguments < 2 ) {
172
174
result <- res(values )
173
175
} else {
174
176
result <- res(variable , values )
175
- }
176
-
177
+ }
178
+
177
179
} else {
178
180
result <- res [values ]
179
181
}
180
-
182
+
181
183
return (result )
182
184
}
183
185
}
0 commit comments