Skip to content

Commit 85f351c

Browse files
committed
Added two functions, labeller and label_wrap_gen.
These two functions aid when labelling facets. labeller allows for providing multiple named vectors and/or methods to change the labels for each margin. label_wrap_gen allows labels to be word wrapped.
1 parent f7269c3 commit 85f351c

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

R/facet-labels.r

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,67 @@ label_bquote <- function(expr = beta ^ .(x)) {
6262
}
6363
}
6464

65+
#' Label facets with a word wrapped label.
66+
#'
67+
#' Uses \code{\link[base]{strwrap}} for line wrapping.
68+
#' @param width integer, target column width for output.
69+
#' @export
70+
#' @seealso , \code{\link{labeller}}
71+
#' @examples
72+
#' set.seed(331)
73+
#' x=runif(60)
74+
#' y=rnorm(60)
75+
#' speed=sample(c('Prime group', 'Rib group', 'No group'), 60, replace=TRUE)
76+
#' group=sample(letters[1:3], 60, replace=TRUE)
77+
#'
78+
#' df = data.frame(x=x, y=y, speed=as.factor(speed), group=as.factor(group))
79+
#' group.names <- c('a'='First','b'='Second','c'="Don\'t")
80+
#'
81+
#' ggplot(df, aes(x, y)) + geom_point() + facet_grid(speed ~ group, labeller=label_wrap_gen(3))
82+
#' ggplot(df, aes(x, y)) + geom_point() + facet_grid(speed ~ group, labeller=labeller(speed=label_wrap_gen(3), group=group.names))
83+
label_wrap_gen <- function(width = 25) {
84+
function(variable, value) {
85+
lapply(strwrap(as.character(value), width=width, simplify=FALSE),
86+
paste, collapse="\n")
87+
}
88+
}
89+
90+
#' Generic labeller function for facets
91+
#'
92+
#' One-step function for providing methods or named character vectors
93+
#' as labels in facets.
94+
#'
95+
#' @param keep.as.numbers logical, default TRUE. When FALSE, converts numeric values supplied as margins to the facet to characters.
96+
#' @family facet labeller
97+
#' @return Function to supply to \code{\link{facet_grid}} for the argument \code{labeller}.
98+
#' @export
99+
#' @examples
100+
#' numbers <- c(`4`='four', `6`='six', `8`='eight')
101+
#' vs <- c(`0`='No vs', `1`='vs')
102+
#' p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
103+
#' p + facet_grid(vs~cyl, labeller=labeller(cyl=numbers, vs=vs))
104+
labeller <- function(keep.as.numeric=FALSE, ...) {
105+
args <- list(...)
106+
lbl <- function(variable, values) {
107+
res <- args[[variable]]
108+
if (is.numeric(values) & !keep.as.numeric) values <- as.character(values)
109+
#print(str(variable))
110+
#print(str(values))
111+
112+
if (is.null(res)) {
113+
if (is.factor(values)) return(levels(values[drop=TRUE]))
114+
return(values)
115+
}
116+
if (is.function(res)) return(res(variable, values))
117+
if (is.logical(values)) values <- as.integer(values)+1
118+
if (is.factor(values)) values <- levels(values)[values]
119+
return(res[values])
120+
}
121+
return(lbl)
122+
}
123+
124+
125+
65126
# Grob for strip labels
66127
ggstrip <- function(text, horizontal=TRUE, theme) {
67128
text_theme <- if (horizontal) "strip.text.x" else "strip.text.y"

0 commit comments

Comments
 (0)